runtime: SetFinalizer does nothing with -gc=boehm, so syscall/js values leak
Summary
runtime.SetFinalizer is an empty function when the Boehm collector is used.
syscall/js depends on finalizers to release JavaScript values. With
-gc=boehm no JavaScript value is ever released, and the page grows until the
browser runs out of memory.
The block collectors got finalizers in #5521 and #5545. The Boehm collector did not.
How to reproduce
main.go
//go:build js
package main
import (
"runtime"
"syscall/js"
"time"
)
func main() {
array := js.Global().Get("Uint8Array")
for i := 0; i < 2000; i++ {
buffer := array.New(1 << 20)
buffer.SetIndex(0, i)
if i%100 == 99 {
runtime.GC()
time.Sleep(time.Millisecond)
}
}
js.Global().Call("report")
}run.mjs
// node run.mjs <wasm_exec.js> <program.wasm>
import { readFile } from 'node:fs/promises';
import { createRequire } from 'node:module';
const [shim, wasm] = process.argv.slice(2);
globalThis.require = createRequire(import.meta.url);
(0, eval)(await readFile(shim, 'utf8'));
const go = new Go();
globalThis.report = () => {
const live = go._values.filter((v) => v instanceof Uint8Array).length;
console.log(`${live} of 2000 arrays still referenced, ${go._values.length} slots in the value table`);
process.exit(0);
};
const { instance } = await WebAssembly.instantiate(await readFile(wasm), go.importObject);
go.run(instance);Build and run
tinygo build -target=wasm -gc=boehm -o leak.wasm .
node run.mjs $(tinygo env TINYGOROOT)/targets/wasm_exec.js leak.wasmResult
| Build | Arrays still referenced | Slots in the value table |
|---|---|---|
TinyGo 0.42.0 -gc=boehm |
2000 of 2000 | 2010 |
TinyGo dev 7db115a2 -gc=boehm |
2000 of 2000 | 2010 |
TinyGo 0.42.0 -gc=precise |
1 of 2000 | 111 |
TinyGo 0.42.0 -gc=conservative |
1 of 2000 | 112 |
| Go 1.26.8 | 2 of 2000 | 116 |
Expected
The Boehm build releases the arrays, like the other three builds.
Cause
src/runtime/gc_boehm.go
func SetFinalizer(obj interface{}, finalizer interface{}) {
// Unimplemented.
// The GC *does* support finalization, so this could be added relatively
// easily I think.
}syscall/js sets a finalizer on each value that refers to a JavaScript object.
The finalizer calls finalizeRef, and wasm_exec.js then frees the slot in
_values. With Boehm the finalizer is never stored, so finalizeRef is never
called.
Effect in a real program
A Bubble Tea program renders WebGPU frames and reads them back. It is built
with -target=wasm -opt=2 -no-debug -gc=boehm and runs in Chrome 154.
| Go 1.26.8 | TinyGo 0.42.0 Boehm | |
|---|---|---|
| Page JS heap after about 850 frames | 20 MiB | 1024 MiB |
| Growth for each frame | 0.03 MiB | 1.20 MiB |
| Page JS heap after 70 seconds | not measured | 8.4 GiB |
Each frame is 830 by 352 pixels, which is 1.1 MiB of RGBA. The page keeps about one frame for each frame it draws.
We use Boehm here because the default collector is too slow for this program. It drew 4 to 7 frames in 25 seconds. Boehm drew about 2600.
Who is affected
- Every
-target=wasmprogram that usessyscall/jswith-gc=boehm. - Native programs on darwin, linux and windows. Boehm is the default collector
there, so
SetFinalizerdoes nothing on these targets.
Proposed fix
Implement SetFinalizer for Boehm with the finalization support that bdwgc
already has. finalize.c is already part of the build.
- Register the object with
GC_register_finalizer_no_order. - Set
GC_set_finalize_on_demand(1), so bdwgc queues finalizers and does not run them during a collection. - Move queued finalizers to a list in Go. Run them on the finalizer goroutine from #5545, with the GC lock released.
- Reuse the pressure trigger from #5545. JavaScript memory is not part of the Go heap, so the collector does not run often enough without it.
I have a patch in progress and can open a PR.
Environment
| TinyGo | 0.42.0 release, and dev at 7db115a2 |
| Go | 1.26.8 |
| Host | macOS 27.0, arm64 |
| Node | 26.7.0 |
| Browser | Chrome 154 |
Source: tinygo-org/tinygo