darion-yaphet opened a new pull request, #3682: URL: https://github.com/apache/brpc/pull/3682
Destroying a `KeyTable` may run a user-provided TLS destructor. The destructor can wait for another bthread or access the same pool. Previously, pool destruction deleted cached KeyTables while holding both the pool write lock and `ThreadLocal`'s internal mutex. A user destructor could therefore wait for a bthread that needs one of these locks, causing a deadlock. The cleanup now works in two phases: 1. Under the cache locks, mark the pool destroyed and detach cached KeyTables into a local list. 2. Release the locks, then destroy the detached KeyTables and run their TLS destructors. ## Summary Pool destruction currently deletes cached KeyTables while holding both the pool write lock and `ThreadLocal`'s internal mutex. TLS destructors can execute user code, which may wait for or interact with tasks that need these locks. Detach KeyTable chains while the cache locks protect the pool structure, then destroy the detached KeyTables after releasing both locks. Delete the now-empty thread-local cache container afterward. Add a regression test that attempts to acquire both locks from a TLS destructor. The test fails before the fix and verifies that both locks are available after it. ## Testing - `cmake --build build --target bthread_key_unittest -j6` - `bthread_key_unittest --gtest_filter=KeyTest.*` — 12 tests passed. - `KeyTest.pooled_destructor_runs_outside_cache_locks` repeated 30 times — passed. ## Risk and Rollback The change preserves the pool shutdown sequence while moving user destructor execution outside cache-management locks. It does not change public APIs. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
