Skip to content

Release encode_lines iterators - #1284

Merged
sobolevn merged 2 commits into
msgspec:mainfrom
AryanRG:fix/encode-lines-iterator-lifetime
Oct 18, 2026
Merged

sobolevn merged 2 commits into
msgspec:mainfrom
AryanRG:fix/encode-lines-iterator-lifetime

Conversation

@AryanRG

@AryanRG AryanRG commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Release the owned iterator reference in Encoder.encode_lines on both success and error paths. The success-path Py_DECREF stays inside the iterator branch; error cleanup uses Py_XDECREF.

Validation on macOS arm64 / CPython 3.14.8: 6,536 unit tests passed (143 skipped). Ruff, formatting, codespell and diff checks passed. The patch also passed 6,554 unit tests against current upstream.

Local sanitizers were not run; hosted sanitizer CI still needs maintainer approval.

Comment thread tests/unit/test_json.py Outdated
Comment on lines +369 to +371
gc.collect()
assert len(refs) == 1
assert refs[0]() is None

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.

@sobolevn I know you don't like manually calling gc things, but I feel like this is fine?

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.

I don't think that we should have any tests for leaks, we should better use correct tooling for this: memory sanitizers, leak detectors for the whole test suite, etc :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks @sobolevn, done. I removed the GC-based test and moved the success-path Py_DECREF into the iterator branch, keeping the error cleanup. Rebuilt the extension and all 6,536 local unit tests pass.

@sobolevn sobolevn 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!

Comment thread src/msgspec/_core.c Resolved
Comment thread src/msgspec/_core.c Resolved
Comment thread tests/unit/test_json.py Resolved
Siyet
Siyet previously approved these changes Oct 18, 2026
@sobolevn
sobolevn added this pull request to the merge queue Oct 18, 2026
Merged via the queue into msgspec:main with commit 454153d Oct 18, 2026
23 of 24 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.

4 participants