Skip to content

RcMap: a failing entry release is silently dropped when the map's scope closes #9307

Closed

Description

@nick-inkeep

What version of Effect is running?

4.0.5 (same on main at 34ec440). 3.22.3 propagates the failure.

What steps can reproduce the bug?

When the scope that owns an RcMap closes, it releases every entry still in the map. If one of those releases dies, the defect is discarded: the scope closes successfully and nothing is logged. This affects any entry still in the map at that point: one kept by idleTimeToLive, or one whose borrower's scope is still open. LayerMap inherits it, so a layer finalizer that dies at shutdown goes unreported.

import { Effect, RcMap } from "effect"

const resource = Effect.acquireRelease(Effect.void, () => Effect.die("release failed"))

const program = Effect.gen(function*() {
  const map = yield* RcMap.make({ lookup: (_: string) => resource, idleTimeToLive: "1 minute" })
  yield* Effect.scoped(RcMap.get(map, "a"))
})

console.log((await Effect.runPromiseExit(Effect.scoped(program)))._tag) // RcMap entry
console.log((await Effect.runPromiseExit(Effect.scoped(resource)))._tag) // same release, registered directly

These tests, added at the end of packages/effect/test/RcMap.test.ts, fail on main (the first is a control and passes):

describe("release defects", () => {
  const dyingRelease = (released: Array<string>) => (key: string) =>
    Effect.acquireRelease(
      Effect.succeed(key),
      () => Effect.sync(() => released.push(key)).pipe(Effect.andThen(Effect.die(`release ${key}`)))
    )

  it.effect("closing the map runs every release when one dies", () =>
    Effect.gen(function*() {
      const released: Array<string> = []
      const mapScope = yield* Scope.make()
      const map = yield* RcMap.make({ lookup: dyingRelease(released), idleTimeToLive: Infinity }).pipe(
        Scope.provide(mapScope)
      )
      yield* Effect.scoped(RcMap.get(map, "a"))
      yield* Effect.scoped(RcMap.get(map, "b"))

      yield* Effect.exit(Scope.close(mapScope, Exit.void))
      assert.deepStrictEqual(released.sort(), ["a", "b"])
    }))

  it.effect.each([1000, Infinity])(
    "closing the map reports a dying release of an idle entry with TTL %s",
    (idleTimeToLive) =>
      Effect.gen(function*() {
        const released: Array<string> = []
        const mapScope = yield* Scope.make()
        const map = yield* RcMap.make({ lookup: dyingRelease(released), idleTimeToLive }).pipe(
          Scope.provide(mapScope)
        )
        yield* Effect.scoped(RcMap.get(map, "a"))

        const exit = yield* Effect.exit(Scope.close(mapScope, Exit.void))
        assert.deepStrictEqual(released, ["a"])
        assert.deepStrictEqual(exit, Exit.die("release a"))
      })
  )

  it.effect("closing the map reports a dying release of a borrowed entry", () =>
    Effect.gen(function*() {
      const released: Array<string> = []
      const mapScope = yield* Scope.make()
      const borrowerScope = yield* Scope.make()
      const map = yield* RcMap.make({ lookup: dyingRelease(released) }).pipe(Scope.provide(mapScope))
      yield* RcMap.get(map, "a").pipe(Scope.provide(borrowerScope))

      const exit = yield* Effect.exit(Scope.close(mapScope, Exit.void))
      assert.deepStrictEqual(released, ["a"])
      assert.deepStrictEqual(exit, Exit.die("release a"))
    }))
})

What is the expected behavior?

Every entry is still released, and the owner scope's close fails with the release's defect. The defect already surfaces when the same acquireRelease is registered directly in that scope, when the entry's last borrower releases it (with the default idleTimeToLive), when an RcRef owner closes, and from RcMap owner closure in 3.22.3. Scope itself attempts every finalizer and still returns the defect, as the "finalizers that throw" tests in Scope.test.ts check.

What do you see instead?

The snippet prints Success, then Failure. The release runs, but its defect reaches neither the exit nor the log. The three tests fail with expected { _id: 'Exit', _tag: 'Success', …(1) } to deeply equal { _id: 'Exit', _tag: 'Failure', …(1) }.

Additional information

The map's finalizer wraps each entry's close in Effect.exit and never reads the exits (RcMap.ts#L273-L292). main has the same code. 3.22.3 used forEachSequentialDiscard over scopeClose, which propagated the defect but stopped at the first failing entry, so later entries were never released. 4.0.5 releases all of them but drops the result. One option: after the map is cleared, combine the collected exits with Exit.asVoidAll, as scopeCloseFinalizers does with its finalizers' exits (internal/effect.ts#L3982-L4003).

Two related paths on 4.0.5 behave the same way:

Activity

  1. self-assigned this
    on Oct 16, 2026
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