Avoid joining parent thread pools in forked children - #31961
Open
Taegyun Kim (taegyunkim) wants to merge 1 commit into
Open
Avoid joining parent thread pools in forked children#31961Taegyun Kim (taegyunkim) wants to merge 1 commit into
Taegyun Kim (taegyunkim) wants to merge 1 commit into
Conversation
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Author
|
@microsoft-github-policy-service agree [company="DataDog"] I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer. |
Author
|
@microsoft-github-policy-service agree company="Datadog" |
Taegyun Kim (taegyunkim)
marked this pull request as ready for review
August 10, 2026 19:21
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
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.
Description
Record the process ID that creates each thread pool. If the copied pool is
destroyed in a forked child, abandon its inherited low-level state instead of
touching synchronization objects or joining thread handles owned by the parent.
Add a Linux regression test that creates a thread pool, forks, destroys the
copied pool in the child, and verifies that the child exits normally.
This does not make inherited thread pools usable after fork.
Motivation and Context
Destroying an ONNX Runtime
InferenceSessioninherited across fork can terminatethe child with SIGSEGV in
pthread_joinbecause the stored worker handles belongto the parent process.
The issue is reproducible with the CPU Execution Provider and does not require
inference, CUDA, or a particular model. Applications should create sessions
after fork, but accidental cleanup of inherited state should not crash.
Forking a multithreaded process is inherently unsafe. CPython documents this
limitation for
os.fork()and emits aDeprecationWarningwhen it detectsmultiple threads. Applications should avoid carrying a live ONNX Runtime
session across a fork boundary.
At the same time, prefork process models are common in Python web deployments.
For example, Gunicorn forks worker processes from a master process. If
application preloading or module-level initialization creates an
InferenceSessionin the master, ONNX Runtime's native worker threads arecreated before Gunicorn forks. The resulting session and thread handles are
then inherited by each worker.
The application-level fix is to create each
InferenceSessionafter the finalfork, such as during worker initialization. However, library-side defensive
handling is still useful. Python object finalization can destroy an accidentally
inherited session during worker shutdown, and that cleanup should not attempt
to synchronize with or join threads owned by the parent process.
This change does not make inherited sessions fork-safe or endorse using them in
a child. It only recognizes that the copied thread pool cannot be safely
destroyed there and prevents that unsupported usage from becoming a process
crash during cleanup.
References
os.fork()warningReproducer
Run:
Output with ONNX Runtime 1.28.0:
Confirmed environments:
The same failure was reproduced with ONNX Runtime 1.26.0, 1.27.0, and 1.28.0. No inference, CUDA provider,
onnxpackage, or external model file is required.