From 489e8e7f3cf583d017f865a9358f0f9452a78fb1 Mon Sep 17 00:00:00 2001 From: Andrey Cheptsov Date: Tue, 24 Nov 2026 19:13:11 +0100 Subject: [PATCH 1/2] [Nebius] Fail provisioning of stopped instances Nebius assigns addresses to an instance that is still starting, so update_provisioning_data() set the hostname before the instance ran, and the server never looked at the instance's state again. An instance that Nebius stopped instead of starting was found only by the 10-minute limit. Co-Authored-By: Claude Fable 5.2 --- .../_internal/core/backends/nebius/compute.py | 15 ++++ .../core/backends/nebius/test_compute.py | 72 ++++++++++++++++++- 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/src/dstack/_internal/core/backends/nebius/compute.py b/src/dstack/_internal/core/backends/nebius/compute.py index 0fc7d077a..071d57742 100644 --- a/src/dstack/_internal/core/backends/nebius/compute.py +++ b/src/dstack/_internal/core/backends/nebius/compute.py @@ -407,6 +407,21 @@ def update_provisioning_data( self, provisioning_data, project_ssh_public_key, project_ssh_private_key ): instance = resources.get_instance(self._sdk, provisioning_data.instance_id) + state = instance.status.state + if state in [ + InstanceStatus.InstanceState.STOPPING, + InstanceStatus.InstanceState.STOPPED, + InstanceStatus.InstanceState.DELETING, + InstanceStatus.InstanceState.ERROR, + ]: + raise ProvisioningError( + f"Failed to get instance IP address. Instance state: {state.name}" + ) + # Nebius assigns addresses to an instance that is still starting, and this method + # is no longer called once the hostname is set. So the hostname is set only when + # the instance is running. Otherwise, a failed start would go unnoticed. + if state != InstanceStatus.InstanceState.RUNNING: + return if not instance.status.network_interfaces: return interface = instance.status.network_interfaces[0] diff --git a/src/tests/_internal/core/backends/nebius/test_compute.py b/src/tests/_internal/core/backends/nebius/test_compute.py index 8f91bf403..d66c8af72 100644 --- a/src/tests/_internal/core/backends/nebius/test_compute.py +++ b/src/tests/_internal/core/backends/nebius/test_compute.py @@ -22,6 +22,9 @@ Instance, InstanceSpec, InstanceStatus, + IPAddressStatus, + NetworkInterfaceStatus, + PublicIPAddressStatus, ResourcesSpec, ) @@ -33,7 +36,7 @@ NebiusOfferBackendData, NebiusServiceAccountCreds, ) -from dstack._internal.core.errors import NoCapacityError, NotYetTerminated +from dstack._internal.core.errors import NoCapacityError, NotYetTerminated, ProvisioningError from dstack._internal.core.models.backends.base import BackendType from dstack._internal.core.models.backends.orphans import ListedResourceGroup, OwnerType from dstack._internal.core.models.instances import ( @@ -49,6 +52,7 @@ PlacementGroupProvisioningData, PlacementStrategy, ) +from dstack._internal.core.models.runs import JobProvisioningData CapacityAvailability = ResourceAdviceStatus.Availability CAPACITY_LEVELS = CapacityAvailability.AvailabilityLevel @@ -735,6 +739,72 @@ def make_resource_group( ) +def make_provisioning_data() -> JobProvisioningData: + return JobProvisioningData( + backend=BackendType.NEBIUS, + instance_type=make_offer(1, spot=False, is_preemptible_flat_rate=False).instance, + instance_id="instance-1", + hostname=None, + region="eu-north1", + price=1, + username="ubuntu", + ssh_port=22, + dockerized=True, + ) + + +class TestUpdateProvisioningData: + @pytest.fixture(autouse=True) + def _mock_sdk(self, mocker): + mocker.patch.object(NebiusCompute, "_sdk", Mock()) + + @pytest.fixture + def mock_instance(self, mocker): + def mock(state: InstanceStatus.InstanceState) -> None: + instance = make_instance(state=state) + instance.status.network_interfaces = [ + NetworkInterfaceStatus( + ip_address=IPAddressStatus(address="10.0.0.5/32"), + public_ip_address=PublicIPAddressStatus(address="203.0.113.5/32"), + ) + ] + mocker.patch.object(compute_module.resources, "get_instance", return_value=instance) + + return mock + + @pytest.mark.parametrize( + ("state", "expected_hostname"), + [ + (InstanceStatus.InstanceState.STARTING, None), + (InstanceStatus.InstanceState.RUNNING, "203.0.113.5"), + ], + ) + def test_sets_hostname_only_when_running(self, mock_instance, state, expected_hostname): + mock_instance(state) + provisioning_data = make_provisioning_data() + + make_compute().update_provisioning_data(provisioning_data, "public-key", "private-key") + + assert provisioning_data.hostname == expected_hostname + + @pytest.mark.parametrize( + "state", + [ + InstanceStatus.InstanceState.STOPPING, + InstanceStatus.InstanceState.STOPPED, + InstanceStatus.InstanceState.DELETING, + InstanceStatus.InstanceState.ERROR, + ], + ) + def test_raises_if_instance_will_not_run(self, mock_instance, state): + mock_instance(state) + + with pytest.raises(ProvisioningError, match=state.name): + make_compute().update_provisioning_data( + make_provisioning_data(), "public-key", "private-key" + ) + + class TestListResourceGroups: @pytest.fixture(autouse=True) def _mock_sdk(self, mocker): From 356b98e0013636c39c59d1fd238d99caa1e485a2 Mon Sep 17 00:00:00 2001 From: Andrey Cheptsov Date: Tue, 24 Nov 2026 21:47:02 +0100 Subject: [PATCH 2/2] [Nebius] Don't fail a VM that is stopped before it starts A new VM is `STOPPED` for a few seconds before it starts, with `reconciling` set. `STOPPED` now fails provisioning only when Nebius has no operation left to run on the VM, and `STOPPING` no longer does. The error also says what failed: the VM had its address, it did not start. Co-Authored-By: Claude Fable 5.2 --- .../_internal/core/backends/nebius/compute.py | 13 +++++++------ .../core/backends/nebius/test_compute.py | 18 +++++++++++------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/dstack/_internal/core/backends/nebius/compute.py b/src/dstack/_internal/core/backends/nebius/compute.py index 071d57742..b9ead10e4 100644 --- a/src/dstack/_internal/core/backends/nebius/compute.py +++ b/src/dstack/_internal/core/backends/nebius/compute.py @@ -408,15 +408,16 @@ def update_provisioning_data( ): instance = resources.get_instance(self._sdk, provisioning_data.instance_id) state = instance.status.state - if state in [ - InstanceStatus.InstanceState.STOPPING, - InstanceStatus.InstanceState.STOPPED, + # A new instance is `STOPPED` before it starts, with `reconciling` set. `STOPPED` + # is final once Nebius has no operation left to run on the instance. + is_stopped = ( + state == InstanceStatus.InstanceState.STOPPED and not instance.status.reconciling + ) + if is_stopped or state in [ InstanceStatus.InstanceState.DELETING, InstanceStatus.InstanceState.ERROR, ]: - raise ProvisioningError( - f"Failed to get instance IP address. Instance state: {state.name}" - ) + raise ProvisioningError(f"Instance failed to start. Instance state: {state.name}") # Nebius assigns addresses to an instance that is still starting, and this method # is no longer called once the hostname is set. So the hostname is set only when # the instance is running. Otherwise, a failed start would go unnoticed. diff --git a/src/tests/_internal/core/backends/nebius/test_compute.py b/src/tests/_internal/core/backends/nebius/test_compute.py index d66c8af72..aa5619ecb 100644 --- a/src/tests/_internal/core/backends/nebius/test_compute.py +++ b/src/tests/_internal/core/backends/nebius/test_compute.py @@ -760,8 +760,9 @@ def _mock_sdk(self, mocker): @pytest.fixture def mock_instance(self, mocker): - def mock(state: InstanceStatus.InstanceState) -> None: + def mock(state: InstanceStatus.InstanceState, reconciling: bool = False) -> None: instance = make_instance(state=state) + instance.status.reconciling = reconciling instance.status.network_interfaces = [ NetworkInterfaceStatus( ip_address=IPAddressStatus(address="10.0.0.5/32"), @@ -773,14 +774,18 @@ def mock(state: InstanceStatus.InstanceState) -> None: return mock @pytest.mark.parametrize( - ("state", "expected_hostname"), + ("state", "reconciling", "expected_hostname"), [ - (InstanceStatus.InstanceState.STARTING, None), - (InstanceStatus.InstanceState.RUNNING, "203.0.113.5"), + # A new instance is stopped before it starts + (InstanceStatus.InstanceState.STOPPED, True, None), + (InstanceStatus.InstanceState.STARTING, True, None), + (InstanceStatus.InstanceState.RUNNING, False, "203.0.113.5"), ], ) - def test_sets_hostname_only_when_running(self, mock_instance, state, expected_hostname): - mock_instance(state) + def test_sets_hostname_only_when_running( + self, mock_instance, state, reconciling, expected_hostname + ): + mock_instance(state, reconciling) provisioning_data = make_provisioning_data() make_compute().update_provisioning_data(provisioning_data, "public-key", "private-key") @@ -790,7 +795,6 @@ def test_sets_hostname_only_when_running(self, mock_instance, state, expected_ho @pytest.mark.parametrize( "state", [ - InstanceStatus.InstanceState.STOPPING, InstanceStatus.InstanceState.STOPPED, InstanceStatus.InstanceState.DELETING, InstanceStatus.InstanceState.ERROR,