fix(effect): give finalizer callbacks their own cleanup scope

This commit is contained in:
Effect Bot
2026-11-25 08:59:52 +00:00
parent 2576bbcbe8
commit 74af3a81ef
3 changed files with 73 additions and 3 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"effect": patch
---
Give `Effect.addFinalizer` and `Effect.acquireRelease` release callbacks their own scope so temporary scoped resources stay alive until cleanup finishes.
+7 -2
View File
@@ -4169,6 +4169,11 @@ export const scopedWith = <A, E, R>(
return onExit(f(scope), (exit) => suspend(() => scopeCloseUnsafe(scope, exit) ?? void_))
})
const provideFinalizerContext = <A, E, R>(
self: Effect.Effect<A, E, R>,
context: Context.Context<R>
): Effect.Effect<A, E> => effectIsExit(self) ? self : provideContext(scoped(self), context) as any
/** @internal */
export const acquireRelease = <A, E, R, R2>(
acquire: Effect.Effect<A, E, R>,
@@ -4182,7 +4187,7 @@ export const acquireRelease = <A, E, R, R2>(
(scope) =>
tap(
options?.interruptible ? restore(acquire) : acquire,
(a) => scopeAddFinalizerExit(scope, (exit) => provideContext(release(a, exit), context))
(a) => scopeAddFinalizerExit(scope, (exit) => provideFinalizerContext(release(a, exit), context))
)
)
)
@@ -4196,7 +4201,7 @@ export const addFinalizer = <R>(
scope,
(scope) =>
contextWith((context: Context.Context<R>) =>
scopeAddFinalizerExit(scope, (exit) => provideContext(finalizer(exit), context))
scopeAddFinalizerExit(scope, (exit) => provideFinalizerContext(finalizer(exit), context))
)
)
+61 -1
View File
@@ -1,5 +1,5 @@
import { assert, describe, expect, it } from "@effect/vitest"
import { Cause, Deferred, Duration, Effect, Exit, Fiber, Scope } from "effect"
import { Cause, Context, Deferred, Duration, Effect, Exit, Fiber, Scope } from "effect"
import { TestClock } from "effect/testing"
describe("Scope", () => {
@@ -30,6 +30,66 @@ describe("Scope", () => {
assert.deepStrictEqual(order, ["release", "effect", "scope"])
}))
describe("cleanup scope", () => {
const Tag = Context.Service<string>("ScopeTest/Tag")
it.effect("Effect.addFinalizer keeps resources acquired during cleanup alive until cleanup finishes", () =>
Effect.gen(function*() {
const events: Array<string> = []
yield* Effect.scoped(
Effect.gen(function*() {
yield* Effect.addFinalizer((exit) =>
Effect.gen(function*() {
const tag = yield* Tag
yield* Effect.acquireRelease(
Effect.sync(() => {
events.push("acquire")
}),
() =>
Effect.sync(() => {
events.push("release")
})
)
events.push(`use:${tag}:${Exit.isFailure(exit) ? String(Cause.squash(exit.cause)) : "ok"}`)
})
).pipe(Effect.provideService(Tag, "captured"))
return yield* Effect.fail("boom")
})
).pipe(Effect.exit)
expect(events).toEqual(["acquire", "use:captured:boom", "release"])
}))
it.effect("Effect.acquireRelease keeps resources acquired during release alive until release finishes", () =>
Effect.gen(function*() {
const events: Array<string> = []
yield* Effect.scoped(
Effect.gen(function*() {
yield* Effect.acquireRelease(
Effect.succeed("resource"),
(resource, exit) =>
Effect.gen(function*() {
const tag = yield* Tag
yield* Effect.acquireRelease(
Effect.sync(() => {
events.push("acquire")
}),
() =>
Effect.sync(() => {
events.push("release")
})
)
events.push(
`use:${resource}:${tag}:${Exit.isFailure(exit) ? String(Cause.squash(exit.cause)) : "ok"}`
)
})
).pipe(Effect.provideService(Tag, "captured"))
return yield* Effect.fail("boom")
})
).pipe(Effect.exit)
expect(events).toEqual(["acquire", "use:resource:captured:boom", "release"])
}))
})
describe("parallel finalization", () => {
it.effect("executes finalizers in parallel", () =>
Effect.gen(function*() {