Repository navigation
Description
Summary
In Effect 4 (4.0.0-rc.112 through 4.0.0), if an interrupt is recorded on a fiber while it is suspended in an uninterruptible block and that block subsequently fails:
- The surrounding catch handler (
Effect.catch/Effect.catchCause) is skipped. - The typed
Failescapes directly to the fiber's exit. - The recorded interruption (
_interruptedCause) is completely dropped from the exit cause.
This violates static type guarantees: a fiber statically typed as Fiber<number, never> (or Effect<number, never>) completes with Exit.Failure(Cause.fail(42)). Furthermore, callers inspecting Cause.isInterrupted(exit.cause) receive false, even though the fiber was interrupted and its catch handlers were bypassed specifically because of that pending interruption.
Environment
- Effect version:
4.0.0(also present in4.0.0-rc.112) - Runtime: Node.js / Bun (tested on Bun 1.4.2 and Node 22)
- TypeScript: 5.x / 7.x
Minimal Reproduction
Self-contained script that typechecks with 0 errors:
import { Effect, Fiber, Deferred, Exit, Cause } from "effect"
const program = Effect.gen(function* () {
const latch = yield* Deferred.make<void>()
// Child fiber is statically certified as Fiber.Fiber<number, never>
const child = yield* Effect.forkChild(
Effect.catch(
Effect.uninterruptible(
Effect.andThen(Deferred.await(latch), Effect.fail(42))
),
() => Effect.succeed(0)
)
)
// Static type assertion: E is 'never'
const _typeCheck: Fiber.Fiber<number, never> = child
yield* Effect.yieldNow
// 1. Interrupt the child while suspended inside the uninterruptible block
child.interruptUnsafe()
// 2. Resolve latch so the uninterruptible block fails with 42
yield* Deferred.succeed(latch, undefined)
const exit = yield* Effect.exit(Fiber.join(child))
console.log("Exit tag:", exit._tag)
if (Exit.isFailure(exit)) {
console.log("Raw Cause:", JSON.stringify(exit.cause, null, 2))
console.log("Cause.isInterrupted:", Cause.isInterrupted(exit.cause))
console.log("Cause.hasFailures:", Cause.hasFailures(exit.cause))
}
})
Effect.runPromise(program)Actual Output
Exit tag: Failure
Raw Cause: {
"_id": "Cause",
"failures": [
{
"_tag": "Fail",
"error": 42
}
]
}
Cause.isInterrupted: false
Cause.hasFailures: true
Expected Behavior
- Since the child fiber is typed
Fiber<number, never>, it should never terminate withCause.hasFailures === true. - Since the catch handler was bypassed due to the pending interruption, the fiber's exit should reflect the interruption (
Cause.isInterrupted === true). - Any typed
Failcauses should be stripped when bypassing catch handlers, matching Effect 3 semantics (stripFailures(cause)combined with_interruptedCause).
Root Cause Analysis & Code References
-
Failure loop discards handlers without stripping
Failor preserving interrupt (packages/effect/src/internal/core.ts:556-563):let cont = fiber.getCont(contE) while (fiber.interruptible && fiber._interruptedCause && cont) { cont = fiber.getCont(contE) } return cont ? cont[contE](cause, fiber, annotated ? undefined : this) : fiber.yieldWith(annotated ? exitFailCause(cause) : this)
When
fiber.interruptibleis restored andfiber._interruptedCauseis present, the while-loop discards thecatchcontinuation. However, it passes the originalcause(which containsFail(42)) directly toexitFailCause(cause)without removingFailreasons or combining withfiber._interruptedCause. -
SetInterruptibleonly checks interruption on success path (packages/effect/src/internal/effect.ts:4572-4580):const setInterruptible: (interruptible: boolean) => Primitive = makePrimitive({ op: "SetInterruptible", [contAll](fiber) { fiber.interruptible = this[args] if (fiber._interruptedCause && fiber.interruptible) { return () => failCause(fiber._interruptedCause!) } } })
SetInterruptibleonly convertsfiber._interruptedCauseto an exit oncontAll(the success path). On the failure path (contE),SetInterruptibleis popped and discarded by the while-loop incore.ts.
Suggested Fix
In packages/effect/src/internal/core.ts inside exitFailCause:
When the discard loop terminates, if the fiber was interrupted (fiber.interruptible && fiber._interruptedCause):
- Strip
Failreasons fromcause(retaining untyped defects). - Combine the stripped cause with
fiber._interruptedCause.
if (fiber.interruptible && fiber._interruptedCause) {
cause = Cause.combine(Cause.stripFailures(cause), fiber._interruptedCause)
}
Activity