Skip to content

Fix potential memory leak in Encoder_init#1040

Open
sobolevn wants to merge 1 commit into
msgspec:mainfrom
sobolevn:fix-leak-in-Encoder_init
Open

Fix potential memory leak in Encoder_init#1040
sobolevn wants to merge 1 commit into
msgspec:mainfrom
sobolevn:fix-leak-in-Encoder_init

Conversation

@sobolevn

@sobolevn sobolevn commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

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!

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