Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix reference leak when decoding msgpack Ext payloads - #1109

Merged
Siyet merged 1 commit into
mainfrom
1108-mpack-ext-leak
Jul 2, 2026
Merged

Fix reference leak when decoding msgpack Ext payloads#1109
Siyet merged 1 commit into
mainfrom
1108-mpack-ext-leak

Conversation

@Siyet

@Siyet Siyet commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Fixes #1108.

Ext_New doesn't steal the reference to data (it does its own Py_INCREF, matching the borrow contract expected by the Ext constructor). Both decode branches in mpack_decode_ext (typed Ext decode, and Any decode without an ext_hook) passed a fresh reference from PyBytes_FromStringAndSize and never released it, leaking one bytes object (the ext payload) per decoded Ext. The datetime (code -1) and ext_hook branches were not affected.

The fix releases the caller's reference after constructing the Ext, same pattern as the Raw.copy() leak fix (#709). Verified empirically: RSS delta over 200k decodes of a 1 KiB payload went from ~205 MiB to 0 on both affected branches; the new test fails before the fix (sys.getrefcount 3 vs 2) and passes after.

Thanks @K-ANOY for the precise report, the ownership analysis there was spot on.

@codspeed-hq

codspeed-hq Bot commented Jul 2, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 139 untouched benchmarks
⏩ 135 skipped benchmarks1


Comparing 1108-mpack-ext-leak (61c5075) with main (52ae4f1)

Open in CodSpeed

Footnotes

  1. 135 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@Siyet
Siyet added this pull request to the merge queue Jul 2, 2026
Merged via the queue into main with commit 4440c96 Jul 2, 2026
27 checks passed
@Siyet
Siyet deleted the 1108-mpack-ext-leak branch July 2, 2026 12:12
pull Bot pushed a commit to Future-Outlier/msgspec that referenced this pull request Jul 3, 2026
Batches the changelog entries accumulated since the 0.21.1 release,
following the usual pre-release batching practice (like msgspec#1020). Covers
merged user-facing changes through msgspec#1109/msgspec#978/msgspec#1105; internal
CI/benchmarking/test-infra changes are intentionally omitted. Also
records the move to the msgspec GitHub organization.

---------

Co-authored-by: Siyet <[email protected]>
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.

Reference leak in mpack_decode_ext() for MessagePack Ext payloads

1 participant