Skip to content

Interrupted fiber in uninterruptible block drops interrupt and leaks typed Fail past catch #8650

Closed

Description

@mepuka

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:

  1. The surrounding catch handler (Effect.catch / Effect.catchCause) is skipped.
  2. The typed Fail escapes directly to the fiber's exit.
  3. 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 in 4.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

  1. Since the child fiber is typed Fiber<number, never>, it should never terminate with Cause.hasFailures === true.
  2. Since the catch handler was bypassed due to the pending interruption, the fiber's exit should reflect the interruption (Cause.isInterrupted === true).
  3. Any typed Fail causes should be stripped when bypassing catch handlers, matching Effect 3 semantics (stripFailures(cause) combined with _interruptedCause).

Root Cause Analysis & Code References

  1. Failure loop discards handlers without stripping Fail or 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.interruptible is restored and fiber._interruptedCause is present, the while-loop discards the catch continuation. However, it passes the original cause (which contains Fail(42)) directly to exitFailCause(cause) without removing Fail reasons or combining with fiber._interruptedCause.

  2. SetInterruptible only 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!)
        }
      }
    })

    SetInterruptible only converts fiber._interruptedCause to an exit on contAll (the success path). On the failure path (contE), SetInterruptible is popped and discarded by the while-loop in core.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):

  1. Strip Fail reasons from cause (retaining untyped defects).
  2. Combine the stripped cause with fiber._interruptedCause.
if (fiber.interruptible && fiber._interruptedCause) {
  cause = Cause.combine(Cause.stripFailures(cause), fiber._interruptedCause)
}
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