fix: release parser on Init/Final handler exceptions#258
Draft
fix: release parser on Init/Final handler exceptions#258
Conversation
When Init or Final handlers die, the expat parser was not released, creating a circular reference leak (self_sv holds a ref to the Perl hash, preventing DESTROY from ever being called). The entire C parser struct, all handler SVs, and the Perl object leaked permanently. Wrap Init and Final handler calls in eval blocks with release-on-error cleanup in both parse() and parse_start(). This ensures the parser is always properly released regardless of handler behavior. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #258 +/- ##
=======================================
Coverage 75.73% 75.73%
=======================================
Files 1 1
Lines 1092 1092
Branches 342 342
=======================================
Hits 827 827
Misses 59 59
Partials 206 206
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Ensure
$expat->releaseis always called even when Init or Final handlers die.Why
When an Init or Final handler throws an exception, the
release()call is skipped. Sinceself_sv(the C-side back-reference to the Perl hash) still holds a reference, a circular reference is created. Perl's reference counting GC never collects the object — DESTROY is never called, and the entire parser (C struct, all handler SVs, the Perl hash) leaks permanently.How
&$init($expat)in eval with release-on-error in bothparse()andparse_start()&$final($expat)in eval with release-after inparse(), capturingwantarraybefore the eval block (sincewantarraydoesn't propagate through eval)Testing
Added 7 tests to
t/parser_api.tcovering Init/Final die paths and parser reuse after handler failure. Full suite passes (683 tests).🤖 Generated with Claude Code