Repository navigation
Conversation
…ools empty_cache() holds instance_mutex_ exclusively while it runs process_events() on each released private pool. When a block's last event has completed, process_events() finds the block's pool with pool_from_block(), which locks instance_mutex_ shared on the same thread. That is undefined behaviour. With libstdc++ on glibc it throws "Resource deadlock avoided", and other implementations may hang. The block is already off the event queue, so it leaks along with its pool. Graph capture does not hit this, because blocks allocated during capture never queue events. It needs a private pool used outside capture through begin_allocate_to_pool(). process_events() already has the pool, and pool.events_ only holds that pool's blocks, since free() queues events on the block's own pool and process_events() requeues them there. So use it and drop the lookup. Storing a pool pointer in every block would also avoid the lock, and would take instance_mutex_ off free() for private-pool blocks, but nothing needs that yet. The same loop read blocks_.empty() without blocks_mutex_. When pinned_max_cached_size_mb is set, free() destroys a block larger than it instead of caching it. The default is unlimited. That free() removes the block from blocks_ and keeps using the pool until it releases blocks_mutex_, so empty_cache() could erase the pool in between. The check now takes blocks_mutex_. maybe_cache_block() now releases blocks_mutex_ after the free-list mutex by construction, since std::scoped_lock does not guarantee an unlock order. Test Plan: ```bash python -m pip install -e . -v --no-build-isolation build/bin/cuda_caching_host_allocator_test --gtest_filter='CachingHostAllocatorTest.*' python -m pytest test/test_cuda.py \ -k "CachingHostAllocator or host_memory or pinned or pin_memory or host_emptyCache" ``` All 8 gtests pass, and the pytest set gives 41 passed and 3 skipped, all disabled upstream. With the header reverted to 2a20f3e, the new empty_cache_released_pool_with_completed_event test fails: ``` Expected: allocator->empty_cache() doesn't throw an exception. Actual: it throws std::system_error with description "Resource deadlock avoided". ``` No test here covers the blocks_mutex_ change, because it needs a concurrent free() in a narrow window. In a TSAN build of a mock-backend harness, which is not part of this PR, the race showed up before the change and not after. CI does not run cuda_caching_host_allocator_test, so the gtests were run locally. This change was prepared with assistance from Claude, an AI assistant. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/199145
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (1 Unrelated Failure)As of commit 83a0a03 with merge base 2a20f3e ( FLAKY - The following job failed but was likely due to flakiness present on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@pytorchbot label "topic: not user facing" |
|
@claude review this PR, pay attention to correctness under multithreading scenarios, and no additional locks on fast path |
|
Claude finished @ngimel's task in 2m 45s —— View job The fix looks correct under concurrency and adds no locks on the allocate()/free() fast path. The only thing I'd raise is a pre-existing pool-lifetime gap that the planned host mempool could make easier to hit. PR Review: #199145SummaryThe PR does two things. It stops I checked these multithreading points and found no new problems:
Thread Safety
Testing
RecommendationApprove The deadlock fix and the |
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
…ools (pytorch#199145) I also found this while prototyping the host mempool: when a private pool is used outside graph capture, empty_cache() takes the same lock twice on one thread and fails with "Resource deadlock avoided". The host mempool needs to use private pools outside capture, so it would run into this. The new test reproduces the failure on main and passes with the fix. process_events() already knows which pool it is processing, so it can use that pool directly instead of looking it up by id; that lookup is what took the lock a second time. The same loop had another problem: it checked whether a pool was empty without holding blocks_mutex_. With pinned_max_cached_size_mb set, empty_cache() could delete a pool that another thread was still using, so the check now holds that lock as well. The second problem has no dedicated test, because it needs pinned_max_cached_size_mb to be set and another thread to free the pool's last block within a very narrow window, which is hard to reproduce reliably in a test. The technical summary below was drafted with Claude, and I checked it against the code. <details> <summary>Technical details and test plan (drafted with Claude)</summary> > empty_cache() holds instance_mutex_ exclusively while it runs > process_events() on each released private pool. When a block's last event > has completed, process_events() finds the block's pool with > pool_from_block(), which locks instance_mutex_ shared on the same thread. > That is undefined behaviour. With libstdc++ on glibc it throws "Resource > deadlock avoided", and other implementations may hang. The block is already > off the event queue, so it leaks along with its pool. Graph capture does not > hit this, because blocks allocated during capture never queue events. It > needs a private pool used outside capture through begin_allocate_to_pool(). > > process_events() already has the pool, and pool.events_ only holds that > pool's blocks, since free() queues events on the block's own pool and > process_events() requeues them there. So use it and drop the lookup. Storing > a pool pointer in every block would also avoid the lock, and would take > instance_mutex_ off free() for private-pool blocks, but nothing needs that > yet. > > The same loop read blocks_.empty() without blocks_mutex_. When > pinned_max_cached_size_mb is set, free() destroys a block larger than it > instead of caching it. The default is unlimited. That free() removes the > block from blocks_ and keeps using the pool until it releases blocks_mutex_, > so empty_cache() could erase the pool in between. The check now takes > blocks_mutex_. maybe_cache_block() now releases blocks_mutex_ after the > free-list mutex by construction, since std::scoped_lock does not guarantee > an unlock order. > > Test Plan: > > ```bash > python -m pip install -e . -v --no-build-isolation > build/bin/cuda_caching_host_allocator_test --gtest_filter='CachingHostAllocatorTest.*' > python -m pytest test/test_cuda.py \ > -k "CachingHostAllocator or host_memory or pinned or pin_memory or host_emptyCache" > ``` > > All 8 gtests pass, and the pytest set gives 41 passed and 3 skipped, all > disabled upstream. With the header reverted to 2a20f3e, the > new empty_cache_released_pool_with_completed_event test fails: > > ``` > Expected: allocator->empty_cache() doesn't throw an exception. > Actual: it throws std::system_error with description "Resource deadlock avoided". > ``` > > No test here covers the blocks_mutex_ change, because it needs a concurrent > free() in a narrow window. In a TSAN build of a mock-backend harness, which > is not part of this PR, the race showed up before the change and not after. > CI does not run cuda_caching_host_allocator_test, so the gtests were run > locally. > > pytorch#199144 also changes the empty_cache() loop, so whichever of the two lands second > needs a small rebase. > > This change was prepared with assistance from Claude, an AI assistant. </details> 🤖 Generated with [Claude Code](https://claude.com/claude-code) Pull Request resolved: pytorch#199145 Approved by: https://github.com/ngimel Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
I also found this while prototyping the host mempool: when a private pool is used outside graph capture, empty_cache() takes the same lock twice on one thread and fails with "Resource deadlock avoided". The host mempool needs to use private pools outside capture, so it would run into this. The new test reproduces the failure on main and passes with the fix.
process_events() already knows which pool it is processing, so it can use that pool directly instead of looking it up by id; that lookup is what took the lock a second time. The same loop had another problem: it checked whether a pool was empty without holding blocks_mutex_. With pinned_max_cached_size_mb set, empty_cache() could delete a pool that another thread was still using, so the check now holds that lock as well. The second problem has no dedicated test, because it needs pinned_max_cached_size_mb to be set and another thread to free the pool's last block within a very narrow window, which is hard to reproduce reliably in a test.
The technical summary below was drafted with Claude, and I checked it against the code.
Technical details and test plan (drafted with Claude)
🤖 Generated with Claude Code