Under free-threaded Python, memoryview(packer) can race with concurrent pack() calls because Packer.__getbuffer__ / Packer.__releasebuffer__ were not using the per-object critical section used by other public Packer methods. This change closes that gap by synchronizing both buffer-protocol hooks.
Scope
Update only msgpack/_packer.pyx in the Packer buffer-protocol path.
Change
Add @cython.critical_section directly above:
Packer.__getbuffer__(self, Py_buffer *buffer, int flags)
Packer.__releasebuffer__(self, Py_buffer *buffer)
Consistency impact
Aligns buffer acquisition/release with existing Packer method-level synchronization strategy, so exported-buffer bookkeeping (self.exports) is protected under the same lock discipline.
Create a pull request in repository msgpack/msgpack-python that fixes the free-threading race described in issue #683 by adding @cython.critical_section to Packer.__getbuffer__ and Packer.__releasebuffer__ in _packer.pyx.
The specific requested change is limited to adding @cython.critical_section to the two buffer-protocol methods.
The issue text says these methods currently miss the decorator, which can allow concurrent memoryview(packer) and pack() operations to race under free-threaded Python.
Requirements:
Modify the Cython source file that defines Packer.__getbuffer__ and Packer.__releasebuffer__.
Add @cython.critical_section immediately above both methods.
Keep the change minimal and focused only on these two methods unless a tiny adjacent edit is required for correctness.
Regenerate generated artifacts only if this repository expects checked-in generated files to be updated as part of source changes.
CopilotAI
changed the title
[WIP] Fix free-threading race in Packer buffer methods
Guard Packer buffer protocol hooks with Cython critical sections
Jun 3, 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
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.
Under free-threaded Python, memoryview(packer) can race with concurrent pack() calls because Packer.__getbuffer__ / Packer.__releasebuffer__ were not using the per-object critical section used by other public Packer methods. This change closes that gap by synchronizing both buffer-protocol hooks.
Scope
Change
Consistency impact
This pull request was created from Copilot chat.