diff --git a/src/dstack/_internal/server/background/pipeline_tasks/gateways.py b/src/dstack/_internal/server/background/pipeline_tasks/gateways.py index 300c6a1c6..bf9b84608 100644 --- a/src/dstack/_internal/server/background/pipeline_tasks/gateways.py +++ b/src/dstack/_internal/server/background/pipeline_tasks/gateways.py @@ -407,7 +407,7 @@ async def _process_submitted_gateway(gateway_model: GatewayModel) -> _SubmittedR update_map["hostname"] = backend_data.hostname update_map["backend_data"] = backend_data.backend_data - scale_result = _reconcile_gateway_replica_count(gateway_model, gateway_replicas=[]) + scale_result = await _reconcile_gateway_replica_count(gateway_model, gateway_replicas=[]) update_map["status"] = GatewayStatus.PROVISIONING update_map.update(scale_result.gateway_update_map) return _SubmittedResult( @@ -444,7 +444,7 @@ async def _process_provisioning_item(item: GatewayPipelineItem): log_lock_token_mismatch(logger, item) return - result = _process_provisioning_gateway(gateway_model) + result = await _process_provisioning_gateway(gateway_model) gateway_update_map = result.gateway_update_map set_processed_update_map_fields(gateway_update_map) set_unlock_update_map_fields(gateway_update_map) @@ -481,12 +481,12 @@ class _ProvisioningResult: scale_result: _ReplicaScalingResult = field(default_factory=_ReplicaScalingResult) -def _process_provisioning_gateway(gateway_model: GatewayModel) -> _ProvisioningResult: +async def _process_provisioning_gateway(gateway_model: GatewayModel) -> _ProvisioningResult: gateway_replicas = get_gateway_replica_models(gateway_model) # Provisioning gateways must have a replica. assert len(gateway_replicas) > 0 - scale_result = _reconcile_gateway_replica_count(gateway_model, gateway_replicas) + scale_result = await _reconcile_gateway_replica_count(gateway_model, gateway_replicas) statuses = { replica.status for replica in gateway_replicas @@ -553,7 +553,7 @@ async def _process_running_item(item: GatewayPipelineItem): return gateway_replicas = get_gateway_replica_models(gateway_model) - scale_result = _reconcile_gateway_replica_count(gateway_model, gateway_replicas) + scale_result = await _reconcile_gateway_replica_count(gateway_model, gateway_replicas) update_map = _GatewayUpdateMap() update_map.update( @@ -736,7 +736,7 @@ def _get_active_replica_filters() -> list[ColumnElement[bool]]: ] -def _reconcile_gateway_replica_count( +async def _reconcile_gateway_replica_count( gateway_model: GatewayModel, gateway_replicas: list[GatewayReplicaModel], ) -> _ReplicaScalingResult: @@ -773,7 +773,7 @@ def _reconcile_gateway_replica_count( } new_nums = itertools.islice(get_lowest_unused_nums(used_nums), diff) new_gateway_replica_models = [ - gateways_services.create_gateway_replica_model( + await gateways_services.create_gateway_replica_model( gateway_model=gateway_model, replica_num=replica_num, ) diff --git a/src/dstack/_internal/server/background/scheduled_tasks/offers_catalog.py b/src/dstack/_internal/server/background/scheduled_tasks/offers_catalog.py index 9fafd21a3..8f770b241 100644 --- a/src/dstack/_internal/server/background/scheduled_tasks/offers_catalog.py +++ b/src/dstack/_internal/server/background/scheduled_tasks/offers_catalog.py @@ -9,6 +9,8 @@ async def preload_offers_catalog(): """Pre-load the `gpuhunt` offers catalog so the get offer requests do not pay the catalog download cost.""" logger.debug("Pre-loading offers catalog") - catalog = gpuhunt.default_catalog() + # The first `default_catalog()` call downloads and parses the catalog, so it must not run + # on the event loop. Later calls return the cached catalog, which `load()` refreshes. + catalog = await run_async(gpuhunt.default_catalog) await run_async(catalog.load) logger.debug("Pre-loaded offers catalog") diff --git a/src/dstack/_internal/server/services/gateways/__init__.py b/src/dstack/_internal/server/services/gateways/__init__.py index 678649426..5286dcd5b 100644 --- a/src/dstack/_internal/server/services/gateways/__init__.py +++ b/src/dstack/_internal/server/services/gateways/__init__.py @@ -85,6 +85,7 @@ get_current_datetime, get_or_error, interpolate_gateway_domain, + run_async, ) from dstack._internal.utils.logging import get_logger @@ -195,14 +196,14 @@ async def get_gateway_by_name( return gateway_model_to_gateway(gateway, default_gateway_id=project.default_gateway_id) -def create_gateway_replica_model( +async def create_gateway_replica_model( gateway_model: GatewayModel, replica_num: int, ) -> GatewayReplicaModel: configuration = get_gateway_configuration(gateway_model) replica_name = f"{gateway_model.name}-{replica_num}" - private_bytes, public_bytes = crypto.generate_rsa_key_pair_bytes() + private_bytes, public_bytes = await run_async(crypto.generate_rsa_key_pair_bytes) gateway_ssh_private_key = private_bytes.decode() gateway_ssh_public_key = public_bytes.decode() diff --git a/src/dstack/_internal/server/services/jobs/configurators/base.py b/src/dstack/_internal/server/services/jobs/configurators/base.py index 2d18ba6ed..39338591e 100644 --- a/src/dstack/_internal/server/services/jobs/configurators/base.py +++ b/src/dstack/_internal/server/services/jobs/configurators/base.py @@ -228,7 +228,7 @@ async def _get_job_spec( retry=self._retry(), working_dir=self._working_dir(), volumes=self._volumes(job_num), - ssh_key=self._ssh_key(jobs_per_replica), + ssh_key=await self._ssh_key(jobs_per_replica), repo_data=self.run_spec.repo_data, repo_code_hash=self.run_spec.repo_code_hash, repo_dir=self._repo_dir(), @@ -484,11 +484,13 @@ def _python(self) -> str: def _volumes(self, job_num: int) -> List[MountPoint]: return interpolate_job_volumes(self.run_spec.configuration.volumes, job_num) - def _ssh_key(self, jobs_per_replica: int) -> Optional[JobSSHKey]: + async def _ssh_key(self, jobs_per_replica: int) -> Optional[JobSSHKey]: if jobs_per_replica < 2: return None if self._job_ssh_key is None: - private, public = crypto.generate_rsa_key_pair_bytes(comment="dstack_job") + private, public = await run_async( + crypto.generate_rsa_key_pair_bytes, comment="dstack_job" + ) self._job_ssh_key = JobSSHKey( private=private.decode(), public=public.decode(),