Guard _mysql.connection with a per-connection lock on non-free-threaded CPython#781
Draft
Copilot wants to merge 6 commits into
Draft
Guard _mysql.connection with a per-connection lock on non-free-threaded CPython#781Copilot wants to merge 6 commits into
_mysql.connection with a per-connection lock on non-free-threaded CPython#781Copilot wants to merge 6 commits into
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
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.
Concurrent use of the same
_mysql.connectionobject was still possible on non-free-threaded CPython when_mysql.cdropped the GIL around libmysql calls. This change adds a lightweight connection-local guard for that build mode and hasResultreuse the same guard so connection/result state stays serialized without introducing a second lock.Build-mode split
Py_GIL_DISABLEDas the compile-time switch.PyThread_type_lockon_mysql_ConnectionObject.Connection-level serialization
Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADSregions so libmysql calls remain serialized even while the GIL is released.Result reuses connection guard
Resultinitialization, fetch/discard/seek helpers, metadata access, and deallocation now synchronize through the owning connection.MYSQL*share one guard.Focused regression coverage
thread_id()thread_id()Example of the new guard shape: