code: def _find_free_network_port_cloudspace():
      """Finds a free port in the exposed range when running in a cloudspace."""
      for port in range(
          constants.APP_SERVER_PORT
          + 1,  # constants.APP_SERVER_PORT is reserved for the app server
          constants.APP_SERVER_PORT
          + constants.LIGHTNING_CLOUDSPACE_EXPOSED_PORT_COUNT,
      ):
          if port in _reserved_ports:
              continue
  
          try:
              sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
              sock.bind(("", port))
              sock.close()
              _reserved_ports.add(port)
              return port
          except OSError:
              continue
  
      # This error should never happen. An app using this many ports would probably fail on a single machine anyway.
      raise RuntimeError(
          f"All {constants.LIGHTNING_CLOUDSPACE_EXPOSED_PORT_COUNT} ports are already in use."
      )
  
imports: import socket
  from functools import wraps
  from typing import Any, Callable, Dict, Optional
  from urllib.parse import urljoin
  import requests
  from lightning_cloud.rest_client import GridRestClient, LightningClient, create_swagger_client
  from requests import Session
  from requests.adapters import HTTPAdapter
  from requests.exceptions import ConnectionError, ConnectTimeout, ReadTimeout
  from urllib3.util.retry import Retry
  from lightning.app.core import constants
  from lightning.app.utilities.app_helpers import Logger
label_desc: Use image node_id 2 for calling a global function with example usage: _find_free_network_port_cloudspace() and returns: port