Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Hold the diffusion load worker on an event instead of a sleep (#11392)

Daniel Han9ba936a5c486
diff --git a/studio/backend/tests/test_diffusion_backend.py b/studio/backend/tests/test_diffusion_backend.py
index 786826a8a27..fd0a999df91 100644
--- a/studio/backend/tests/test_diffusion_backend.py
+++ b/studio/backend/tests/test_diffusion_backend.py
@@ -2494,19 +2494,49 @@ def test_begin_load_rejects_concurrent(monkeypatch):
monkeypatch.setattr(
DiffusionBackend, "_estimate_download_bytes", staticmethod(lambda *a, **k: (0, []))
)
- # Block the spawned worker so the load stays "in progress".
- monkeypatch.setattr(
- DiffusionBackend, "load_pipeline", lambda self, **k: __import__("time").sleep(0.2)
- )
- before = set(threading.enumerate())
+ # Hold the spawned worker inside the load, and drain that worker by identity rather than
+ # by diffing threading.enumerate(). The old drain joined every thread that had appeared
+ # since the call, and enumerate() includes threads that have called start() but have not
+ # reached the point where join() is legal, so it failed run 35542154102 on a commit that
+ # touched none of this:
+ #
+ # tests/test_diffusion_backend.py:2509: in test_begin_load_rejects_concurrent
+ # thread.join(timeout = 5)
+ # RuntimeError: cannot join thread before it is started
+ #
+ # Filtering that set is not a fix. CPython assigns Thread.ident before it sets the
+ # `_started` event join() actually waits on, so a thread can be enumerated, carry an
+ # ident, and still raise. Since enumerate() is process-global the offending thread need
+ # not even belong to this test. Recording the worker from inside the stub sidesteps all
+ # of it: a thread that is running its own target is past that window by definition.
+ #
+ # Holding it on an event rather than a 0.2s sleep also makes the rejection below an
+ # assertion about a load that is genuinely running, rather than one that happens to be
+ # inside a sleep that has not elapsed yet.
+ holding = threading.Event()
+ entered = threading.Event()
+ worker = []
+
+ def held(self, **kwargs):
+ # Recorded from inside the worker, so the drain below joins exactly the thread this
+ # test started and nothing else. A thread running its own target has passed the
+ # point where join() is legal, which enumerating cannot establish.
+ worker.append(threading.current_thread())
+ entered.set()
+ # Bounded so a mistake here cannot hang the suite. The release below is what is
+ # expected to end this wait; the timeout is only a backstop.
+ assert holding.wait(timeout = 30), "the test never released the load worker"
+
+ monkeypatch.setattr(DiffusionBackend, "load_pipeline", held)
backend.begin_load("unsloth/Z-Image-Turbo-GGUF", gguf_filename = "z-image-turbo-Q4_K_S.gguf")
+ assert entered.wait(timeout = 30), "the load worker never reached load_pipeline"
with pytest.raises(RuntimeError):
backend.begin_load("unsloth/Z-Image-Turbo-GGUF", gguf_filename = "z-image-turbo-Q4_K_S.gguf")
- # Drain the worker while the stubs above still make it exit in 0.2s: begin_load's thread is
- # fire-and-forget, so left running it outlives this test and then runs the REAL load_pipeline
- # inside whatever test is current, under that test's patches.
- for thread in set(threading.enumerate()) - before:
- thread.join(timeout = 5)
+ # Drain the worker: begin_load's thread is fire-and-forget, so left running it outlives
+ # this test and then runs the REAL load_pipeline inside whatever test is current, under
+ # that test's patches.
+ holding.set()
+ worker[0].join(timeout = 5)
def test_unload_cancels_in_flight_load(fake_runtime):