Skip to content

Fix potential memory leak in Encoder_init - #1040

Merged
provinzkraut merged 2 commits into
msgspec:mainfrom
sobolevn:fix-leak-in-Encoder_init
Jun 10, 2026
Merged

provinzkraut merged 2 commits into
msgspec:mainfrom
sobolevn:fix-leak-in-Encoder_init

Conversation

@sobolevn

@sobolevn sobolevn commented Jun 7, 2026 •

Copy link
Copy Markdown
Member

There was a potential memory leak in Encoder_init.

Why?

  1. Some valid callable is passed as enc_hook, it has +1 rc in Py_INCREF(enc_hook);
  2. Then, invalid order is checked, we return -1 from Encoder_init, but does not Py_DECREF(enc_hook);

We can just swap the check order. First check things that do not incref, lastly incref.

Repro:

>>> from msgspec.json import Encoder
>>> import sys
>>> def test(*args, **kwargs): ...
... 
>>> sys.getrefcount(test)
2
>>> Encoder(enc_hook=test, order='wrong')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: `order` must be one of `{None, 'deterministic', 'sorted'}`, got 'wrong'
>>> sys.getrefcount(test)
3

@provinzkraut provinzkraut left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks!

@provinzkraut
provinzkraut enabled auto-merge June 10, 2026 15:59
@provinzkraut
provinzkraut added this pull request to the merge queue Jun 10, 2026
Merged via the queue into msgspec:main with commit 4152dc1 Jun 10, 2026
9 checks passed
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