Skip to content

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

Open

Description

@OvOhao

Description

pkg/janet (commit a08b5cb) roots several Janet values with janet_gcroot but never releases them, so they stay alive for the life of the VM. Janet's contract (janet.c:16082) is that every janet_gcroot needs a matching janet_gcunroot. There are three places, all in how the Go wrapper takes and releases roots.

A. Every fiber stays rooted. createFiber roots the new fiber (fiber.go:173 v.value(C.janet_wrap_fiber(fiber)) -> value.go:30 janet_gcroot). Nothing unroots it afterwards: continueFiber (fiber.go:231-279) returns on OK, YIELD and ERROR without releasing it, and neither do the early returns in handleCallback (:190, :207, :218). The only unroot in the package is Value.unroot (value.go:49), and it is never called for a fiber. Since every entry point creates a fiber (ExecuteCall, Execute, ExecuteFile, Function.Call / CallResult, and two per ExecuteFunction), each call keeps one fiber, its stack and everything reachable from it. That includes key bindings (pkg/cy/client.go:186), hooks (client.go:139) and dynamic layout properties (pkg/layout/prop/module.go:141).

B. handleCodeResult releases roots on the wrong thread. The VM goroutine is locked to its OS thread (module.go:78), and Janet's VM state is thread-local (janet_vm is JANET_THREAD_LOCAL, janet.c:30100). runCode handles the result in a separate goroutine (exec.go:173), and handleCodeResult (exec.go:82-114) calls unroot() / v.value(...) there. On that thread janet_gcunroot searches that OS thread's own root table, which does not contain the VM's roots, and returns 0, so the root continueFiber took on the result (fiber.go:247) is never released. In addition, the string (error) result path (exec.go:86-94) returns without any unroot. The Value.Free path, which queues an unlockRequest to the VM thread (module.go:113-114), would release them correctly.

The same goroutine also writes v.env (exec.go:101-107) while the VM thread reads it in runCode (:122-123). With concurrent UpdateEnv calls the race detector reports this as a data race. That is a separate concurrency issue, mentioned here because it touches the same lines.

C. ExecuteFunction never frees its values. exec.go:326-357 unmarshals the function from out.Yield and returns without calling Free() on either out.Yield or the *Function. That is two roots per call, on every hook and copy-to-register event. cmd/cy/rpc.go:178 shows the intended pattern: defer result.Yield.Free().

Reproduction

I added debug-only instrumentation to a copy of pkg/janet: a VM-loop request that forces janet_collect on the VM thread (installing a temporary root fiber, since janet_collect dereferences janet_vm.root_fiber) and then counts roots by Janet type, plus a counter on janet_gcunroot returning 0 inside Value.unroot. The wrapper's root/unroot logic is unchanged. 1000 calls of each kind (macOS arm64, Go 1.27):

pure_ok   (ExecuteCall, success)  roots=2014 fiberRoots=1003 tableRoots=1005 fiberRootStackKB=3009
janet_err (ExecuteCall, (error))  roots=2014 fiberRoots=1003 stringRoots=1000 fiberRootStackKB=6102
keybind   (Function.Call)         roots=1017 fiberRoots=1004 fiberRootStackKB=1012
fncall_ok (ExecuteFunction)       roots=4014 fiberRoots=2003 fnRoots=2006

The counts grow by exactly one fiber per call and do not go down after forced collections. In the pure_ok run, the counter on janet_gcunroot returning 0 in Value.unroot went up once per call; handleCodeResult's out.unroot() is the only unroot on that path. With the changes below, after 1000 calls of each kind:

pure_ok   roots=9  fiberRoots=0 stringRoots=0 tableRoots=3
janet_err roots=9  fiberRoots=0 stringRoots=0 tableRoots=3
keybind   roots=11 fiberRoots=0 stringRoots=0 tableRoots=3
fncall_ok roots=9  fiberRoots=0 fnRoots=6

and go test ./pkg/janet/ (also with -race) passes.

Suggested fix

  • continueFiber: unroot the fiber when the signal is anything other than JANET_SIGNAL_USER5 (a USER5 fiber is resumed later through resolveRequest and comes back through continueFiber). Also unroot it on the early returns in handleCallback, and in the context-cancel branch send params.Error(...) before queueing the release.
  • handleCodeResult: use Free() (which runs on the VM thread) instead of unroot(), also on the string and unexpected-type paths, and free the old v.env. The new env can reuse out, which is already rooted on the VM thread, instead of rooting it again from the goroutine. Freeing the old env means a *Table previously returned by Env() becomes freed (JSON/Raw/Bytes/Unmarshal return ErrFreed); the only caller, cmd/docs (main.go:99), re-reads Env() on each callback, so it is not affected.
  • ExecuteFunction: defer out.Yield.Free() and defer fun.Free().

Moving the v.env swap onto the VM thread (or guarding it with the VM mutex) would also remove the data race in B. I have a patch for A-C and the harness, and can open a PR if that is useful.

If you could credit me as a reporter for my contributions to security advisory I will be thankful.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions