Skip to content

fix(pyo3): don't wait for runtime workers when the last handle drops - #388

Merged
vuanhphung merged 3 commits into
mainfrom
vu-phung/pyo3-runtime-drop-gil-deadlock
Oct 8, 2026
Merged

vuanhphung merged 3 commits into
mainfrom
vu-phung/pyo3-runtime-drop-gil-deadlock

Conversation

@vuanhphung

@vuanhphung vuanhphung commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Session.close() can hang forever. After the DeleteSession RPC returns, the session's tokio runtime is dropped with the GIL held, and dropping a multi-thread runtime waits for every worker to exit. If a worker is forwarding a log record through pyo3_log at that moment, it blocks waiting for the GIL, and the two threads deadlock. The same happens whenever Python frees the last handle holding the runtime.

This wraps the runtime in KernelRuntime. Whichever explicit close() releases the last handle (session, statement or result stream) shuts the runtime down with the GIL released and waits up to 1s for the workers, so none is still logging into Python after close() returns. When Python frees the last handle without close(), Drop uses shutdown_background(), which never blocks. Pending tasks are still cancelled as before.

pyo3's finalization check is compiled out for abi3-py39, so a worker attaching while the interpreter exits could abort the process. An atexit hook makes the log bridge drop records once the interpreter starts exiting, which covers workers left running by Drop.

This is the cause of the intermittent 60-minute timeouts in the databricks-driver-test Python kernel · reyden nightly (e.g. run 37566313877). pytest-timeout never fired there because its timer thread also needs the GIL.

Testing

  • Unit tests with a worker blocked in Python::attach: dropping the runtime with the GIL held must not deadlock, and release() must not return before the worker finishes. Each test fails when its fix is mutated out. A third test covers the exit guard.
  • Live repro: connector main + kernel, through the driver-test mitmproxy, against the pecotesting Reyden warehouse, with DEBUG logging to widen the race. The baseline (80f2aee) hung in 5 of 25 close() calls; py-spy showed the main thread in the runtime-shutdown condvar wait and a worker blocked in PyGILState_Ensure. With shutdown_background() alone: 0 of 100, but over 60 closes 11 worker log records arrived after close() had returned. With the close() wait: 0 hangs and 0 post-close worker records in 60 cycles. Closing the session before its statement left 9 post-close worker records in 20 cycles before routing every close() through release(), and 0 after. At default logging with CloudFetch fault injection, neither the baseline nor the first version hung in 150 cycles.
  • cargo test, cargo clippy --all-targets -- -D warnings.

This pull request and its description were written by Isaac.

Dropping the session's tokio runtime blocks until every worker thread
exits, and the last reference is usually released with the GIL held: at
the end of Session.close(), or when Python frees a handle. A worker
forwarding a log record through pyo3_log waits for that same GIL, so the
two deadlock and the process hangs. pytest-timeout can't fire either,
since its timer thread also needs the GIL.

Wrap the runtime in KernelRuntime, whose Drop calls shutdown_background()
so releasing the last handle never waits on the workers. Pending tasks
are cancelled as before.

Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No issues identified by the review bot.

With shutdown_background() alone, close() can return while a worker is
still forwarding a log record into Python. pyo3's finalization check is
compiled out for abi3-py39, so a worker that attaches while the
interpreter exits can crash it.

When close() holds the last runtime handle, shut the runtime down with
the GIL released and wait up to 1s for the workers. Drop keeps
shutdown_background() for handles Python frees without close().

Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No issues identified by the review bot.

@vikrantpuppala vikrantpuppala left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root-cause analysis and repro here are solid, and this fixes the hang. I checked two of the claims against the dependency sources: pyo3 0.29.2 only has the Py_IsFinalizing guard under #[cfg(Py_3_13)] (internal/state.rs), so it's compiled out for abi3-py39; and tokio 1.53's Runtime::shutdown_timeout waits on the blocking pool. I left three inline comments; none of them blocks merging.

One more nit that doesn't anchor to the diff: each *Inner struct now depends on declaring the kernel object before runtime, so that kernel Drop impls which spawn CloseStatement / DeleteSession run while the runtime still exists. A one-line comment on KernelRuntime would stop someone reordering those fields.

Related, but older than this PR and out of scope: those fire-and-forget tasks are cancelled right away by the runtime shutdown. So a GC'd pyo3 session never actually sends DeleteSession, even though the kernel comments say "the runtime keeps the task alive until completion". That's probably worth a separate issue.

Comment thread pyo3/src/runtime.rs Resolved
Comment thread pyo3/src/session.rs Resolved
Comment thread pyo3/src/runtime.rs Resolved
Route every explicit close() through KernelRuntime::release(), so
whichever handle drops the last runtime reference waits for the workers
with the GIL released, including when close() fails. The wait is capped
by a named CLOSE_SHUTDOWN_TIMEOUT, since shutdown also waits on
blocking-pool work such as the OAuth refresh.

For handles Python frees without close(), Drop still doesn't wait. An
atexit hook now makes the log bridge drop records once the interpreter
starts exiting, so a lingering worker can't attach to a finalizing
interpreter (pyo3's guard for that is compiled out under abi3-py39).

Also note that handle structs must declare the kernel object before the
runtime, and add tests for release() and the exit guard.

Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No issues identified by the review bot.

@vuanhphung
vuanhphung added this pull request to the merge queue Oct 8, 2026
Merged via the queue into main with commit ab1e6a7 Oct 8, 2026
26 checks passed
@vuanhphung
vuanhphung deleted the vu-phung/pyo3-runtime-drop-gil-deadlock branch October 8, 2026 17:12

This branch was previously deployed

1 inactive deployment
azure-prod — 42965f04 Deployed Oct 8, 2026 by vuanhphung via review #902
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.

2 participants