Skip to content

Guard Packer buffer protocol hooks with Cython critical sections#686

Merged
methane merged 2 commits into
mainfrom
copilot/fix-free-threading-race
Jun 3, 2026
Merged

Guard Packer buffer protocol hooks with Cython critical sections#686
methane merged 2 commits into
mainfrom
copilot/fix-free-threading-race

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jun 3, 2026

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.
@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-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.

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_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:

  1. Modify the Cython source file that defines Packer.__getbuffer__ and Packer.__releasebuffer__.
  2. Add @cython.critical_section immediately above both methods.
  3. Keep the change minimal and focused only on these two methods unless a tiny adjacent edit is required for correctness.
  4. Regenerate generated artifacts only if this repository expects checked-in generated files to be updated as part of source changes.
  5. 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.

Copilot AI changed the title [WIP] Fix free-threading race in Packer buffer methods Guard Packer buffer protocol hooks with Cython critical sections Jun 3, 2026
Copilot AI requested a review from methane June 3, 2026 07:12
@methane methane marked this pull request as ready for review June 3, 2026 07:22
@methane methane merged commit 7df7136 into main Jun 3, 2026
36 checks passed
@methane methane deleted the copilot/fix-free-threading-race branch June 3, 2026 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants