Summary

Implements Promise.become(fiber) to address #9877.

The issue identifies that when a Fiber forks work which will eventually complete a Promise, and then awaits that Promise, we pay an unnecessary overhead:

  1. The forked fiber allocates a callback to complete the promise
  2. promise.await allocates an asyncInterrupt callback to register itself
  3. These two callbacks communicate indirectly through the Promise state machine

Solution

Promise.become(fiber) wires a fiber directly to a promise by registering an observer via fiber.unsafe.addObserver. When the fiber completes, the observer fires completeWith(ZIO.done(exit)), completing the promise with zero extra allocation on the completion path.

// Before: indirect, allocates intermediate callback
for {
promise <- Promise.make[E, A]
fiber <- (work >>= promise.succeed).fork
result <- promise.await
} yield result
// After: direct observer registration, no intermediate callback
for {
promise <- Promise.make[E, A]
fiber <- work.fork
_ <- promise.become(fiber)
result <- promise.await
} yield result

Implementation

// Public API (safe)
def become(fiber: Fiber.Runtime[E, A])(implicit trace: Trace): UIO[Unit] =
ZIO.succeed(unsafe.become(fiber)(Unsafe))
// Unsafe implementation
def become(fiber: Fiber.Runtime[E, A])(implicit unsafe: Unsafe): Unit = {
if (!isDone) {
fiber.unsafe.addObserver { exit =>
completeWith(ZIO.done(exit))
}
}
}

Key properties:

  • Race-safe: fiber.unsafe.addObserver is designed to handle the race between registration and completion; if the fiber has already completed, the observer fires immediately
  • Idempotent completion: completeWith uses CAS internally, so multiple observers or concurrent completions are safe
  • Short-circuit: the isDone check avoids unnecessary observer registration when the promise is already complete

Changes

  • Promise.scala: Add become to the public API, UnsafeAPI trait, and the concrete AtomicReference-based implementation

Fixes #9877

/claim #9877

Claim

Total prize pool $750
Total paid $0
Status Pending
Submitted March 01, 2026
Last updated March 01, 2026

Contributors

HH

hhhcccbbb

@hhhcccbbb

100%

Sponsors

ZI

ZIO

@ZIO

$750