typelevel / typelevel/fs2

Scope lookup failure when using concurrently/merge/parJoinUnbounded

Open
#1,679 0 comments 0 reactions 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

Brought up by @augustjune on Gitter today:

@ def holdLater[F[_]: Concurrent, A](stream: Stream[F, A]): Stream[F, Signal[F, A]] = {
    def uncons1(stream: Stream[F, A]): Stream[F, (A, Stream[F, A])] =
      stream.pull.uncons1.flatMap {
        case Some((a, rest)) => Pull.output1(a -> rest)
        case None            => Pull.done
      }.stream

    uncons1(stream).flatMap { case (h, t) =>
      Stream.eval(SignallingRef[F, A](h)).flatMap { sig =>
        Stream(sig).concurrently(t.evalMap(sig.set))
      }
    }
  }
defined function holdLater

@ holdLater(Stream(1, 2, 3).covary[IO].metered(1.second)).flatMap(_.discrete).take(3).compile.toList.unsafeToFuture
res19: scala.concurrent.Future[List[Int]] = Future(<not completed>)

@ res19
res20: scala.concurrent.Future[List[Int]] = Future(Failure(java.lang.Throwable: Fail to find scope for next step: current: Token(7e32dc50), step: Step(FreeC.Bind(FreeC.Bind(Step(FreeC.Bind(Eval(IO$1895120358)),None))),Some(Token(74cab720)))))

Replacing concurrently with merge or parJoinUnbounded results in the same error.

Note metered is implemented with zipRight, which uses stepLeg. Replacing metered with a simple Stream(1, 2, 3).zipRight(Stream(1, 2, 3)) shows the same problem. I suspect the issue is that uncons1 returns the tail as a stream element, which breaks scoping.

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 with the reproducer in the issue, then trace holdLater's uncons1 through concurrently, merge, and parJoinUnbounded. Compare the metered and zipRight paths, including stepLeg, to understand how the returned tail stream affects scope lookup. Done means the reproducer completes without the "Fail to find scope" error for all affected combinators.

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
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.