fix: release the options handle after parsing - #56
Open
Garbee wants to merge 2 commits into
Open
Conversation
Parser.ParseWithOptions and its UTF-16 / custom-encoding siblings Saved their *ParseOptions into mattn/go-pointer's process-global handle map but never Unref'd it, so every call made with non-nil options retained the options value -- and the progress callback it holds -- for the lifetime of the process. The input payload handle in the same functions is already released with a deferred Unref; the options handle was not. Capture the saved options pointer and defer its Unref, mirroring the input payload. The parse is synchronous and tree-sitter does not retain the payload past ts_parser_parse_with_options, so releasing it on return is safe. Applies to all four option-taking entry points: ParseWithOptions, ParseUTF16LEWithOptions, ParseUTF16BEWithOptions, and ParseCustomEncoding.
Assert that a *ParseOptions passed to ParseWithOptions becomes collectable once the call returns, by counting finalizer runs across many parses, with a nil-options control loop proving finalizers run at all. Fails on the unfixed binding (options reclaimed 0/N, pinned in go-pointer's global map) and passes once the handle is released. No grammar needed — the handle is saved before parsing.
Garbee
marked this pull request as ready for review
May 23, 2026 16:44
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.
This patch re-uses the existing pattern to release references. Freeing memory to prevent a leak. It also provides a unit test to ensure this does not regress in the future.
With the test, you can run it on the current
masterto see the failure. Then with the patch, it passes successfully.AI Disclosure: Claude was used in iterating on the patch and test. Using the test case I ran things manually to validate and confirm. However, I don't yet have a strong enough grasp of tooling with memory analysis in Go to validate the behavior using an external tool that is abstract from runtime operation.
Fixes: #55