Skip to content

[mypyc]Fix reference leak in mypyc bytes concatenation#21469

Open
colinxu2020 wants to merge 2 commits into
python:masterfrom
colinxu2020:patch-1
Open

[mypyc]Fix reference leak in mypyc bytes concatenation#21469
colinxu2020 wants to merge 2 commits into
python:masterfrom
colinxu2020:patch-1

Conversation

@colinxu2020
Copy link
Copy Markdown

@colinxu2020 colinxu2020 commented May 12, 2026

Fixes Mypyc #1192

Fix a reference leak in mypyc-generated code for bytes + bytes.

The bytes + bytes primitive for CPyBytes_Concat was marked as stealing the
left operand with steals=[True, False]. However, CPyBytes_Concat does not
steal or decref either argument; it allocates and returns a new bytes object.

Because of this mismatch, the refcount pass skipped emitting a dec_ref for
owned left operands. For code such as:

return (1).to_bytes(4, "big") + (2).to_bytes(4, "big")

the generated IR decrefed only the right operand after CPyBytes_Concat, leaving
the owned left operand alive. Chained concatenations amplified the leak because
intermediate concat results could also become leaked left operands.

This PR removes the incorrect steals=[True, False] annotation from the
bytes + bytes primitive, so both operands are treated as non-stolen and owned
operands are decrefed normally after the call.

The tests verify that the generated IR decrefs both owned operands after
CPyBytes_Concat.

@colinxu2020 colinxu2020 marked this pull request as ready for review May 12, 2026 15:31
@colinxu2020 colinxu2020 changed the title Fix reference leak in mypyc bytes concatenation [mypyc]Fix reference leak in mypyc bytes concatenation May 12, 2026
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.

mypyc leaks owned left operand for binary bytes concatenation

1 participant