Raise ProgrammingError immediately on concurrent connection use instead of blocking#781
Merged
Merged
Conversation
Copilot
AI
changed the title
[WIP] Add lightweight lock on connection for thread safety
Guard Jun 8, 2026
_mysql.connection with a per-connection lock on non-free-threaded CPython
Copilot
AI
changed the title
Guard
Unify connection locking on Jun 9, 2026
_mysql.connection with a per-connection lock on non-free-threaded CPythonPyThread_type_lock across GIL and free-thread builds
Copilot
AI
changed the title
Unify connection locking on
Raise ProgrammingError immediately on concurrent connection use instead of blocking
Jun 9, 2026
PyThread_type_lock across GIL and free-thread builds
methane
reviewed
Jun 9, 2026
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.
Using the same
Connectionobject from multiple threads simultaneously is unsupported and leads to undefined behavior. Previously, the C-level lock usedWAIT_LOCK, which silently serialized concurrent calls rather than surfacing the bug to callers. This change switches toNOWAIT_LOCKso that any second thread that attempts to use a connection already locked by another thread receives aProgrammingErrorimmediately.C extension (
_mysql.c)PyThread_type_lock(done in prior commit); thePyCriticalSectionbranch is gone._mysql_ConnectionObject_Lock→int: usesNOWAIT_LOCK; setsProgrammingErrorand returns-1on contention._mysql_ConnectionObject_LockWait(new): blockingWAIT_LOCKvariant used exclusively in_mysql_ResultObject_dealloc, wheremysql_free_result()must be called regardless.BEGIN_CONNECTION_OPERATION: propagates lock failure viareturn NULLbefore checkingopen.BEGIN_CONNECTION_LOCKcall sites updated to check the return value and propagate errors correctly (handles bothPyObject *andintreturn contexts).Tests (
test_connection.py)Replaced the two "operations are serialized" tests with tests that assert concurrent access raises
ProgrammingErrorimmediately:Both
Connectionmethod contention andResult.fetch_rowcontention are covered.