Skip to content

fix(tsx): let a runtime go once its memory and instances are cleaned up - #2825

Merged
houko merged 1 commit into
mainfrom
fix/tsx-memory-cleanup
Dec 1, 2026
Merged

houko merged 1 commit into
mainfrom
fix/tsx-memory-cleanup

Conversation

@houko

@houko houko commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Summary

runtime.AddCleanup never runs a cleanup, nor collects its object, while the object is reachable from the cleanup or its argument. Two cleanups in tsx reached their object through the runtime:

  • hold (tsx/memory_goja.go) registered m.release, the sandbox's quota, which keeps pin, iterate, project js.Callable and so the runtime, whose JS values reach the allocation. Every runtime that charged an allocation (ArrayBuffers, typed arrays, arrays, strings of heldMin or more) was never collected.
  • bind (tsx/classes.go) registered a closure over the runtime r for r.classes, and the runtime's values reach the bound object. Every runtime whose JS kept an instance of a Go class was never collected.

So each plugin closed, reloaded or stopped by a watchdog, and each sandboxed view built again, stayed in memory with all its JS held. In Kando, a test package whose plugin keeps about 300 MiB grew its live heap by that much per run (425, 821, 1219 MiB over -count=3), and the watchdog's heap checks failed under the slower collections that followed.

Both cleanups now hold what they need weakly; a runtime that is gone has no quota to release into and no objects to forget, so the cleanup does nothing then.

base/formula/worker_js.go's cleanup holds the worker, which holds no engine, so it is left as it is.

Fixes #2823

Tests

  • TestMemoryLetsAClosedRuntimeGo: a runtime that charged an ArrayBuffer, a long string and an array is collected once dropped (failed before: still reachable after 10 s).
  • TestBoundInstanceLetsAClosedRuntimeGo: a runtime whose JS keeps a bound Go instance is collected once dropped (failed before).
  • go test -race ./tsx

Built against main with #2824's fix to ui/chat, which main needs to compile.

@github-actions github-actions Bot added bug Something isn't working tsx size/S local-ci-skipped and removed bug Something isn't working labels Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

This pull request has no valid Local CI declaration for its head commit, so a maintainer audits it before merging:

  • the body has no <!-- local-ci --> block under ## Verification

Run the checks on the head commit, fill the block in the template, then push or edit the body: this label and comment update on their own.

Two cleanups reached the object they were registered on through the runtime, which runtime.AddCleanup never runs nor lets be collected, so every runtime that charged an allocation to its memory quota, or whose JS kept an instance of a Go class, stayed in memory for ever with all it held: each plugin closed or stopped and each sandboxed view built again.

- hold's cleanup held the quota (m.release), which reaches the runtime through pin, iterate and project; it now holds the quota weakly.
- bind's cleanup held the runtime (r.classes); it now holds the runtime weakly.

A runtime that is gone has nothing left to release or forget, so a cleanup that finds it collected does nothing.

Fixes #2823
@houko
houko force-pushed the fix/tsx-memory-cleanup branch from 170f9d6 to eb7354e Compare December 1, 2026 02:17
@github-actions github-actions Bot added the bug Something isn't working label Dec 1, 2026
@houko
houko merged commit a10710d into main Dec 1, 2026
47 checks passed
@houko
houko deleted the fix/tsx-memory-cleanup branch December 1, 2026 02:34
houko added a commit to yldm-tech/Kando that referenced this pull request Dec 1, 2026
…go (#605)

NSGo a10710d (nsgo-dev/nsgo#2825) fixes two cleanups in tsx that kept every plugin runtime that charged an allocation, or kept a Go class instance, in memory for ever. In Kando that leaked each closed, reloaded or stopped plugin and each panel built again, and made the script watchdog's memory tests fail when the package ran more than once.

The range also brings nsgo-dev/nsgo#2739, which makes core.Style.FlexShrink a type of its own: every 0 becomes core.NoShrink and every 1 core.Shrink(1), and growing boxes and scroll views now shrink by default.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tsx: the memory quota's cleanup keeps every runtime that charged an allocation reachable for ever

1 participant