Use RLock instead of Lock to prevent thread deadlock#1008
Merged
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
Kludex
reviewed
Jun 1, 2026
|
|
||
|
|
||
| class ThreadLock: | ||
| class ThreadRLock: |
Member
There was a problem hiding this comment.
Do we need to rename the classes tho?
Contributor
Author
There was a problem hiding this comment.
No, that was only to make it more explicit. I've updated the PR now to make the change more minimal ;-)
A thread that already holds `_optional_thread_lock` can re-enter it in nested call paths. `threading.Lock` deadlocks on a second acquire from the same thread; `threading.RLock` allows re-entrant acquisition. Ported from encode/httpcore#1003 (Zenulous), via codeberg.org/httpxyz/httpcorexyz@3058e2d. Co-Authored-By: Zenulous <zen@zenulous.com>
e4339a3 to
3735a33
Compare
mbeijen
added a commit
to mbeijen/httpx2
that referenced
this pull request
Jun 2, 2026
Kludex
approved these changes
Jun 2, 2026
Member
|
Thanks! |
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.
Summary
A thread that already holds
_optional_thread_lockon(Async)ConnectionPoolcan re-enter the lock in nested call paths.threading.Lockdeadlocks on a second acquire from the same thread;threading.RLockallows re-entrant acquisition and resolves the hang.This PR:
threading.Lock()forthreading.RLock()in the sync variant. The async variant remains a no-op.- RenamesThreadLock→ThreadRLockandAsyncThreadLock→AsyncThreadRLockinhttpcore2/_synchronization.pyto make the lock semantics explicit. The names are private (module is_synchronization), so this is not a public API change.- Updates the two import / usage sites in_async/connection_pool.pyand_sync/connection_pool.py.Ported from encode/httpcore#1003 (Zenulous), via codeberg.org/httpxyz/httpcorexyz@3058e2d.
Note: this change was prepared with AI assistance (Claude Code).