Fix memory leak in Raw.copy(). - #709
Merged
Merged
Conversation
tjgq
marked this pull request as ready for review
July 12, 2024 19:15
Contributor
Author
|
@jcrist could you please take a look? |
Raw_New borrows a reference to its PyBytesObject argument, so Raw_Copy must still dispose of it. The following is a minimal repro for this issue: import msgspec DECODER = msgspec.msgpack.Decoder(type=msgspec.Raw) PAYLOAD = b"\xda\xff\xff" + (65535 * b"\0") # array of 65535 zeros for _ in range(1000000): raw = DECODER.decode(PAYLOAD) raw.copy()
Member
|
Thanks! |
pull Bot
pushed a commit
to Future-Outlier/msgspec
that referenced
this pull request
Jul 2, 2026
Fixes msgspec#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 (msgspec#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. Co-authored-by: Siyet <Siyet@users.noreply.github.com>
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.
Raw_New borrows a reference to its PyBytesObject argument, so Raw_Copy must still dispose of it.
The following is a minimal repro for this issue: