Repository navigation
Description
What version of Effect is running?
[email protected] on Node 26.11.1. The code is unchanged on main.
What steps can reproduce the bug?
When an effect fails with a typed error and a finalizer also dies, the Cause holds a Fail and a Die. Effect.catchDefect recovers that whole Cause, so the typed failure disappears and the effect succeeds. An Interrupt beside a Die is lost the same way.
import { Effect, Exit } from "effect"
const show = (exit: Exit.Exit<unknown, unknown>) =>
Exit.isSuccess(exit) ? `Success(${exit.value})` : exit.cause.reasons.map((r) => r._tag).join(" + ")
const recover = Effect.catchDefect(() => Effect.succeed("recovered"))
// A typed failure whose finalizer dies
const failed = Effect.fail("typed").pipe(Effect.ensuring(Effect.die("finalizer defect")))
console.log(show(Effect.runSyncExit(failed)))
console.log(show(Effect.runSyncExit(recover(failed))))
console.log(show(Effect.runSyncExit(recover(Effect.fail("typed"))))) // control
// An interruption whose finalizer dies
const interrupted = Effect.interrupt.pipe(Effect.ensuring(Effect.die("finalizer defect")))
console.log(show(Effect.runSyncExit(interrupted)))
console.log(show(Effect.runSyncExit(recover(interrupted))))
console.log(show(Effect.runSyncExit(recover(Effect.interrupt)))) // controlFail + Die
Success(recovered)
Fail
Interrupt + Die
Success(recovered)
Interrupt
These cases, added to the Effect.test.ts suite, fail on main:
it.effect("does not catch a typed failure beside a defect", () =>
Effect.gen(function*() {
const exit = yield* Effect.fail("error").pipe(
Effect.ensuring(Effect.die("finalizer defect")),
Effect.catchDefect(() => Effect.succeed("recovered")),
Effect.exit
)
assert.deepStrictEqual(Exit.findErrorOption(exit), Option.some("error"))
}))
it.effect("does not catch an interruption beside a defect", () =>
Effect.gen(function*() {
const exit = yield* Effect.interrupt.pipe(
Effect.ensuring(Effect.die("finalizer defect")),
Effect.catchDefect(() => Effect.succeed("recovered")),
Effect.exit
)
assert.isTrue(Exit.hasInterrupts(exit))
}))What is the expected behavior?
The typed failure and the interruption still fail the effect. The doc says catchDefect handles defects "without catching typed failures or interruptions" (Effect.ts#L3352-L3353). Effect.retry already passes this same Cause through untouched (#8799).
What do you see instead?
The handler runs and the effect succeeds with "recovered"; the Fail or Interrupt is gone. Stream.catchDefect and Channel.catchDefect, documented as "Typed failures and interruptions are not caught", do the same.
Additional information
catchDefect is catchCauseFilter(self, findDefect, f) (internal/effect.ts#L2716). findDefect matches if any reason is a Die, whatever else the Cause holds (#L181-L184), and catchCauseFilter then replaces the whole Cause with the handler's result (#L2664-L2668). Channel.catchDefect uses the same filter (Channel.ts#L4363).
Matching only when every reason is a Die, as retry does for typed failures with findErrorOnly (internal/schedule.ts#L13-L14), would let a mixed Cause through.
Activity
effect-bot commentedon Oct 17, 2026
Thanks for the detailed report and repro. After looking into the intended semantics, the current behavior is by design: when a cause contains a defect, the defect takes precedence. A
Fail + DieorInterrupt + Diecause counts as a defect cause, socatchDefectrecovers it.The docs say
catchDefectrecovers defects "without catching typed failures or interruptions". That meansfonly ever receives a defect, and a cause with noDiein it passes through untouched. The docs don't currently spell out what happens with mixed causes, which is fair to point out.Effect 3 had the same rule:
catchAllDefectfound the firstDieanywhere in the cause and recovered, discarding the other reasons. v4'scatchDefectdoes the same thing.catchAllwent throughmatchEffect. If the cause held any defect,matchEffectre-failed withelectFailures(cause)and never called the typed handler. A typed error sitting beside a defect was never treated as recoverable.Making
catchDefectskip mixed causes (the approach in #9387) would have the opposite effect. In v4,Effect.catchcurrently does recoverFail + Die, so with that change the typed error would be recovered and the defect silently dropped. That's why #9387 has been closed without merging.The part of this report that doesn't match the defect-first rule is v4's typed-error recovery:
catch,matchEffect,orElseSucceed,ignoreandStream.catchcurrently recoverFail + Die. That's tracked separately for a maintainer decision. I'm leaving this issue open for maintainers to decide how to close it.