typelevel / typelevel/fs2

switchMap stops working on a Signal#discrete made out of Applicative[Signal]

Open
#1,829 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
2.5k
Forks
636
Avg merge
2d 4h
Merged PRs (30d)
7

Description

fs2-core 2.3.0, Scala 2.13.1, Scala.js 0.6.23

Ran into a very weird issue:

  • If a signal is made with some Cartesian operator like mapN or product, and
  • a .discrete stream of it's values is switchMapped into something, and
  • the value returned in switchMap is a non-terminating stream

Then the second inner stream will never be switched from, i.e. function passed to switchMap will be called at most twice.


E.g. the following code:

def run = for {
  sig <- SignallingRef[IO, Int](0)
  _ <- fs2.Stream.awakeEvery[IO](100.millis).as(1).scanMonoid.take(20)
          .evalMap(sig.set)
          .compile.drain.start
          
  test = (Signal.constant[IO, String](">> "), sig).mapN(_ ++ _.toString)
    
  _ <- test.discrete.switchMap { x =>
          fs2.Stream.eval_(IO(println(s"Opening: $x"))) ++
          fs2.Stream.never[IO].onFinalize(IO(println(s"Closing $x")))
        }
        .compile
        .drain
        .start
  } yield ()

run.unsafeRunAsyncAndForget()

Produces the following output:

Opening: >> 0
Closing >> 0
Opening: >> 1

Removing the signal altogether:

def run = fs2.Stream.awakeEvery[IO](100.millis)
  .as(1).scanMonoid.take(20)
  .switchMap { x =>
    fs2.Stream.eval_(IO(println(s"Opening: $x"))) ++
    fs2.Stream.never[IO].onFinalize(IO(println(s"Closing $x")))
  }
  .compile.drain
  .start.void

Or replacing the test signal definition with test = sig.map(">> " + _) produces the following output (expected):

Opening: >> 0
Closing >> 0
Opening: >> 1
...<many similar lines>...
Closing >> 18
Opening: >> 19

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the minimal reproducer against fs2-core 2.3.0, focusing on Signal#discrete, the Applicative[Signal] mapN construction, and switchMap with a non-terminating inner stream. Compare it with the direct signal and map-based variants shown in the issue. Done means updates from the combined signal switch to each new inner stream, with the expected opening and closing output.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
stream-processing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.