Skip to content

A spin called in a jump's argument sinks the variable it was lent, so a loop no longer keeps the old list's cells - #1243

Merged
Lorenzobattistela merged 3 commits into
bendlang:mainfrom
mizchi:fix/spin-lent-sink-upstream
Oct 3, 2026

Conversation

@mizchi

@mizchi mizchi commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

What you did

On main @ 43513c4, save the program below as main.bend, compile it, and run it on the C and JS lanes:

bun bend2/main.ts main.bend -o main -o main.js
./main --gpu off --threads 1
./main --gpu off --threads 4
bun main.js

What happened

All runs print the correct result:

4000010 2000000 4

The C run retains the old list's cells on every loop iteration. A spin called inside a jump's argument, as in go(p, Xs.walk(xs, xs, X0{})), is lent a variable that dies at the call; emit_fuse runs bind_dead only for a tail call, and the jump runs none. Reproduced locally.

C worker threads Max RSS before Max RSS with this fix
1 81.6 MB 1.6 MB
4 81.6 MB 1.6 MB

Measured with wait4 of the child, decimal MB, --gpu off.

This PR calls bind_dead(fl, tail ? [] : fl.rest) after the spin returns. Tail calls keep their existing cleanup; non-tail calls sink bindings the remaining expressions do not use. Reclamation adds work where memory previously leaked and can move cleanup earlier at other non-tail spin calls. The generated C for this program differs only by two added term_sink calls.

Fork argument evaluation also includes hold in rest: emit_args({ ...fl, rest: [...hold, chain[i]] }, c). A nested spin must preserve the parallel join's captures even when the serial chain no longer uses them, so both paths supply the same values to the join.

spin_lent_sink.bend covers passing one variable twice (go) and lending one variable to a parameter another call site made borrowed (go2). Its #| lines check output, not RSS; memory was checked separately. The comment now records approximately 81.6 MB before and 1.7 MB after the fix, consistent with the review measurements. Local measurements after both fixes were 1.6 MB with 1, 4 and 8 C threads.

fork_spin_hold.bend adds the reviewer's fork program as a regression. Before the fork fix, it prints 12 5 with 1 C thread but fails with bend: frontier drained without a result with 4 and 8 threads. With the fix, all three thread settings print 12 5.

  • All 28 spin/fork tests in tests/compile, tests/reg and tests/run pass on C with --gpu off --threads 1, 4 and 8, and on JS under both Bun and Node 24 (140 executions).
  • The new fork regression also passes the interpreter and --verdict (ALL PROOFS CHECK).
  • gates/repo.ts: PASS: 50 / 50, including the new regression file.
  • gates/test.ts --gate could not connect: SSH could not resolve the cluster host. The cluster test/performance gates remain unverified locally. GPU execution was not run.

Searched issues and PRs in all states for leak spin, emit_fuse, bind_dead, RSS grows, term_sink, and spin lent; no matching leak report found. No open PR touching comp.ts changes this branch of emit_fuse.

The file

main.bend
import Base

type Xs is Data:
  X0{}
  X1{v: U32, rest: Xs}

def Xs.walk(xs: Xs, +all: Xs, acc: Xs) -> Xs:
  match xs:
    case X0{}:
      acc
    case X1{v, rest}:
      Xs.walk(rest, all, X1{(v + 1 : U32), acc})

def Xs.sum(xs: Xs, acc: U32) -> U32:
  match xs:
    case X0{}:
      acc
    case X1{v, rest}:
      Xs.sum(rest, (acc + v : U32))

def Xs.k(n: Nat, +all: Xs, acc: U32) -> U32:
  match n:
    case 0n:
      acc
    case 1n+p:
      Xs.k(p, all, (acc + 1 : U32))

def go(k: Nat, +xs: Xs) -> Xs:
  match k:
    case 0n:
      xs
    case 1n+p:
      go(p, Xs.walk(xs, xs, X0{}))

def go2(k: Nat, +f: Nat, xs: Xs, +acc: U32) -> U32:
  match k:
    case 0n:
      acc
    case 1n+p:
      go2(p, f, X1{acc, X0{}}, Xs.k(f, xs, acc))

def other(n: Nat, +ys: Xs) -> U32:
  a = Xs.k(n, ys, 0)
  Xs.sum(ys, a)

def main() -> IO(Unit):
  +a = Xs.sum(go(U32.to_nat(1000000), X1{1, X1{2, X1{3, X1{4, X0{}}}}}), 0)
  +b = go2(U32.to_nat(1000000), 2n, X0{}, 0)
  IO.print(U32.show(a) ++ " " ++ U32.show(b) ++ " " ++ U32.show(other(3n, X1{1, X0{}})))

#|4000010 2000000 4

bend --version

bend 2.0.34

uname -sm

Darwin arm64

macOS 26.6.2, Bun 1.3.5.

clang --version (the first line)

Apple clang version 21.0.0 (clang-2100.0.123.102)

… a loop no longer keeps the old list's cells

Co-authored-by: Claude Opus 5.5 <[email protected]>
@Lorenzobattistela Lorenzobattistela self-assigned this Oct 2, 2026
@Lorenzobattistela

Lorenzobattistela commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks, the leak and the fix check out. On current main (75cc360) the test's max RSS goes from 81.6 MB to 1.7 MB, and the PR merges cleanly. I ran every gate on the PR merged onto main, and they all match main: test 1532/1532, perf 124/124, ping 46/46, and safe agrees with the kernel on the new test.

There is a problem with forks, though. Before this PR, a rest that missed a later use only leaked memory. Now bind_dead(fl, fl.rest) frees whatever rest doesn't list. emit_fork evaluates the args of call i with rest: [chain[i]], which leaves out hold, the variables the parallel join task captures. So a spin nested in a fork argument now frees a held variable. The serial path then gives the join fewer values than the parallel task stored, and it reads the wrong slots.

