Skip to content

[3.12] gh-125451: Fix deadlock in ProcessPoolExecutor shutdown (GH-125492) - #125599

Merged
colesbury merged 1 commit into
python:3.12from
colesbury:backport-760872e-3.12
Oct 16, 2024
Merged

colesbury merged 1 commit into
python:3.12from
colesbury:backport-760872e-3.12

Conversation

@colesbury

@colesbury colesbury commented Oct 16, 2024 •

Copy link
Copy Markdown
Contributor

There was a deadlock when ProcessPoolExecutor shuts down at the same time that a queueing thread handles an error processing a task.

Don't use _shutdown_lock to protect the _ThreadWakeup pipes -- use an internal lock instead. This fixes the ordering deadlock where the ExecutorManagerThread holds the _shutdown_lock and joins the queueing thread, while the queueing thread is attempting to acquire the _shutdown_lock while closing the _ThreadWakeup.
(cherry picked from commit 760872e)

…ythonGH-125492)

There was a deadlock when `ProcessPoolExecutor` shuts down at the same
time that a queueing thread handles an error processing a task.

Don't use `_shutdown_lock` to protect the `_ThreadWakeup` pipes -- use
an internal lock instead. This fixes the ordering deadlock where the
`ExecutorManagerThread` holds the `_shutdown_lock` and joins the
queueing thread, while the queueing thread is attempting to acquire the
`_shutdown_lock` while closing the `_ThreadWakeup`.
(cherry picked from commit 760872e)

Co-authored-by: Sam Gross <colesbury@gmail.com>
@colesbury
colesbury merged commit 4256847 into python:3.12 Oct 16, 2024
@colesbury
colesbury deleted the backport-760872e-3.12 branch October 16, 2024 18:03
danielhanchen added a commit to unslothai/unsloth that referenced this pull request Dec 2, 2026
…he re-fork hang (#20665)

## Summary

Inductor shuts its async compile pool down after 60 s idle and forks a
new one on the next compile. On CPython 3.10, 3.11, 3.12.0 to 3.12.7 and
3.13.0, a fork that overlaps the old pool's shutdown can inherit a held
`_shutdown_lock`, and the compile never returns (pytorch/pytorch#200848;
fixed in CPython 3.12.8 and 3.13.1 by python/cpython#125599).

Most Studio installs are not affected: `install.sh` uses a uv-managed
CPython 3.13 on Linux, the macOS 3.12 fallback uses only managed builds,
and Windows never re-forks the pool. The Docker Studio images
(`Dockerfile.studio`, `Dockerfile.studio-rocm`) install with
`UNSLOTH_PYTHON=3.12` on Ubuntu 24.04, where uv picks the system Python
3.12.3, and any Ubuntu 24.04 user passing `--python 3.12` lands there
too.

`main.py` now sets `TORCHINDUCTOR_QUIESCE_ASYNC_COMPILE_POOL=0` before
torch is imported (so spawned workers inherit it), only on affected
POSIX interpreters and only when the variable is unset. Fixed
interpreters keep the default, so idle compile workers are still
reclaimed there. The probe cache key ignores the variable, so cached
probe results stay valid.

## Evidence

The stdlib repro from pytorch#200848, run in parallel:

| CPython | Result |
|---|---|
| 3.12.3 (uv-managed and Ubuntu system) | job lost |
| 3.13.0 | job lost |
| 3.12.8 | ok |
| 3.13.1 | ok |

`uv venv --python ">=3.12,<3.13"` on Ubuntu 24.04 with no managed 3.12
picks `/usr/bin/python3.12` (3.12.3), and the new gate reports it as
affected and sets the variable.

## Tests

`studio/backend/tests/test_inductor_pool_env.py`: the version gate, an
explicit user value winning, the call happening before any torch import,
the probe cache key, Inductor reading the variable as False, and the
stdlib repro losing the job exactly when the gate says the interpreter
is affected.

## Not covered

A torch-level hang under a long-lived Studio server was not reproduced
(the race window is tens of milliseconds); the evidence is the stdlib
repro and the torch source.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant