Skip to content

fix(query): fix reference leak in captures_for_match - #471

Merged
ObserverOfTime merged 1 commit into
tree-sitter:masterfrom
A-S-Manoj:fix/captures-for-match-refleak
Jul 8, 2026
Merged

fix(query): fix reference leak in captures_for_match#471
ObserverOfTime merged 1 commit into
tree-sitter:masterfrom
A-S-Manoj:fix/captures-for-match-refleak

Conversation

@A-S-Manoj

Copy link
Copy Markdown
Contributor

Fixes #467.

nodes_for_capture_index() returns a new reference, and PyDict_SetItem() doesn't steal it — it takes its own independent reference to the same object. The local nodes variable (and captures on the error paths) was never released, so every capture evaluated through a custom query predicate leaked one node-list object permanently.

Verified the leak and the fix by comparing sys.getrefcount() on the captured list inside a custom predicate callback: 4 references before the fix, 3 after — matching the expected single-owner (the dict) refcount.

nodes_for_capture_index() returns a new reference, and PyDict_SetItem() does not steal it -- it stores its own reference. The local nodes and captures references were never decref'd, leaking one node-list object per capture on every custom predicate evaluation.
Fixes tree-sitter#467
@ObserverOfTime
ObserverOfTime merged commit 3c32483 into tree-sitter:master Jul 8, 2026
29 checks passed
@A-S-Manoj
A-S-Manoj deleted the fix/captures-for-match-refleak branch July 8, 2026 13:54
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 captures_for_match

2 participants