Repository navigation
fix(pyo3): don't wait for runtime workers when the last handle drops - #388
Conversation
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>
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>
vikrantpuppala
left a comment
There was a problem hiding this comment.
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.
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>
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 throughpyo3_logat 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 explicitclose()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 afterclose()returns. When Python frees the last handle withoutclose(),Dropusesshutdown_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. Anatexithook makes the log bridge drop records once the interpreter starts exiting, which covers workers left running byDrop.This is the cause of the intermittent 60-minute timeouts in the databricks-driver-test Python
kernel · reydennightly (e.g. run 37566313877). pytest-timeout never fired there because its timer thread also needs the GIL.Testing
Python::attach: dropping the runtime with the GIL held must not deadlock, andrelease()must not return before the worker finishes. Each test fails when its fix is mutated out. A third test covers the exit guard.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 25close()calls; py-spy showed the main thread in the runtime-shutdown condvar wait and a worker blocked inPyGILState_Ensure. Withshutdown_background()alone: 0 of 100, but over 60 closes 11 worker log records arrived afterclose()had returned. With theclose()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 everyclose()throughrelease(), 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.