Skip to content

fix(interp): release the arguments a rejected finalizer call encodes - #344

Open
siyul-park wants to merge 4 commits into
mainfrom
fix/invoke-finalizer-args
Open

siyul-park wants to merge 4 commits into
mainfrom
fix/invoke-finalizer-args

Conversation

@siyul-park

@siyul-park siyul-park commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

A Go func bound to a VM function encodes its arguments before invoke copies them to the operand stack. The finalizer guard merged in #342 rejects that call before the copy, and the error path returned without releasing the refs Encoder.retain and Encoder.alloc published, so every callback a Close made on a string or ref argument left one more object alive. The other pre-copy rejections (ErrInterpreterBusy, ErrFrameOverflow, ErrTypeMismatch, ErrStackOverflow) leaked the same way.

  • interp/interp.go: invoke now takes params. Its refusals are decided in one switch, and the single refusal branch releases the arguments, so a refused call returns the heap as it found it and a started call releases the arguments with the stack — each reference is released exactly once on every path. The callee reference is resolved before the arguments reach the stack, so nothing fails once the stack holds them; that ordering also removes the previous Alloc-failure path, which had copied the arguments first and then abandoned them. Everything stays inside invoke: the change adds no helper, no state flag, and no transfer hook on Encoder (a conversion's result already owns the refs it published, so once the argument boxes are in params the Go caller owns them and hands them to invoke).
  • interp/interp_test.go: a regression test calls a Go func bound to a VM function from a Close and asserts the rejected call's string argument leaves no live heap value behind. It fails on main with one leaked count per call and passes here.

Related Issues

Follow-up to the CodeRabbit review comment on #342 at interp/interp.go:818-819 (release the encoded arguments when the finalizer guard rejects the callback).

Additional Information

  • go test ./... and the benchmarks module pass on the pinned Go 1.26.2, and ./interp ./types pass on Go 1.27.4.
  • go vet ./..., gofmt -l ., go run ./internal/cmd/codegen -check, GOOS=linux GOARCH=arm64 go build ./..., GOARCH=arm64 go test -exec=true ./..., the check-inline gate, and git diff --check are clean.
  • -race needs cgo, which the environment this was prepared in does not have, so CI is the first race run.
  • invoke has a single caller (Registry.unmarshalFunc), so no other caller's ownership changes.

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a6877560-c4f0-4f03-aaf9-8b10ad87dd39

No actionable comments were generated in the recent review.

Walkthrough

The VM function adapter transfers boxed argument ownership before invocation. The interpreter releases arguments when invocation returns before the operand stack takes ownership. A regression test checks heap-reference cleanup when a finalizer call is rejected.

Changes: Argument ownership

Layer / File(s) Summary
Argument transfer and cleanup
interp/encode.go, interp/codec.go, interp/interp.go, interp/interp_test.go
Encoder.transfer clears the encoder’s owned-reference list without releasing its references. The adapter transfers boxed arguments before invocation. invoke releases parameters if execution returns before the operand stack takes ownership, and the added regression test checks cleanup after a rejected finalizer call.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: Minimal · previous review eca55

This change fixes a leak of heap references when a finalizer call into a VM function is rejected. No concrete merge-blocking risk was found, and a regression test covers the rejected-call case.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the fix and its scope: releasing the encoded arguments when a rejected finalizer call fails. The changeset matches this. The summaries show ownership changes in invoke, Encoder.transfe…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description follows the repository template: it has Changes Made, Related Issues, and Additional Information sections. It explains the leak (refs from Encoder.retain and Encoder.alloc are not rele…
Full details: Title check

Explanation

The title names the fix and its scope: releasing the encoded arguments when a rejected finalizer call fails. The changeset matches this. The summaries show ownership changes in invoke, Encoder.transfer, and codec.go, plus a regression test for a rejected finalizer-bound call. The title is specific and follows the conventional-commit style. It does not cover the broader pre-copy rejection paths, but that detail is not required.

Full details: Description check

Explanation

The description follows the repository template: it has Changes Made, Related Issues, and Additional Information sections. It explains the leak (refs from Encoder.retain and Encoder.alloc are not released on pre-copy rejections of invoke), lists the changed files in interp/interp.go and interp/interp_test.go, links the follow-up to the CodeRabbit review comment on #342, and reports the test and lint results. The description is specific and matches the raw summary. One gap: the description says Encoder.transfer is not added, while the raw summary lists encode.go with a new Encoder.transfer method. The description omits interp/codec.go and interp/encode.go from its file list, so the description and the file-level summary do not fully agree.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3960f4b9-e376-4f97-a17a-a5c7dda62b1b
Commits

Reviewing files that changed from the base of the PR and between 87c13d1 and eca5567.

Files selected for processing: 4

  • interp/codec.go
  • interp/encode.go
  • interp/interp.go
  • interp/interp_test.go

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@siyul-park
siyul-park force-pushed the fix/invoke-finalizer-args branch from e222fff to ec8e472 Compare November 16, 2026 15:46
siyul-park and others added 3 commits November 17, 2026 01:02
A Go func bound to a VM function encodes its arguments before invoke copies
them to the stack. The finalizer guard merged in #342 rejects the call before
that copy, and the error path left every ref Encoder.retain and Encoder.alloc
published in the heap, so repeated callbacks from a Close grew the heap. The
other pre-copy rejections leaked the same way.

invoke now takes params: its refusals share one point that releases them, so a
refused call returns the heap as it found it and a started call releases the
arguments with the stack. The callee reference is resolved before the arguments
reach the stack, so nothing fails once the stack holds them.

Refs #342
@siyul-park
siyul-park force-pushed the fix/invoke-finalizer-args branch from 130c910 to 1f69d9b Compare November 16, 2026 16:03
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.

1 participant