Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(tests): identify the cache worker thread instead of counting threads

Alexander Eichhorn68e5e5154a59
diff --git a/tests/backend/model_manager/load/model_cache/test_model_cache_ram_budget.py b/tests/backend/model_manager/load/model_cache/test_model_cache_ram_budget.py
index 6e328ea16a..bc186af479 100644
--- a/tests/backend/model_manager/load/model_cache/test_model_cache_ram_budget.py
+++ b/tests/backend/model_manager/load/model_cache/test_model_cache_ram_budget.py
@@ -3721,16 +3721,17 @@ def test_dropped_cache_is_collectable_and_its_worker_exits(mock_logger):
store = SharedCpuWeightsStore()
budget = RamBudget(max_bytes=int(S * 8), shared_store=store)
- def worker_count() -> int:
- return sum(1 for t in threading.enumerate() if t.name == "model-cache-deferred-work")
+ def workers() -> set[threading.Thread]:
+ return {t for t in threading.enumerate() if t.name == "model-cache-deferred-work"}
- before = worker_count()
+ # Identity, not a count: a worker from an earlier test in this process may still be winding
+ # down, and one starting as another exits leaves the count unchanged. Threads also start
+ # asynchronously, so the set is converged on rather than sampled at one instant.
+ before = workers()
cache = _make_cache(store, budget, mock_logger)
module = DummyModule()
cache.put("model", module)
- # The worker starts asynchronously, and a worker from an earlier test may still be winding
- # down, so the count is converged on rather than sampled at one instant.
- assert _wait_until(lambda: worker_count() == before + 1), "the cache never started its worker"
+ assert _wait_until(lambda: workers() - before), "the cache never started its worker"
cache_ref = weakref.ref(cache)
module_ref = weakref.ref(module)
@@ -3740,7 +3741,7 @@ def test_dropped_cache_is_collectable_and_its_worker_exits(mock_logger):
assert cache_ref() is None, "the worker thread kept the ModelCache alive"
assert module_ref() is None, "the dropped cache's model is still resident in RAM"
# The finalizer wakes the parked worker so it exits rather than leaking a thread per cache.
- assert _wait_until(lambda: worker_count() == before), "the worker thread outlived its cache"
+ assert _wait_until(lambda: not workers() - before), "the worker thread outlived its cache"
def test_dropped_non_shared_cache_releases_only_its_budget_charge(mock_logger):