fs2 (Scala)
Pull-based, chunked, resource-safe effectful streams for Scala, compiled down to Cats Effect fibers: a Stream[F, O] is a thin wrapper over a free Pull monad whose interpreter owns a tree of interruptible resource scopes, and every concurrent operator (switchMap, merge, concurrently, debounce) is built from fibers, Deferreds and a Channel on top of that interpreter.
| Field | Value |
|---|---|
| Language | Scala 2.12 / 2.13 / 3, on the JVM, Scala.js and Scala Native (README.md) |
| License | fs2: MIT (LICENSE); cats-effect: Apache-2.0 (LICENSE.txt) |
| Repository | typelevel/fs2 · typelevel/cats-effect |
| Documentation | fs2.io (rendered from site/guide.md) · typelevel.org/cats-effect |
| Category | pull-based effectful streams (FS2) over a fiber runtime with structured cancellation (cats-effect 3) |
| Versions surveyed | fs2 main at f679d5b45054 (2026-11-19), tlBaseVersion := "3.16" (build.sbt; newest upstream tag v3.16.0); cats-effect main at de84c01f76b2 (2026-09-28), tlBaseVersion := "3.7", newest tag v3.7.1 (build.sbt) |
| Core abstraction | Stream[+F[_], +O] wrapping Pull[F, O, Unit]; consumed by compile into one F[B]; Signal/SignallingRef for state, Channel/Topic for messaging |
NOTE
fs2's build pins "org.typelevel" %%% "cats-effect" % "3.7.1" (build.sbt); the cats-effect clone is the later main, so cats-effect claims below are verified against main source and assumed unchanged since 3.7.1. The upstream tags were read with git ls-remote; their dates were not verified. For cats-effect as an effect system — the typeclass hierarchy, Resource, the work-stealing runtime — see Cats Effect (Scala); this page covers only the parts a stream library leans on.
Overview
What it solves
fs2 gives Scala a value for "a possibly infinite, possibly effectful sequence" that is pure (a description, not a running process), resource-safe (every bracket is released exactly once, on success, error or interruption) and fast (values travel in Chunks, which may wrap unboxed arrays). The Stream scaladoc opens with the evaluation model (Stream.scala):
"'''Pull''': to evaluate a stream, a consumer pulls its values from it, by repeatedly performing one pull step at a time. Each step is a
F-effectful computation that may yield someOvalues (or none), and a stream from which to continue pulling. The consumer controls the evaluation of the stream, which effectful operations are performed, and when."
The same library covers file and socket I/O (fs2.io), hashing (fs2.hashing), text codecs and compression, so the sparkles worked pipeline — keystrokes, a debounce timer, a file-set hash, a toggle — can be written entirely in fs2 vocabulary.
Design philosophy
The README names four goals (README.md): "Its design goals are compositionality, expressiveness, resource safety, and speed." Three decisions follow from them:
- One primitive,
Pull.Pullis "a 'free' implementation ofMonad" (Pull.scala) with constructors for output, eval, acquire, scope and interruption.Streamis a newtype overPull[F, O, Unit](final class Stream[+F[_], +O] private[fs2] (private[fs2] val underlying: Pull[F, O, Unit])); every operator is either aPullloop or a composition of other operators. - Chunks are semantic, not just an optimisation. "Each pull is emitted from a chunk atomically, which is to say, any errors or interruptions in a pull can only happen between chunks, not within a chunk" (
Pull.scala). Chunk structure is observable (chunks), so theMonadright-identity law only holds up to re-chunking (Stream.scala). - Effect-polymorphic, cats-effect-shaped. Operators demand the weakest typeclass they need — none for
map,Temporalfordebounce,Concurrentformerge— and "FS2 core types (streams and pulls) are polymorphic in the effect type (as long as it is compatible withcats-effecttypeclasses)" (README.md).
How it works
Pull: a free monad with scopes
Pull[F, O, R] is a sealed ADT (Pull.scala). Its interpreter-visible leaves are Output(values: Chunk[O]), Eval(value: F[R]), Acquire(resource, release, cancelable), InScope(stream, useInterruption), InterruptWhen(haltOnSignal), the three CloseScope variants (SucceedScope, CanceledScope, FailedScope), Uncons, StepLeg, Translate and FlatMapOutput; Bind sequences them. Termination is one of Succeeded(r), Fail(error) or Interrupted(context, deferredError).
A stream transformer is a recursive Pull that unconses a chunk and outputs another. mapChunks is the shape of most of the library:
// core/shared/src/main/scala/fs2/Stream.scala
def mapChunks[O2](f: Chunk[O] => Chunk[O2]): Stream[F, O2] =
underlying.unconsFlatMap((hd: Chunk[O]) => Pull.output(f(hd))).stream.stream opens a scope; .streamNoScope does not. That one distinction decides where finalizers run, and the API exposes both spellings for most resource operators (bracket/bracketWeak, onFinalize/onFinalizeWeak).
compile: the interpreter
Pull.compile (Pull.scala) is a CPS interpreter, go(scope, extendedTopLevelScope, translation, runner, stream), that folds outputs into a B and returns one F[B]. Three of its cases carry the concurrency semantics:
goEvalruns anEvalthroughscope.interruptibleEval(...), mappingLeft(Outcome.Canceled())toInterrupted(scope.id, None).goAcquireregisters the resource on the current scope viascope.acquireResource(poll => …); the acquire is polled (cancelable) only foracquireCancelable.interruptGuardchecksscope.isInterruptedbefore acquiring, so a stream that was interrupted between steps stops at the next step.
Scopes and the interrupt context
Scope (Scope.scala) is a tree: "A child scope never outlives its parent scope. I.e., when a parent scope is closed for whatever reason, the child scopes are closed too." Resources are prepended as acquired and released in reverse order. A scope may be interruptible; then
"Normally the interruption awaits next step in Algebra to be evaluated, with exception of Eval, that when interruption is enabled on scope will be wrapped in race, that eventually allows interruption while eval is evaluating." (
Scope.scala)
The race is in InterruptContext.eval (InterruptContext.scala): F.raceOutcome(deferred.get, fa.attempt). When the interrupt Deferred wins, cats-effect cancels the losing fa — and a cancel waits for the loser's finalizers (see Concurrency and cancellation). A child interruptible scope subscribes to its parent's Deferred through a forked fiber (childContext), so interrupting a scope interrupts its whole subtree.
Concurrency is built from fibers
concurrently is representative (Stream.scala L548): it starts the background stream with compileBack.start inside a Stream.bracket, makes both sides interruptWhen(interrupt.get.attempt), and on release runs stopBack, documented in the source as "stop background process but await for it to finalise with a result". merge, parJoin, parEvalMap, switchMap and debounce are the same kit: fibers, a Deferred per stop signal, a Channel or Semaphore for hand-off, and a bracket to tie the fiber's life to a scope.
Evaluation model
- Pull, lazy, cold. Nothing runs until
compilefolds the stream; each compilation re-runs every effect. AStreamvalue is as cold as an RxJSObservableand more strictly lazy:Stream.eval(fo)evaluatesfoonly when the consumer pulls that step. - Demand-driven, chunk-granular. The consumer's
unconsdrives the producer one chunk at a time. There is norequest(n)protocol: back-pressure is implicit in the pull. Concurrent operators re-introduce bounded buffering:mergereads at most one chunk ahead per side ("there may be up to two chunks (one from each stream) waiting to be processed",Stream.scalaL2039);prefetchNandChannel.bounded(n)make the bound explicit. - Hot sources are explicit objects. Sharing comes from
SignallingRef,Channel,Topicor cats-effectQueue, each a mutable cell or queue whosediscrete/stream/subscribeproduces a fresh coldStreamper subscriber (State and sharing). - Glitch-freedom: not a goal. There is no dependency graph. Combining two
Signals through theApplicativeinstance zips theirdiscretestreams non-deterministically (nondeterministicZipinSignal.scalausesxs.either(ys).scan(...)), so a diamond(s, s.map(f)).mapN(g)can emit the intermediate(new, stale)pair. - Propagation is asynchronous. A
SignallingRef.setcompletes the waiting listeners'Deferreds; each subscriber wakes on its own fiber and runs its own pipeline. There is no transaction or batch boundary beyond what a singlemodifymakes atomic.
Composition
Stream carries about 180 distinct instance-method names and the companion about 60 constructors (a rough grep count over Stream.scala, overloads merged). They group as:
| Group | Examples |
|---|---|
| Pure, list-like | map, filter, scan, take, zip, changes, chunks, mapChunks, ++, flatMap (list monad: (a ++ b) flatMap f == (a flatMap f) ++ (b flatMap f)) |
| Effectful | evalMap, evalTap, evalScan, foreach, Stream.eval, Stream.exec, Stream.repeatEval |
| Concurrent | merge, mergeHaltBoth, either, concurrently, parJoin(n), parEvalMap(n), parEvalMapUnordered(n), switchMap, prefetch, conflate* |
| Time | debounce, metered, spaced, Stream.sleep, fixedRate, fixedDelay, awakeEvery, timeout, groupWithin, keepAlive, interruptAfter |
| Lifecycle | bracket, resource, onFinalize(Case)(Weak), scope, interruptWhen, interruptScope, pauseWhen |
| Errors | handleErrorWith, attempt, attempts, mask, onComplete, rethrow, Stream.raiseError |
Below the operator surface, Pull is the escape hatch: s.pull.uncons, uncons1, stepLeg and Pull.Timed (timeoutWith) let a user write any stateful operator as a tail-recursive pull, which is how the guide teaches take (site/guide.md, "Transforming streams using pulls"). A Pipe[F, I, O] is just Stream[F, I] => Stream[F, O], and through applies it.
Derived values are not memoised. s.map(f) re-runs per subscriber; the only cached derived value is a Signal built by hold/holdResource, which is a SignallingRef fed by a background fiber.
Time and scheduling
All time flows through cats-effect's Temporal[F] (sleep, monotonic, realTime). fs2 has no scheduler of its own and no frame clock: where an operator needs time it sleeps on a fiber, which the cats-effect runtime parks on its timer.
debounce is a fixed-duration trailing throttle, not a restart-on-input debounce
debounce takes one d: FiniteDuration, fixed when the stream is built (Stream.scala L691):
// core/shared/src/main/scala/fs2/Stream.scala — Stream#debounce (abridged)
def debounce[F2[x] >: F[x]](d: FiniteDuration)(implicit F: Temporal[F2]): Stream[F2, O] =
Stream.force {
for {
chan <- Channel.bounded[F2, O](1)
ref <- F.ref[Option[O]](None)
} yield {
val sendLatest: F2[Unit] = ref.getAndSet(None).flatMap(_.traverse_(chan.send))
def sendItem(o: O): F2[Unit] =
ref.getAndSet(Some(o)).flatMap {
case None => (F.sleep(d) >> sendLatest).start.void // first item arms ONE timer
case Some(_) => F.unit // later items only overwrite
}
def go(tl: Pull[F2, O, Unit]): Pull[F2, Nothing, Unit] =
tl.uncons.flatMap {
case Some((hd, tl)) => Pull.eval(sendItem(hd.last.get)) >> go(tl)
case None => Pull.eval(sendLatest >> chan.close.void)
}
chan.stream.concurrently(go(this.underlying).streamNoScope)
}
}Three findings follow from the source:
- The timer is not reset by new input. The first element after a quiet period starts
sleep(d); elements arriving during it only replace the slot. Atdthe latest is emitted. That is RxJS'sauditTime, notdebounceTime: a user typing continuously at 50 ms intervals withd = 300 msgets an emission every ~300 ms, never "300 ms after the last keystroke". The scaladoc says as much — "Debounce the stream with a minimum period ofdbetween each element" — and its example(1,2,3) ++ sleep(500ms) ++ (4,5) ++ sleep(10ms) ++ 6debounced by100msyieldsVector(3, 6). - Only the last element of each chunk is seen (
hd.last.get): debounce is chunk-granular. dcannot change at runtime. A runtime-adjustable interval needs either aswitchMapover an interval signal (rebuilding the operator) or a hand-written trailing debounce; the worked pipeline does the latter in two lines.
WARNING
The timer fiber is started with a bare .start.void — it is not registered on any scope and not cancelled when the debounced stream is interrupted. Reading the source, after an interruption it still wakes at d and offers its value to a channel no one drains (capacity 1, so the send completes). It is not a leak in the "grows without bound" sense, but it is an unstructured fiber inside a library that is otherwise strictly scoped. (Reading of the source; not reproduced.)
metered, spaced, fixedRate
metered(rate) is Stream.fixedRate[F2](rate).zipRight(this) (Stream.scala L725) — a rate limiter that never drops. fixedRate_ keeps a lastAwakeAt from Temporal.monotonic, sleeps until lastAwakeAt + period, and on overrun emits one catch-up tick (dampen = true) or one per missed period (Stream.scala L3615). spaced(delay) instead zips with fixedDelay, i.e. sleep(period).repeat, so it "simply introduced a fixed sleep time between elements, irrespective of other pauses in the stream or the consumer". All three take a fixed FiniteDuration.
Where the work runs
cats-effect decides: compute fibers run on the work-stealing pool, Sync.blocking thunks on a separate blocking pool, and IO.evalOn(ec) pins a region to a chosen ExecutionContext (see Cats Effect: thread pool architecture). fs2 adds nothing; fs2.io's JVM file reads are F.blocking { … } per chunk (FileHandlePlatform.scala).
Concurrency and cancellation
switchMap: latest-wins, and the next inner waits for the previous one's finalizers
switchMap is the latest-wins operator (Stream.scala L1975). Its scaladoc states the guarantee directly:
"Finializers of each inner stream are guaranteed to run before the next inner stream starts."
The implementation enforces it with a one-permit Semaphore acquired by each inner stream and released only by that inner's bracket finalizer:
// core/shared/src/main/scala/fs2/Stream.scala — Stream#switchMap
def runInner(o: O, halt: Deferred[F2, Unit]): Stream[F2, O2] =
Stream.bracketFull[F2, Unit] { poll =>
poll(guard.acquire) // guard inner with a semaphore to prevent parallel inner streams
} {
case (_, ExitCase.Errored(_)) => F.unit // if there's an error, don't start next stream
case _ => guard.release
} >> f(o).interruptWhen(halt.get.attempt)
def haltedF(o: O): F2[Stream[F2, O2]] =
for {
halt <- F.deferred[Unit]
prev <- haltRef.getAndSet(halt.some)
_ <- prev.traverse_(_.complete(())) // interrupt previous one if any
} yield runInner(o, halt)
this.evalMap(haltedF).parJoin(2)Step by step, for a new outer element o2 while inner i1 runs:
haltedF(o2)completesi1'shaltDeferred— a non-blocking signal; the outer keeps going.parJoin(2)startsi2, whose first step ispoll(guard.acquire); it blocks (cancelably, viapoll) becausei1still holds the permit.i1observes interruption at its next step or, if it is inside anEval, by theraceOutcomein its interruptible scope, which cancels the runningFand waits for that cancellation to finish.i1's scope closes, running its finalizers in reverse acquisition order; theguard.releasebracket was acquired first, so it runs last.- Only then does
i2acquire the permit and runf(o2).
So a cancel does wait for the cancelled work: the new inner starts only after the old one has fully torn down. The test suite pins this — "inner stream finalizer always runs before switching" (StreamSwitchMapSuite.scala) runs an inner whose onFinalize sleeps 100 ms and fails if the next inner sees the flag unset. Two corollaries: at most two inners exist at once (the dying one and the waiting one, hence parJoin(2)), and a burst of outer elements interrupts each waiting inner in turn, since each is itself an interruptible stream blocked in poll. If an inner errors, the permit is deliberately not released and the whole stream fails.
How cancellation reaches running work
fs2 interruption becomes cats-effect fiber cancellation at an Eval, and cats-effect's rules then decide:
- Cancellation is cooperative, observed at run-loop boundaries.
MonadCanceldefines a cancelation boundary as one "where the cancelation status of a fiber ''may'' be checked", introduced "before each of the combinators from the Cats/Cats Effect hierarchy (map,flatMap,handleErrorWith,attempt, etc)" (MonadCancel.scala). InIOFiber.runLoop(IOFiber.scalaL225)shouldFinalize()(canceled && masks == 0) is tested on every iteration, with areadBarrier()to see a cross-thread cancel everycancelationCheckThresholditerations (default512on the JVM,IORuntimeConfigCompanionPlatform.scala). Adelay { … }thunk is never interrupted mid-body. cancelbackpressures. "cancelsemantically blocks all callers until finalization is complete" (the scaladoc's[[Fiber!.cancel cancel]]link rendered as plaincancel) (GenSpawn.scalaL89). This is why fs2'sinterruptWhen,concurrentlyandswitchMapall wait.uncancelable/Poll.uncancelable(poll => …)masks the body;poll(fb)re-opens a cancelable window. "cancelation is effective; it cannot be undone. It can be suppressed, but once it is observed, it must be respected" (monadcancel.md). fs2'sbracketFullpasses thePolltoacquire, which is howswitchMap's waitingguard.acquirestays cancelable.
CPU-bound and blocking work
| Primitive | Runs on | On cancel |
|---|---|---|
IO.delay / Sync.delay | compute pool | not interrupted; cancel observed after it returns |
IO.blocking / Sync.blocking | blocking pool | "the created effect will be uncancelable" (Sync.scala L64); cancel waits for the thunk |
IO.interruptible | blocking pool | "will attempt to abort the blocking operation using thread interrupts in the event of cancelation. The interrupt will be attempted only once." |
IO.interruptibleMany | blocking pool | interrupts repeatedly until the thunk exits |
IO.cede | compute pool | yields; the scaladoc recommends (fa <* IO.cede).map(expensiveWork).guarantee(IO.cede) and splitting work above ~1 ms (IO.scala L1542) |
For the file-set hash, fs2's own stack already yields fine-grained cancel points: Files.readAll reads 64 KiB per F.blocking step (readAll(path, 64 * 1024, Flags.Read), Files.scala) and Hasher.update is Sync[F].delay(unsafeUpdate(bytes)) per chunk (HasherCompanionPlatform.scala). A cancel therefore waits for at most one 64 KiB read plus one digest update.
Other combinators
parEvalMap(n)keeps order through aChannel.bounded(n + 1)of per-element results ("One is taken by inner stream read");parEvalMapUnordereddrops ordering (Stream.scalaL2283).interruptWhen(Stream[F, Boolean] | Signal | Deferred | F[Either[Throwable, Unit]])— theFform is primitive:(Pull.interruptWhen(haltOnSignal) >> this.pull.echo).stream.interruptScope. TheStream[F, Boolean]overload runs the watcher on a background fiber and, on release, awaits it (stopWatch = interruptR.complete(()) >> backResult.get…) (Stream.scalaL1764).interruptAfter(d)isinterruptWhen(sleep(d).attempt).concurrentlyruns a background stream for the lifetime of the foreground; a background error interrupts the foreground and is re-raised.mergehalts when both sides halt, or on the first error, stopping the other side through a sharedstopDef(Stream.scalaL2039).
Errors and completion
A stream terminates in exactly one way: normal exhaustion, an error (Fail), or interruption (Interrupted), the last of which is not an error and is invisible to handleErrorWith.
- Errors are
Throwables inF.Stream.raiseError(e) ++ s == Stream.raiseError(e)andraiseError(e).handleErrorWith(h) == h(e)are stated laws (Stream.scala).handleErrorWithisPull.scope(underlying).handleErrorWith(…), so the failed stream's resources are released before the handler runs (Stream.scalaL1623). attemptmaterialises the first error as a terminalLeft(Right(1), Right(2), Right(3), Left(...)), then halts;attempts(delays)retries. A typed-error discipline (EitherT, or the result as data) is a user choice; the library's channel isThrowable.- Errors are terminal; there is no resumption inside a stream — a recovered stream continues with the handler's stream. Per-element failure is modelled by
attempton the inner effect (evalMap(f(_).attempt)), as the worked pipeline does. - Concurrent errors compose.
mergereports both sides' failures throughCompositeFailure.fromResults;concurrentlyraises the background error in the foreground. - Cleanup belongs in finalizers. The guide warns that under asynchronous interruption "you should not use
handleErrorWithor other interruptible functions for resource cleanup. UsebracketoronFinalizefor this purpose" (site/guide.md, Appendix A1).
State and sharing
Signal and SignallingRef
Signal[F, A] is "Pure holder of a single value of type A that can be read in the effect F" with get, discrete and continuous (Signal.scala). SignallingRef adds the Ref writers (set, update, modify). The two streams differ precisely:
discreteemits the current value, then each update — but conflated: "when you pull from this stream you may be notified of only the latest update since your last pull. If you want to be notified about every single update, use aQueueorChannelinstead." Implementation: the state isState(value, lastUpdate: Long, listeners: LongMap[Deferred[F, (A, Long)]]); a writer bumpslastUpdate, clears the listener map and completes every listener'sDeferred; a subscriber whoselastSeenis behind returns the current value immediately, otherwise registers a freshDeferredand waits. Each subscription is aStream.bracket(newId)(cleanup), so unsubscription removes the listener.continuousisStream.repeatEval(get)— every pull samples the cell; it never blocks and spins if pulled unthrottled.
discrete does not de-duplicate equal writes; Signal#changes(Eq) derives one that does. Stream#hold(initial) / holdResource convert a stream into a Signal by running evalMap(sig.set) on a background fiber — fs2's analogue of a BehaviorSubject / StateFlow.
Channel, Topic, Queue
Channel— many producers, one consumer ("This method CANNOT be called concurrently by multiple consumers, if you do so, one of the consumers might become permanently deadlocked"),bounded(n),unboundedorsynchronous;closeis graceful (drains enqueued elements first) (Channel.scala). Each pull serves all queued elements as one chunk.Topic— multicast pub/sub with per-subscribermaxQueued; a slow subscriber at its bound "semantically block[s]"publish1for everyone (Topic.scala). There is no replay: late subscribers miss earlier elements.- cats-effect
Queue— work distribution (each element to one consumer);Stream.fromQueueUnterminatedadapts it.
Conflation is available as an operator family (conflateChunks, conflate, conflateSemigroup), each a Channel.bounded(chunkLimit) whose consumer combineAlls what accumulated.
Lifetime and ownership
Lifetime is lexical and checked by construction, which is fs2's strongest property:
Stream.bracket(acquire)(release)/Stream.resource(r: Resource[F, O])register a finalizer on a new scope;bracketCasehands the release anExitCase(Succeeded,Errored(e),Canceled— the last on "either stream interruption or overall compiled effect cancelation") (Stream.scala).- The guarantee. "A stream will never be interrupted while it is acquiring a resource (via
bracket) or while it is releasing a resource. Thebracketfunction guarantees that if FS2 starts acquiring the resource, the corresponding release action will be run." (site/guide.md) onFinalize(f)isStream.bracket(F2.unit)(_ => f) >> this. The…Weakvariants omit the scope so finalization attaches to the enclosing scope — needed, for example, when a stream is turned into aResourcewithcompile.resourceand must outlive the stream's own scope (Stream.scalaL2234).- Concurrent operators do not propagate scopes. "Stream combinators which require
Fto have aConcurrentinstance generally do not propagate open scopes. Finalizers may be run concurrently with subsequent operations in the stream" (Stream.scala) — a resource acquired inside amergebranch is released when that branch's fiber finishes, not when downstream stops using its output. - Fibers are owned by brackets.
concurrently,merge,interruptWhenandparJoineachstarttheir fibers inside aStream.bracketwhose release stops and joins them. The exception found in this survey isdebounce's timer fiber (above). - The edge. A program typically ends in
compile.draininsideIOApp.run; callback-driven code (a GUI toolkit) crosses intoIOwith cats-effect'sDispatcher(Dispatcher.scala), itself aResourcewhose release cancels outstanding work.
Testing
cats-effect's TestControl (TestControl.scala) is the virtual-time harness, and fs2's own suite uses it for every time operator. Per its scaladoc and the test-runtime guide:
- It is a single-threaded mock runtime where "no time is considered to have elapsed as a consequence of ''compute''"; only
IO.sleepadvances the clock. - When several fibers are ready it picks randomly (seeded, reproducible via
seed), "intended to simulate different scheduling interleavings". tickruns all ready fibers without advancing time;tickOneruns one;advance(d)moves the clock;advanceAndTick(d);tickFor(d);tickAllruns to quiescence, jumping the clock to each next timer;nextInterval,results,isDeadlockedobserve.executeEmbed(program)=execute+tickAll+ result, raisingNonTerminationExceptionon deadlock. Interaction with an external thread (IO.asyncwaiting on a foreign callback,evalOn) reads as a deadlock.
fs2's debounce test is the model (StreamCombinatorsSuite.scala):
// core/shared/src/test/scala/fs2/StreamCombinatorsSuite.scala
test("debounce") {
val delay = 200.milliseconds
TestControl.executeEmbed {
(Stream(1, 2, 3) ++ Stream.sleep_[IO](delay * 2) ++ Stream() ++ Stream(4, 5) ++ Stream
.sleep_[IO](delay / 2) ++ Stream(6))
.debounce(delay)
.assertEmits(List(3, 6))
}
}A 400 ms scenario runs in microseconds and asserts exact emissions. For stepwise inspection, TestControl.execute(program) returns the control handle, and the test interleaves advanceAndTick(…) with results. Driving the program's inputs from outside the mock runtime (an outer Ref set between ticks) is explicitly discouraged as "quite tricky"; scripting the inputs inside the program with IO.sleep (as below) is the supported shape.
NOTE
execute builds the test runtime with ctx.deriveBlocking() (TestControl.scala), so IO.blocking appears to run on the mocked single thread rather than a real pool — real file I/O inside a TestControl run takes wall time but no virtual time (reading of the source). Pure fakes for the hash are the reliable choice.
The worked pipeline
Query field → debounce N ms (N adjustable at runtime) → hash the file set with latest-wins cancellation → show the result; a toggle enables the whole pipeline, and switching it off cancels a pending debounce and an in-flight hash. In fs2 3.16 / cats-effect 3.7 (Scala 3 syntax):
import cats.effect.IO
import cats.syntax.all.*
import fs2.Stream
import fs2.concurrent.SignallingRef
import fs2.hashing.{Hash, HashAlgorithm, Hashing}
import fs2.io.file.{Files, Path}
import scala.concurrent.duration.*
enum Result:
case Idle
case Running(query: String)
case Done(query: String, digest: Hash)
case Failed(query: String, error: Throwable)
final case class Inputs(
query: SignallingRef[IO, String], // written by the UI per keystroke
interval: SignallingRef[IO, FiniteDuration], // written by the numeric field
enabled: SignallingRef[IO, Boolean] // written by the toggle
)
// 64 KiB `F.blocking` read + `Sync.delay` digest update per chunk: a cancel point every chunk.
def hashFileSet(paths: Stream[IO, Path]): IO[Hash] =
paths
.flatMap(p => Files[IO].readAll(p))
.through(Hashing[IO].hash(HashAlgorithm.SHA256))
.compile
.lastOrError
def pipeline(in: Inputs, resolve: String => Stream[IO, Path]): Stream[IO, Result] =
in.enabled.discrete.changes.switchMap {
case false => Stream.emit(Result.Idle)
case true =>
in.query.discrete.changes.switchMap { q =>
// Trailing debounce: every keystroke restarts this inner stream, and the
// interval is read when the timer is armed, so a change applies from the next keystroke.
Stream.exec(in.interval.get.flatMap(IO.sleep)) ++
Stream.emit(Result.Running(q)) ++
Stream.eval(hashFileSet(resolve(q)).attempt).map {
case Right(h) => Result.Done(q, h)
case Left(e) => Result.Failed(q, e)
}
}
}
// UI edge: the frame reads `latest.get` (via a Dispatcher) and is woken when it changes.
def run(in: Inputs, resolve: String => Stream[IO, Path], latest: SignallingRef[IO, Result], wake: IO[Unit]): IO[Unit] =
pipeline(in, resolve).foreach(r => latest.set(r) >> wake).compile.drainThe built-in debounce is deliberately not used: its duration is a fixed FiniteDuration and it does not restart on input (Time and scheduling). The switchMap + sleep idiom gives a true trailing debounce, and the same switchMap provides latest-wins for the hash, so one operator owns both cancellations.
Trace (N = 300 ms, enabled = true):
- Keystroke during debounce. t=0
query.set("a"):discretewakes the outer-innerswitchMap, which starts inner"a", now inIO.sleep(300ms). t=120query.set("ab"):haltedFcompletes"a"'shalt; inner"ab"blocks onguard.acquire."a"'s interruptible scope races thesleepagainsthalt, cancels the sleep (no finalizers), closes, releases the permit;"ab"starts its own 300 ms sleep. Nothing reaches the UI. Keystrokes faster than the subscriber pulls are conflated bydiscrete, which is harmless here. - Keystroke during hash. t=420
"ab"emitsRunning("ab")and starts the hash. t=500query.set("abc"):"ab"is halted while insideStream.eval(hashFileSet(…)); the race cancels thatIO. Its innercompileobserves cancellation at the next run-loop boundary — after the current 64 KiBF.blockingread ordelaydigest update returns — then releases the file handle (thereadAllresource) and theHasher.cancelblocks until that is done; then"abc"acquires the permit and starts a fresh 300 ms timer. The UI still showsRunning("ab")until t≈800, whenRunning("abc")arrives. - Interval change.
interval.set(1.second)emits nothing and restarts nothing: the running inner already read 300 ms. The next keystroke's timer uses 1 s. Restarting on an interval change instead means switching on the pair —(in.query, in.interval).tupled.discretethroughSignal'sApplicative— at the cost of the non-deterministic zip noted under Evaluation model. - Toggle off.
enabled.set(false): the outerswitchMaphalts the wholetruebranch, an interruptible scope containing the innerswitchMap, itsparJoin(2)fibers, any pendingsleepand any in-flight hash. Interruption propagates down the scope tree; every finalizer runs, children first; when the last permit is released thefalsebranch emitsResult.Idle. A pending debounce never fires and a half-hashed result is never emitted.
Fit for a per-frame UI. The pipeline itself fits well: latest-wins, the wait-for-teardown guarantee, scoped resources and virtual-time tests are exactly what sparkles wants. The edges fit less well. The output is push-shaped (foreach), so an immediate-mode frame needs a cell (latest: SignallingRef) read each frame plus an explicit wake, and every UI write crosses the effect boundary through a Dispatcher (unsafeRunAndForget(in.query.set(text))). There is no frame clock and no glitch-free derived state: anything the widget tree derives (enabled-and-non-empty, say) is either recomputed per frame from gets or becomes another asynchronous stream. And cancellation latency is "one effect step", so CPU work must be chunked or ceded to be responsive.
Strengths
- One interpreter, one cancellation story. Every operator reduces to
Pull+ scopes, and every concurrent operator reduces to cats-effect fibers whosecancelbackpressures, so "does cancel wait?" has one answer everywhere it was checked: yes. switchMapserialises teardown. The next inner never overlaps the previous one's finalizers — the property a latest-wins file hash needs so two hashes never contend for the same files or worker.- Resource safety by construction.
bracketacquire/release is uninterruptible and always paired; scopes form a tree that closes children first; fibers are owned by brackets. - Chunking gives cheap, regular cancel points.
readAll's 64 KiB chunks and per-chunk digest updates make a large hash cancellable within one chunk without usercedes. - Deterministic virtual-time tests via
TestControl.executeEmbed, with randomised interleavings that surface ordering bugs. Pullas the extension mechanism — a user-written operator has exactly the power of a library one.
Weaknesses
debounceis misnamed for UI use: fixed duration, no restart on input, last-of-chunk, and an unscoped timer fiber.- No glitch-freedom and no dependency graph.
Signalcombination is a non-deterministic zip; derived state is a stream, not a memoised node. - Concurrent operators drop scopes. Resources acquired under
merge/parJoinare finalised when the producing fiber ends, which surprises users (documented, but a sharp edge). - Heavy machinery per operator.
switchMapallocates aSemaphore, aRef, aDeferredper element and runs throughparJoin(2);mergeallocates aSignallingRef, a synchronousChanneland three fibers. Fine on the JVM, notable for a@nogcdesign. Throwableis the only error channel, and interruption is a third outcome that error handlers do not see.- Cooperative cancellation is coarse at the edges.
blockingis uncancelable,interruptiblerelies on JVM thread interrupts, and adelaythunk runs to completion; responsiveness depends on how finely work is chunked.
Key design decisions and trade-offs
| Decision | Rationale | Trade-off |
|---|---|---|
Pull-based evaluation over a free Pull monad | Consumer controls effects; back-pressure for free; one interpreter to verify | Hot, push-shaped sources (UI events) must be adapted through Signal/Channel/Queue |
| Chunks are the atomic unit of emission and interruption | Unboxed arrays, amortised interpreter overhead, well-defined interrupt points | Chunk structure leaks into semantics (debounce sees only hd.last, laws hold up to ===) |
Interruptible scope tree; Eval raced against interrupt | Interrupt in-flight effects, not just between steps; children close first | A Deferred and a forked listener fiber per interruptible child scope |
cancel backpressures until finalizers finish (cats-effect) | No overlapping lifetimes; switchMap can promise teardown-before-start | Cancel latency = longest uncancelable region (a blocking read, a long delay) |
switchMap = evalMap(halt previous) + one-permit semaphore + parJoin(2) | Reuses existing pieces; serialises inner lifetimes | Per-element allocation; an erroring inner deliberately never releases the permit |
Time via Temporal only, durations as FiniteDuration args | Effect-polymorphic; mockable by TestControl | No runtime-variable debounce/metered; no frame clock |
| Concurrent combinators do not propagate scopes | Lets fibers finish and release independently | Resources from merge/parJoin branches may close before downstream is done with them |
Signal.discrete conflates | O(1) state, a slow subscriber never blocks writers | Every-update consumers need a Channel/Queue; no glitch-free combination |
Sources
- fs2 source at
f679d5b45054:Stream.scala,Pull.scala,internal/Scope.scala,internal/InterruptContext.scala,concurrent/Signal.scala,concurrent/Channel.scala,concurrent/Topic.scala,hashing/Hashing.scala,HasherCompanionPlatform.scala,io/file/Files.scala,FileHandlePlatform.scala,build.sbt,README.md,LICENSE. - fs2 tests:
StreamCombinatorsSuite.scala,StreamSwitchMapSuite.scala. - fs2 guide:
site/guide.md(fs2.io "Guide", incl. Appendix A1 on interruption),site/concurrency-primitives.md; fs2.io. - cats-effect source at
de84c01f76b2:IOFiber.scala,IO.scala,GenSpawn.scala,MonadCancel.scala,Sync.scala,TestControl.scala,IORuntimeConfigCompanionPlatform.scala,Dispatcher.scala,build.sbt,LICENSE.txt. - cats-effect docs:
docs/core/test-runtime.md(rendered),docs/typeclasses/monadcancel.md; typelevel.org/cats-effect. - Related deep-dives: Cats Effect (Scala), ZIO (Scala), RxJS, Kotlin Flow, Effect-TS, Rust streams; the umbrella index, concepts, comparison; async I/O survey.