Skip to content

Fix reference leak in is_namedtuple_class when _fields is not a tuple - #1377

Merged
Siyet merged 1 commit into
msgspec:mainfrom
shashank11yadav:fix/is-namedtuple-fields-leak
Nov 28, 2026
Merged

Siyet merged 1 commit into
msgspec:mainfrom
shashank11yadav:fix/is-namedtuple-fields-leak

Conversation

@shashank11yadav

@shashank11yadav shashank11yadav commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1376

When a tuple subclass has a _fields attribute that is not exactly a tuple (for example a list), is_namedtuple_class returned early without releasing the reference it got from PyObject_GetAttr. Each decoder built for such a class leaked one reference to _fields.

The fix adds the missing Py_DECREF(fields) before that early return in src/msgspec/_core.c. NamedTupleInfo_Convert already releases its reference on every path and is unchanged.

Testing:

  • New regression test test_namedtuple_like_fields_not_tuple_no_leak in tests/unit/test_json.py. Without the fix it fails (reference count of _fields is 14, expected 4, after 10 decoder builds). With the fix it passes.
  • Unit test suite, run after an offline reinstall of the C extension: 7468 passed, 172 skipped.
  • Lint and format: the ruff check and codespell lint run, and ruff format in diff mode, all with the pinned ruff 0.17.3 and codespell 2.4.3, pass. The repo's combined lint target (the prek hooks) was not run as such.
  • Not run: CI's ASAN and free-threaded jobs.

Changelog entry added under Unreleased.

provinzkraut
provinzkraut previously approved these changes Nov 27, 2026

@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.

Thanks!

…uple

`is_namedtuple_class` returned early without releasing the `_fields`
reference when it was not exactly a `tuple`, so every decoder built for
such a `tuple` subclass leaked one reference.
@shashank11yadav
shashank11yadav force-pushed the fix/is-namedtuple-fields-leak branch from c49f8bb to 26c5e6a Compare November 28, 2026 02:11
@Siyet
Siyet merged commit 6a995ff into msgspec:main Nov 28, 2026
23 checks passed

Siyet commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Thx for the fix.

Siyet added a commit that referenced this pull request Nov 28, 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.

Reference leak in is_namedtuple_class when _fields is not exactly a tuple

3 participants