diff --git a/.changeset/fiber-async-context-unenterable.md b/.changeset/fiber-async-context-unenterable.md new file mode 100644 index 00000000000..33a4153a4cc --- /dev/null +++ b/.changeset/fiber-async-context-unenterable.md @@ -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. diff --git a/packages/effect/src/internal/effect.ts b/packages/effect/src/internal/effect.ts index 8ec9d823415..37ae92e751f 100644 --- a/packages/effect/src/internal/effect.ts +++ b/packages/effect/src/internal/effect.ts @@ -640,13 +640,27 @@ export class FiberImpl implements Fiber.Fiber { } 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, 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 diff --git a/packages/effect/test/Effect.test.ts b/packages/effect/test/Effect.test.ts index 7d0928824ca..71fda9e8f0e 100644 --- a/packages/effect/test/Effect.test.ts +++ b/packages/effect/test/Effect.test.ts @@ -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)