Repository navigation
Keep a shared thread pool alive when the thread count is reset - #24770
Open
shoumikhin wants to merge 1 commit into
Open
shoumikhin wants to merge 1 commit into
shoumikhin wants to merge 1 commit into
Conversation
`_unsafe_reset_threadpool()` destroyed the current pthreadpool and created a new one. Delegates such as XNNPACK get that pthreadpool from `get_pthreadpool()` when a method is loaded and keep using it, so after a reset the loaded method ran on a freed pool and crashed or hung. From Python the reset is the only way to set the thread count, so calling it after `load_method` was enough to hit this. Now a pthreadpool that `get_pthreadpool()` has handed out stays alive with its size when a reset replaces it. XNNPACK methods loaded before the reset keep running on it, and methods loaded after it use the new pool. A reset back to a kept size reuses that pool, so at most one pool per size is kept, and `_unsafe_destroy_threadpool()` frees the kept pools too. A pool that was never handed out is destroyed as before, so a reset before loading costs nothing extra. Resizing a pool in place is not an option, because XNNPACK sizes per-thread buffers by the thread count when it plans a method. Fixes pytorch#24765
shoumikhin
requested review from
JacobSzwejbka,
JakeStevens and
larryliu0820
as code owners
November 7, 2026 05:16
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/24770
Note: Links to docs will display an error until the docs builds have been completed. ⏳ No Failures, 42 PendingAs of commit ce80ef8 with merge base 75af78c ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Contributor
Author
|
@claude /pr-review |
This branch has not been deployed
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.

What is wrong today
From Python,
_unsafe_reset_threadpool(n)is the only way to set the CPU thread count. If it runs after a method that uses XNNPACK is loaded, the next runs can crash or hang:On Linux x86_64 with an 8-thread default pool, a reset to 16 after loading crashed in 10 of 12 runs, and a reset to 4 also in 10 of 12. The Android bindings reset the pool each time a
Moduleis created, so a secondModulecan hit it too.Why it happens
The reset destroys the current pthreadpool and creates a new one. But XNNPACK got that pthreadpool from
get_pthreadpool()when the method was loaded, and it keeps using it. So the loaded method runs on a freed pool.Resizing the pool in place is not safe either. XNNPACK sizes its per-thread buffers by the thread count when it plans a method, and it does not plan again when the count changes.
What this change does
A reset no longer destroys a pthreadpool that
get_pthreadpool()has handed out. It stays alive with its size, next to the new one:_unsafe_destroy_threadpool()frees kept pools too.The header and the Python docstring now explain this.
Tests
ThreadPoolTest.ResetKeepsHandedOutPthreadpoolUsable: after a reset, the held pool still runs work on 2 threads, and a reset back to its size reuses it. It fails without the fix and passes with it.extension_threadpool_test, the native CPU engine tests, the Python tests for the pybindings and XNNPACK, and the LLM custom op tests that call the reset, on Linux x86_64. Tests that failed there fail on main too.Fixes #24765