Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

fix(server): release a failed load after the task step that delivered its error (#1039)

Krisztian Gajdar4a55858a3028
diff --git a/packages/sie_server/src/sie_server/core/registry.py b/packages/sie_server/src/sie_server/core/registry.py
index ae312a67a..7b61da1cd 100644
--- a/packages/sie_server/src/sie_server/core/registry.py
+++ b/packages/sie_server/src/sie_server/core/registry.py
@@ -178,8 +178,10 @@ def _adapter_has_releasable_memory(adapter: ModelAdapter) -> bool:
def _collect_then_unload(adapter: ModelAdapter) -> None:
"""Release a failed load: collect garbage, then ``unload()``.
- The failed attempt's frames take two collector passes to free and
- ``unload()`` runs one, so they would outlive its cache release.
+ The failed attempt's allocations are reachable only through its
+ exception's traceback, so a collection frees them only once nothing
+ refers to that exception. The caller must have left the ``except``
+ clause and, on the event loop, ended the task step that delivered it.
"""
gc.collect()
adapter.unload()
@@ -1865,8 +1867,17 @@ class ModelRegistry:
async def _release_failed_load(self, adapter: ModelAdapter, name: str) -> bool:
"""Free the device memory a load that raised had already allocated; False if that raised."""
+ teardown = functools.partial(_collect_then_unload, adapter)
try:
- await self._run_adapter_unload(adapter, name, teardown=functools.partial(_collect_then_unload, adapter))
+ try:
+ # The task step that threw the load's exception into this
+ # coroutine holds it until the coroutine next suspends.
+ await asyncio.sleep(0)
+ except asyncio.CancelledError:
+ with contextlib.suppress(Exception):
+ await self._run_adapter_unload(adapter, name, teardown=teardown)
+ raise
+ await self._run_adapter_unload(adapter, name, teardown=teardown)
except Exception:
logger.exception("Releasing the failed load of '%s' raised", name)
return False
diff --git a/packages/sie_server/tests/core/test_registry_load_oom.py b/packages/sie_server/tests/core/test_registry_load_oom.py
index 9bcfd776a..df607a988 100644
--- a/packages/sie_server/tests/core/test_registry_load_oom.py
+++ b/packages/sie_server/tests/core/test_registry_load_oom.py
@@ -179,14 +179,6 @@ def _stop_patches() -> Iterator[None]:
patch.stopall()
[email protected](autouse=True)
-def _collect_the_whole_heap() -> None:
- # A failed load's memory is free only once the event loop task that
- # received its exception has yielded. A collection over the whole heap
- # takes long enough for that; one that skips frozen objects does not.
- gc.unfreeze()
-
-
async def test_retry_runs_after_the_failed_attempt_is_released() -> None:
device = _Device(total_bytes=10 * GIB)
registry = await _registry(
@@ -422,6 +414,32 @@ async def test_cancelling_the_load_while_the_attempt_is_released_still_unloads_i
assert registry.loaded_model_names == ["pinned", "cold"]
+async def test_cancelling_the_load_before_the_release_starts_still_unloads_the_attempt() -> None:
+ device = _Device(total_bytes=10 * GIB)
+ registry = await _registry(
+ device,
+ {"pinned": 3 * GIB, "cold": 3 * GIB, "new": 5 * GIB},
+ pinned=["pinned"],
+ resident=["pinned", "cold"],
+ )
+ load = asyncio.ensure_future(registry.load_now("new", "cpu"))
+ is_oom_error = registry._is_oom_error
+
+ def cancel_while_the_oom_is_handled(error: RuntimeError) -> bool:
+ load.cancel()
+ return is_oom_error(error)
+
+ with (
+ patch.object(registry, "_is_oom_error", cancel_while_the_oom_is_handled),
+ pytest.raises(asyncio.CancelledError),
+ ):
+ await load
+
+ device.adapters["new"][0].unload.assert_called_once()
+ assert not any(device.stranded_bytes)
+ assert registry.loaded_model_names == ["pinned", "cold"]
+
+
async def test_load_that_starts_during_an_eviction_waits_for_its_memory() -> None:
device = _Device(total_bytes=10 * GIB)
registry = await _registry(