Skip to content

DeviceThreadPool shutdown leaves queued futures pending and permits work after close #3927

Closed

Description

@Qubitium

Reproduction (main at 9dbcc92)

With one CPU worker, submit a blocking first task and a second queued task, start shutdown(wait=True) on another thread, then release the first task. On the current code, shutdown returns and joins the worker, the first future resolves, but the second future remains pending forever and pool._inflight['cpu'] stays at 1. This was reproduced on CPU:

pool = DeviceThreadPool(devices=[torch.device('cpu')], workers={'cpu': 1}, empty_cache_every_n=0)
entered, release = threading.Event(), threading.Event()
def first():
    entered.set(); release.wait(); return 'first'
f1 = pool.submit('cpu', first)
entered.wait()
f2 = pool.submit('cpu', lambda: 'second')
t = threading.Thread(target=lambda: pool.shutdown(wait=True))
t.start(); assert pool._worker_groups['cpu'][0]._stop.wait(2); release.set(); t.join()
print(t.is_alive(), f1.done(), f2.done(), pool._inflight)
# False True False {'cpu': 1}

Cause

_DeviceWorker.stop sets _stop immediately and enqueues a sentinel, but _DeviceWorker._run loops only while _stop is unset. DeviceThreadPool.shutdown calls stop() before draining the queue, so the worker exits after the current task and never consumes queued tasks or resolves their futures. A caller waiting on f2.result() or pool.wait() can hang indefinitely.

The same lifecycle boundary is not enforced on submission: submit / submit_serial do not reject work after _stop_event is set, and lazy worker selection can start a new thread after shutdown. This should be fixed with the queue-drain/stop behavior as one shutdown contract.

Expected

shutdown(wait=True) should finish previously accepted tasks (or explicitly cancel and resolve their futures) and leave accurate in-flight counters. Submissions after shutdown starts should be rejected. Add a deterministic blocked-first-task/queued-second-task regression test.

Activity

  1. Qubitium commented on Nov 14, 2026

    @Qubitium
    CollaboratorAuthor

    Additional GIL=0 shutdown deadlock on main at 221cd564 (separate from the queued-future behavior above).

    shutdown(wait=True) sets _stop_event and _gc_event, then joins the janitor before stopping workers. The janitor can instead be blocked in _auto_gc_disable_cv.wait() inside no_auto_gc(). Shutdown never notifies that condition, so its join waits until the context exits; if the context remains open, it can wait indefinitely.

    Reproduced on Python 3.14.10 with PYTHON_GIL=0, sys._is_gil_enabled() == False, Torch 2.15 CUDA, one GPU:

    p = DeviceThreadPool(devices=[torch.device('cuda:0')], include_cpu=False,
                         empty_cache_every_n=1, gc_debounce_seconds=0,
                         workers={'cuda:0': 1})
    entered = threading.Event()
    wait = p._auto_gc_disable_cv.wait
    p._auto_gc_disable_cv.wait = lambda *a, **kw: (entered.set(), wait(*a, **kw))[1]
    with p.no_auto_gc():
        p.do('cuda:0', lambda: None)
        assert entered.wait(5)
        t = threading.Thread(target=lambda: p.shutdown(wait=True), daemon=True)
        t.start(); t.join(0.5)
        assert t.is_alive()  # shutdown is blocked
    # The context exit notifies the condition; shutdown now finishes.
    t.join(5)
    assert not t.is_alive()

    The janitor must be notified when shutdown sets the stop event. This belongs with the shutdown lifecycle fix and merits a regression test that holds no_auto_gc() open while shutdown begins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions