Guard `Packer` buffer protocol hooks with Cython critical sections by Copilot · Pull Request #686 · msgpack/msgpack-python
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.pyxin thePackerbuffer-protocol path.
- Update only
-
Change
- Add
@cython.critical_sectiondirectly above:Packer.__getbuffer__(self, Py_buffer *buffer, int flags)Packer.__releasebuffer__(self, Py_buffer *buffer)
- Add
-
Consistency impact
- Aligns buffer acquisition/release with existing
Packermethod-level synchronization strategy, so exported-buffer bookkeeping (self.exports) is protected under the same lock discipline.
- Aligns buffer acquisition/release with existing
@cython.critical_section def __getbuffer__(self, Py_buffer *buffer, int flags): PyBuffer_FillInfo(buffer, self, self.pk.buf, self.pk.length, 1, flags) self.exports += 1 @cython.critical_section def __releasebuffer__(self, Py_buffer *buffer): self.exports -= 1
Original prompt
Create a pull request in repository
msgpack/msgpack-pythonthat fixes the free-threading race described in issue #683 by adding@cython.critical_sectiontoPacker.__getbuffer__andPacker.__releasebuffer__in_packer.pyx.Context:
- The repository is
msgpack/msgpack-python.- Issue: A (tentative) TODO list? (Generated from Claude Code plugins) #683, titled
A (tentative) TODO list? (Generated from Claude Code plugins).- The specific requested change is limited to adding
@cython.critical_sectionto the two buffer-protocol methods.- The issue text says these methods currently miss the decorator, which can allow concurrent
memoryview(packer)andpack()operations to race under free-threaded Python.Requirements:
- Modify the Cython source file that defines
Packer.__getbuffer__andPacker.__releasebuffer__.- Add
@cython.critical_sectionimmediately 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.
- Open a pull request with a concise description referencing issue A (tentative) TODO list? (Generated from Claude Code plugins) #683 and explaining that this protects buffer protocol access with the same per-object critical section used by other public methods.
Suggested validation:
- Run the relevant tests if feasible, especially any packing/free-threading related tests.
- If there is an established command to regenerate Cython output, use it if needed by repo conventions.
The following is the prior conversation context from the user's chat exploration (may be truncated):
User:
Packer.__getbuffer__と__releasebuffer__に@cython.critical_sectionを追加するPRを作って。Assistant: PR作成の確認待ちです。GitHub 側の承認が完了したら、その変更で続けて進めます。
This pull request was created from Copilot chat.