Repository navigation
Description
What is the problem?
An Effect.fn call's stack frame keeps every argument of that call alive. A fiber forked inside the call inherits the frame through CurrentStackFrame, so the arguments of every traced call above it stay reachable for as long as that fiber lives, long after the calls have returned.
Reproduction
Run with Node against [email protected]:
import { Effect, Fiber } from "effect"
import { setFlagsFromString } from "node:v8"
import { runInNewContext } from "node:vm"
setFlagsFromString("--expose_gc")
const gc = runInNewContext("gc") as () => void
const start = Effect.fn("start")(function*(input: { payload: string }) {
yield* Effect.annotateCurrentSpan("payload", input.payload.length)
return yield* Effect.forkDetach(Effect.never)
})
let reference: WeakRef<object> | undefined
const fiber = await (async () => {
const input = { payload: "x".repeat(1024) }
reference = new WeakRef(input)
return Effect.runPromise(start(input))
})()
for (let i = 0; i < 8; i++) {
await new Promise((resolve) => setTimeout(resolve, 0))
gc()
}
console.log(reference!.deref() ? "retained" : "collected")
await Effect.runPromise(Fiber.interrupt(fiber))| Result | |
|---|---|
| Expected | collected: start has returned and nothing references input |
| Actual | retained |
Effect.fnUntraced |
collected |
Bun shows the same behaviour.
Cause
makeFn in packages/effect/src/internal/effect.ts builds the frame inside the traced function:
return defineFunctionLength(body.length, function(this: any, ...args: Array<any>) {
let result = suspend(() => {
const iter = body.apply(this, arguments) // closes over `arguments`
...
})
...
return updateService(result, CurrentStackFrame, (prev) => ({
name,
stack: callError ? fnStackCleaner(() => callError.stack) : constUndefined, // same scope
parent: { name: definitionName, stack: definitionStack, parent: prev }
}))
})The stack closure only reads callError. Engines allocate one context per scope, though, and this scope also holds arguments, which the suspend thunk uses. So the frame's stack closure keeps the whole arguments object alive:
flowchart LR
F["forked fiber"] --> C["context: CurrentStackFrame"]
C --> S["frame.stack closure"]
S --> A["scope: arguments"]
A --> I["every call argument"]
Impact
We found this in a production heap snapshot of a long-running server (opencode). 152 fibers that outlive their calls kept the frame chain … < SessionRunner.runStep, for example:
- watchers and plugin services first started inside a request;
- background job notifiers.
runStep receives the full loaded conversation as its first argument, so those fibers kept 117 MB of message history (including base64 screenshots) alive indefinitely.
Proposed fix
Build the frame in a helper outside the traced function, so the closure's context holds only name, callError and the definition frame. PR: #9878
Activity