Repository navigation
fix(threadx): drain accepted work and reject submissions after shutdown (#3927) - #3958
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3927
TL;DR
DeviceThreadPoolnow has one shutdown contract: tasks accepted beforeshutdown()are drained and their futures resolved, in-flight counters reachzero, and every later
submit/submit_serialraisesRuntimeError.gptqmodel/utils/threadx.py(+81/−46), plus a regression suite and a referencepage (
docs/device-thread-pool-shutdown.md).1. The problem
The worker exited on
_stopinstead of on its sentinel, soshutdown()couldstop a busy worker before it consumed queued tasks. Submission had no
shutdown check, so work could be enqueued behind the sentinel (never run) or
lazily start a fresh worker after close.
flowchart TD S["submit(device, fn)"] --> P["_pick_worker(key)"] P --> M["_mark_scheduled(key)"] M --> E["worker.submit(...)"] E --> Q["queued behind a running task"] H["shutdown(wait=True)"] --> B["_stop_event.set()"] B --> C["snapshot groups under _dispatch_lock"] C --> D["w.stop() outside the lock<br/>set _stop, enqueue sentinel"] D --> W{"worker loop checks _stop"} W -- "set -> exits after current task" --> A["queued futures abandoned<br/>_inflight never drains"] style A fill:#fdd,stroke:#c00Mechanisms removed, with
origin/mainatd1d3da04:while not self._stop.is_set():gptqmodel/utils/threadx.py:518submit()picks/enqueues with no shutdown checkgptqmodel/utils/threadx.py:1146submit_serial()enqueues after releasing the lockgptqmodel/utils/threadx.py:1175_pick_worker()can lazily spawn during/after shutdowngptqmodel/utils/threadx.py:1465shutdown()issuesw.stop()outside the lockgptqmodel/utils/threadx.py:1233,:1257threading.Lock()gptqmodel/utils/threadx.py:7252. The new flow
Submission: accept or reject atomically
flowchart TD S["submit(device, fn) / submit_serial(device, fn)"] --> L["acquire _dispatch_lock (RLock)"] L --> Q{"_stop_event set?"} Q -- "Yes (shutdown started)" --> R["raise RuntimeError<br/>no new work accepted"] Q -- "No" --> P["_pick_worker_locked / _refresh_serial_worker_locked"] P --> M["_mark_scheduled(key)<br/>inflight += 1"] M --> E["worker.submit(...)<br/>enqueue task ahead of sentinels"] E --> U["release _dispatch_lock"] U --> D["worker _run loop: execute queued tasks in FIFO order"] D --> T{"sentinel reached?"} T -- "No" --> D T -- "Yes (queue drained)" --> X["break; _on_worker_exit removes worker"] R --> Z["caller sees RuntimeError"] style R fill:#fdd,stroke:#c00 style Z fill:#fdd,stroke:#c00 style X fill:#dfd,stroke:#060Shutdown: stop accepting, then drain
flowchart TD H["shutdown(wait=True)"] --> A["acquire _dispatch_lock"] A --> B["_stop_event.set()"] B --> C["snapshot _worker_groups"] C --> D["for each worker: w.stop()<br/>set _stop, enqueue sentinel"] D --> E["release _dispatch_lock"] E --> F["_gc_event.set(); join janitor"] F --> G{"wait?"} G -- "Yes" --> I["join every worker thread"] G -- "No" --> J["return"] I --> K["workers drained every accepted task<br/>then exited on the sentinel"] style B fill:#ffd,stroke:#a60 style K fill:#dfd,stroke:#060stop()enqueues its sentinel after every task the pool already accepted, soreaching the sentinel proves the queue is drained.
_dispatch_lockis now anRLockbecause_SyncWorker.stop()re-enters_on_worker_exit, which takesthe same lock, from the shutting-down thread.
3. Failure and ambiguity contract
stateDiagram-v2 state "Accepting" as A state "Draining" as D state "Stopped" as S [*] --> A A --> A: submit accepted, task queued A --> D: shutdown() sets _stop_event A --> A: submit after shutdown observed -> RuntimeError D --> D: workers drain queued tasks D --> S: every worker joined S --> S: submit -> RuntimeError style S fill:#dfd,stroke:#060shutdown()starts_inflightstuck,wait()can hang_inflightreaches 0submitracingshutdown()submitaftershutdown()RuntimeError("DeviceThreadPool is shutting down; ...")submit_serialaftershutdown()RuntimeError_SyncWorkerstop during shutdownRLockprevents self-deadlockNo silent fallback: rejection raises, and an accepted task always runs.
4. Change inventory
gptqmodel/utils/threadx.pyRLock, stops issued under the locktests/test_threadx_shutdown.pydocs/device-thread-pool-shutdown.mdREADME.md5. Tests / guarantees
test_shutdown_drains_a_queued_task_inflight == 0,wait()returnstest_shutdown_drains_every_queued_tasktest_submit_after_shutdown_is_rejectedRuntimeErroron latesubmittest_submit_serial_after_shutdown_is_rejectedRuntimeErroron latesubmit_serialtest_lazy_worker_selection_does_not_spawn_after_shutdowntest_a_shutdown_pool_rejects_work_promptlytest_sync_worker_stop_does_not_self_deadlockRLockre-entry is safe6. Validation
GPU:
CUDA_VISIBLE_DEVICES=GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae,HIP_VISIBLE_DEVICES=0.Before the fix (source stashed, tests kept):
After the fix:
Issue reproducer, after the fix:
thread alive: False,f1 done: True,f2 done: True -> second,inflight: {'cpu': 0}(before:f2 done: False,inflight: {'cpu': 1}).7. Reviewer notes
threadingprimitives are reordered),so
pytorch_gil_0_compat.mdis unchanged.GPTQMODEL_*switch is added; the contract is unconditional.cancellation would race with a task that is already executing, while draining
keeps in-flight accounting exact.
docs/device-thread-pool-shutdown.mdis the committed reference page and islinked from
README.mdbeside the CPU pool lock scopes entry.