Open achyan opened 10 months ago
We have a "scala/collections" team of volunteers we can summon on collections stuff. This ticket is a good example of why we ought to assemble a similar "scala/concurrency" team, as I bet there are people out there who might have some insight into this, but they apparently aren't watching scala/bug.
This can be configured with the default forkjoinpool in Java 9 iirc.
The behavior is because sequence
must execute on the execution context. The weird failure is due to the poorly configured EC.
Here is a better look:
// make it easier to see when the error happens
val threadPool: ExecutionContext = ExecutionContext.fromExecutor(threadPoolExecutor, t => println(s"ERROR ${t.getMessage}"))
// helper to pass in a specific EC
def seqtest[A, CC[X] <: IterableOnce[X], To](in: CC[Future[A]])(ec: ExecutionContext)(implicit bf: BuildFrom[CC[Future[A]], A, To]): Future[To] = Future.sequence(in)(bf, ec)
val fs = Try(seqtest(Seq(future1, future3))(global))
val gs = Try(seqtest(Seq(future3, future1))(global))
val hs = Try(seqtest(Seq(future1, future3))(threadPool))
val is = Try(seqtest(Seq(future3, future1))(threadPool))
You'll see ERROR Task Future(<not completed>)
due to the failed sequence
.
The sequence on global will print the expected failure twice:
Success(Future(Failure(java.util.concurrent.RejectedExecutionException: Task Future(<not completed>)
The bad sequence will print:
Success(Future(<not completed>))
for the first one, "1 then 3". The reason is that "3 then 1" is an error right away, but "1 then 3" has to submit an extra job to combine the results, and that job fails.
The other workaround is to use ExecutionContext.parasitic
for the sequence.
The question is whether this is the same problem as https://github.com/scala/bug/issues/9071.
After all that verbiage, it seems to be a regression in https://github.com/scala/scala/pull/9655. It works on 2.13.6 or stubbing Haoyi's override.
Reproduction steps
Scala version: 2.13.12
Problem
I would've expected Future.sequence(Seq(future3, future1)) to match Future.sequence(Seq(future1, future3)) in either both returning an exception or both returning a failed future.
Also, with Scala 2.12, we would get the RejectedExecutionException when we submitted the 3rd future that exceeded the thread pool's queue limit. With Scala 2.13, we now get back a Future(Failure(ex)) instead of the exception. If that's the case, I would've expected the Future.sequence to also not throw an exception.