Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fiber-async-context-unenterable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Resume a fiber without its captured async context when the runtime refuses to enter it, as Cloudflare Workers does for a context captured during a request that has since finished. Previously the fiber's resumption threw "Cannot call this AsyncLocalStorage bound function outside of the request in which it was created", breaking long-lived fibers such as `OtlpExporter`'s.
16 changes: 15 additions & 1 deletion packages/effect/src/internal/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@

let nextFiberId = 0

interface AsyncContext {

Check failure on line 519 in packages/effect/src/internal/effect.ts

View workflow job for this annotation

GitHub Actions / Test (Bun)

error: All fibers interrupted without error

at causeSquash (/home/runner/work/effect/effect/packages/effect/src/internal/effect.ts:519:38) at <anonymous> (/home/runner/work/effect/effect/packages/effect/src/Stream.ts:8238:51) at addObserver (/home/runner/work/effect/effect/packages/effect/src/internal/effect.ts:715:6) at start (/home/runner/work/effect/effect/packages/effect/src/Stream.ts:8234:21) at <anonymous> (/home/runner/work/effect/effect/packages/effect/src/Stream.ts:8224:9) at makeResponse (/home/runner/work/effect/effect/packages/platform/bun/src/BunHttpServer.ts:277:67) at <anonymous> (/home/runner/work/effect/effect/packages/platform/bun/src/BunHttpServer.ts:192:36) at ~effect/Effect/evaluate (/home/runner/work/effect/effect/packages/effect/src/internal/effect.ts:1119:22) at runLoop (/home/runner/work/effect/effect/packages/effect/src/internal/effect.ts:850:116)
runInAsyncScope<This, Arg, R>(fn: (this: This, arg: Arg) => R, thisArg: This, arg: Arg): R
emitDestroy(): void
}
Expand Down Expand Up @@ -640,13 +640,27 @@
} else if (this._asyncContext !== undefined) {
const asyncContext = this._asyncContext
this._asyncContext = undefined
let entered = false
try {
return asyncContext.runInAsyncScope(this.evaluate, this, effect)
return asyncContext.runInAsyncScope(
function(this: FiberImpl<A, E>, effect: Primitive) {
entered = true
return this.evaluate(effect)
},
this,
effect
)
} catch (error) {
// Runtimes that tie async context to a request, like Cloudflare Workers,
// refuse to enter one captured during a request that has since finished.
// Resume without it rather than failing the fiber.
if (entered) throw error
} finally {
// Each captured context is resumed once, so release it for async_hooks
// consumers like vitest's detectAsyncLeaks.
asyncContext.emitDestroy()
}
return this.evaluate(effect)
} else if (this._yielded !== undefined) {
const yielded = this._yielded as () => void
this._yielded = undefined
Expand Down
28 changes: 28 additions & 0 deletions packages/effect/test/Effect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5776,6 +5776,34 @@ describe("Effect", () => {
})
}

it("resumes a fiber whose captured async context can no longer be entered", async () => {
// Cloudflare Workers ties async context to the request that created it, and
// refuses to enter it from a later request.
const builtin = process.getBuiltinModule
vi.spyOn(process, "getBuiltinModule").mockImplementation(
((name: string) =>
name === "node:async_hooks"
? {
AsyncResource: class {
runInAsyncScope() {
throw new Error("Cannot call this AsyncLocalStorage bound function outside of its request")
}
emitDestroy() {}
}
}
: builtin(name)) as typeof process.getBuiltinModule
)
vi.resetModules()
try {
const { Effect: Fresh } = await import("effect")
const resumed = await Fresh.runPromise(Fresh.sleep(1).pipe(Fresh.as("resumed")))
assert.strictEqual(resumed, "resumed")
} finally {
vi.restoreAllMocks()
vi.resetModules()
}
})

// Bun's async_hooks report no trigger ids or destroy events, so any process.nextTick
// in the worker, including from concurrent tests, would count as a leak.
const itWithAsyncHooks = it.skipIf(process.versions.bun !== undefined)
Expand Down