Skip to content

Commit 2259a29

Browse files
committed
refactor(effect): build Effect.fn call frames from a local factory
Replace the module-level four-argument makeCallFrame with a one-argument closure in makeFn that already sees the definition data. Trim the comment and reword the changeset.
1 parent 6bf36b6 commit 2259a29

2 files changed

Lines changed: 13 additions & 20 deletions

File tree

‎.changeset/fn-frame-arguments.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"effect": patch
33
---
44

5-
Stop `Effect.fn` stack frames from keeping the call's arguments alive. A fiber forked inside an `Effect.fn` call inherits its stack frame, so it previously retained every argument of that call, and of every traced call above it, for as long as the fiber lived.
5+
Fix `Effect.fn` stack frames retaining call arguments. A fiber forked inside an `Effect.fn` call inherits the frame, which kept the arguments of that call and of every traced call above it alive for as long as the fiber ran.

‎packages/effect/src/internal/effect.ts‎

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,17 @@ const makeFn = (
13531353
: (pipeables.shift()!).bind(bodyOrOptions.self)
13541354
const definitionName = `${name} (definition)`
13551355
const definitionStack = defError ? fnStackCleaner(() => defError.stack) : constUndefined
1356+
// Built outside the traced function so the frame cannot capture its `arguments`,
1357+
// which forked fibers inheriting the frame would otherwise keep alive.
1358+
const makeCallFrame = (callError: Error | undefined) => (prev: StackFrame | undefined): StackFrame => ({
1359+
name,
1360+
stack: callError ? fnStackCleaner(() => callError.stack) : constUndefined,
1361+
parent: {
1362+
name: definitionName,
1363+
stack: definitionStack,
1364+
parent: prev
1365+
}
1366+
})
13561367

13571368
return defineFunctionLength(body.length, function(this: any, ...args: Array<any>) {
13581369
let result = suspend(() => {
@@ -1377,29 +1388,11 @@ const makeFn = (
13771388
useSpan(name, spanOptions!, (span) => provideParentSpan(result, span)) :
13781389
result,
13791390
CurrentStackFrame,
1380-
makeCallFrame(name, callError, definitionName, definitionStack)
1391+
makeCallFrame(callError)
13811392
)
13821393
})
13831394
}
13841395

1385-
// Built outside the traced function so the frame's `stack` closure cannot capture that call's `arguments`.
1386-
// Frames outlive the call in forked fibers, which would otherwise keep every argument alive.
1387-
const makeCallFrame = (
1388-
name: string,
1389-
callError: Error | undefined,
1390-
definitionName: string,
1391-
definitionStack: () => string | undefined
1392-
) =>
1393-
(prev: StackFrame | undefined): StackFrame => ({
1394-
name,
1395-
stack: callError ? fnStackCleaner(() => callError.stack) : constUndefined,
1396-
parent: {
1397-
name: definitionName,
1398-
stack: definitionStack,
1399-
parent: prev
1400-
}
1401-
})
1402-
14031396
/** @internal */
14041397
export const fnUntracedEager: Effect.fn.Untraced = (
14051398
body: Function,

0 commit comments

Comments
 (0)