Skip to content

janet: release GC roots for fibers, results and ExecuteFunction values - #56

Open
OvOhao wants to merge 1 commit into
cfoust:mainfrom
OvOhao:fix-janet-gc-roots
Open

OvOhao wants to merge 1 commit into
cfoust:mainfrom
OvOhao:fix-janet-gc-roots

Conversation

@OvOhao

@OvOhao OvOhao commented Oct 14, 2026

Copy link
Copy Markdown

Fixes #55.

Changes in pkg/janet:

  • continueFiber: unroot the fiber when it stops with any signal other than JANET_SIGNAL_USER5. Only USER5 fibers are resumed later (through resolveRequest), and they come back through continueFiber, so they are released then. The early returns in handleCallback unroot too; the context-cancel branch reports the error first and then queues the release with Free(), since it runs off the VM thread.
  • handleCodeResult runs in the goroutine started by runCode, not on the VM thread, so it now releases values with Free() (which unroots on the VM thread) instead of unroot(). That includes the string and unexpected-type paths, which previously did not release out at all. For UpdateEnv, the new env reuses out (already rooted on the VM thread in continueFiber) and the old env is freed. A *Table previously returned by Env() is then marked freed; cmd/docs re-reads Env() on each callback, so it is not affected.
  • ExecuteFunction: free out.Yield and the unmarshalled *Function.

Root counts after 1000 calls, measured with a debug-instrumented copy that forces a collection on the VM thread before counting:

call before after
ExecuteCall (success) roots=2014, fiberRoots=1003 roots=9, fiberRoots=0
ExecuteCall (Janet error) roots=2014, fiberRoots=1003, stringRoots=1000 roots=9, fiberRoots=0
Function.Call (key binding) roots=1017, fiberRoots=1004 roots=11, fiberRoots=0
ExecuteFunction roots=4014, fiberRoots=2003 roots=9, fiberRoots=0

go test ./pkg/janet/ passes with and without -race.

Not changed here: the v.env write in handleCodeResult still happens off the VM thread, so concurrent UpdateEnv calls still race on it (see #55).

- continueFiber: unroot the fiber once it stops with any signal other
  than USER5 (only USER5 fibers are resumed later); also unroot on the
  early returns in handleCallback, and queue the release in the
  context-cancel branch after reporting the error.
- handleCodeResult runs off the VM thread, so release values with
  Free() (which unroots on the VM thread) instead of unroot(), including
  the string and unexpected-type paths; reuse the VM-thread root on out
  for the new env and free the old env.
- ExecuteFunction: free out.Yield and the unmarshalled function.

Fixes cfoust#55
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.

Possible memory leak: Janet GC roots taken by pkg/janet are never released (fibers, evaluate results, ExecuteFunction values)

1 participant