The program below prints "12 5" on main with 1, 4 and 8 threads. With this PR it prints "12 5" on 1 thread, but on 4 and 8 threads it fails with "bend: frontier drained without a result".

import Base

type Xs is Data:
  X0{}
  X1{v: U32, rest: Xs}

def Xs.len(+all: Xs) -> U32:
  match all:
    case X0{}:
      0
    case X1{v, rest}:
      (1 + Xs.len(rest) : U32)

def Xs.k(n: Nat, +all: Xs, acc: U32) -> U32:
  match n:
    case 0n:
      acc
    case 1n+p:
      Xs.k(p, all, (acc + 1 : U32))

def cnt(n: Nat) -> U32:
  match n:
    case 0n:
      0
    case 1n+p:
      (1 + cnt(p) : U32)

def Xs.mk(n: Nat, acc: Xs) -> Xs:
  match n:
    case 0n:
      acc
    case 1n++p:
      Xs.mk(p, X1{U32.from_nat(p), acc})

def go(f: Nat, n: Nat, zs: Xs) -> U32:
  ys = Xs.mk(n, X0{})
  a b = Xs.len(ys) cnt(U32.to_nat(Xs.k(f, zs, 0)))
  (a + b : U32)

def Xs.sum(xs: Xs, acc: U32) -> U32:
  match xs:
    case X0{}:
      acc
    case X1{v, rest}:
      Xs.sum(rest, (acc + v : U32))

def other(n: Nat, +ys: Xs) -> U32:
  a = Xs.k(n, ys, 0)
  b = Xs.len(ys)
  Xs.sum(ys, (a + b : U32))

def main() -> IO(Unit):
  IO.print(U32.show(go(5n, 7n, X1{1, X0{}})) ++ " " ++ U32.show(other(3n, X1{1, X0{}})))

#|12 5

Including hold in that rest fixes it; with this change the program prints "12 5" on 1, 4 and 8 threads:

const cargs = emit_args({ ...fl, rest: [...hold, chain[i]] }, c);

A few more things:

  • Please add the program above as a regression test. It's the fork path your review note called untested, and the gate catches it on the multi-thread lanes.
  • spin_lent_sink.bend checks output only, and main prints the same line, so reverting the fix wouldn't fail it. That's fine for a reg test, but it's worth knowing it guards against a crash, not the leak.
  • The test comment says 79 MB → 10 MB, but your PR body and my runs both show about 81.6 MB → 1.7 MB. Could you update it?

Include hold in the rest used to evaluate fork arguments so nested spins preserve the captures shared with the parallel join. Add the reviewer regression and correct the RSS comment in spin_lent_sink.

Verified Red on 4/8 C threads, then Green on 1/4/8 threads and JS. All 28 related tests pass on C, Bun and Node; the new test passes the interpreter and kernel verdict. Repo gate: 50/50.
@mizchi

mizchi commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Addressed in bee6b1a.

  • Included hold in the rest used to evaluate fork arguments.
  • Added your program as tests/reg/fork_spin_hold.bend. It reproduced the failure on 4/8 C threads before the fix and now prints 12 5 on 1/4/8 threads.
  • Corrected the RSS comment to approximately 81.6 MB → 1.7 MB and clarified that the test checks output, not RSS.

All 28 related spin/fork tests pass on C (1/4/8 threads, GPU off) and Bun/Node JS. The new regression also passes the interpreter and --verdict; the repo gate passes 50/50.

… keeps handing its held lists to its calls

bee6b1a put the fork's held variables in the rest of every call's
arguments. That kept a nested spin from sinking them, but it also made
every held variable passed straight to a forked call look alive after
it, so the call borrowed it instead of taking it: tree-matmul ran 1.94x
slower SEQ and 1.76x PAR. The sink after a non-tail spin call now looks
only at the spin's own variable arguments, which is where the lent
variable that leaked lives, and emit_fork's rest is main's again.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Lorenzobattistela

Copy link
Copy Markdown
Collaborator

Thanks for the quick update! I ran every gate on the PR merged onto main (947db72). Your fork fix fixed the fork_spin_hold case, but it made tree-matmul about 1.8x slower: SEQ went from 1.05x to 1.94x and PAR from 1.03x to 1.76x. Adding hold to the fork's rest also covered the variables passed straight to the forked calls. Those then looked alive after the call, so the call borrowed them instead of taking them, couldn't free their cells in place, and its frame grew.

I pushed 897a313 on top of your branch. It reverts the emit_fork line and limits the sink after a non-tail spin call to the spin's own variable arguments, which is where the leaked lent variable is. Both of your regression tests pass on 1, 4 and 8 threads, and the leak test still runs at 1.7 MB. Every bench compiles to the same C as main except hashmap, where one term_sink moves up a line, and tree-matmul is back to 1.05x. Gates: repo 49/49, ping 46/46, test 1540/1540, perf 124/124.

@Lorenzobattistela
Lorenzobattistela merged commit 5e56772 into bendlang:main Oct 3, 2026
Lorenzobattistela added a commit that referenced this pull request Oct 3, 2026
36 commits since 2.0.34: the M1/M2 Metal pipeline crash (#1154), names
keyed ns:name so a library's CID(None) means its own None (#1045), less
CPU memory from the host's column and a spin's lent variable (#1171,
#1269, #1243), equality-proof arrays (#1130), timers firing under a busy
loop (#1122), and the CLI and --verdict fixes listed in CHANGELOG.md.

Gates on 5e56772 over the bastion: repo 49/49, ping 46/46, test
1540/1540, perf 121/124.

CHANGELOG.md and VERSION name 2.0.35.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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.

3 participants