Skip to content

Effect.catchDefect discards a typed failure or interruption that sits beside the defect (e.g. a finalizer that dies) #9368

Closed

Description

@nick-inkeep

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)))) // control
Fail + 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

  1. self-assigned this
    on Oct 16, 2026
  2. effect-bot commented on Oct 17, 2026

    @effect-bot
    Contributor

    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 + Die or Interrupt + Die cause counts as a defect cause, so catchDefect recovers it.

    The docs say catchDefect recovers defects "without catching typed failures or interruptions". That means f only ever receives a defect, and a cause with no Die in 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:

    • catchAllDefect found the first Die anywhere in the cause and recovered, discarding the other reasons. v4's catchDefect does the same thing.
    • On the typed side, catchAll went through matchEffect. If the cause held any defect, matchEffect re-failed with electFailures(cause) and never called the typed handler. A typed error sitting beside a defect was never treated as recoverable.

    Making catchDefect skip mixed causes (the approach in #9387) would have the opposite effect. In v4, Effect.catch currently does recover Fail + 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, ignore and Stream.catch currently recover Fail + Die. That's tracked separately for a maintainer decision. I'm leaving this issue open for maintainers to decide how to close it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions