Skip to content

Commit 206e2a3

Browse files
committed
Avoid catching fatal errors as a Failed future
scala-async 0.9.6 regressed and started catching fatal exceptions. For 1.0.0, we're fixing this regression and harmonizing the behaviour with direct use of the Future combinators.
1 parent 8c6deae commit 206e2a3

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/main/scala/scala/async/FutureStateMachine.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import java.util.Objects
1515

1616
import scala.util.{Failure, Success, Try}
1717
import scala.concurrent.{ExecutionContext, Future, Promise}
18+
import scala.util.control.NonFatal
1819

1920
/** The base class for state machines generated by the `scala.async.Async.async` macro.
2021
* Not intended to be directly extended in user-written code.
@@ -34,15 +35,14 @@ abstract class FutureStateMachine(execContext: ExecutionContext) extends Functio
3435
/** Assign `i` to the state variable */
3536
protected def state_=(s: Int): Unit = state$async = s
3637

38+
NonFatal // eagerly classloading NonFatal to reduce the chance of a cascading StackOverflowError in `completeFailure`
39+
3740
/** Complete the state machine with the given failure. */
38-
protected def completeFailure(t: Throwable): Unit = {
39-
//
40-
// TODO https://github.com/scala/scala-async/issues/243
41-
//
42-
// scala-async accidentally started catching NonFatal exceptions in:
43-
// https://github.com/scala/scala-async/commit/e3ff0382ae4e015fc69da8335450718951714982#diff-136ab0b6ecaee5d240cd109e2b17ccb2R411
44-
// This follows the new behaviour but should we fix the regression?
45-
result$async.complete(Failure(t))
41+
protected def completeFailure(t: Throwable): Unit = t match {
42+
case NonFatal(t) =>
43+
result$async.complete(Failure(t))
44+
case _ =>
45+
throw t
4646
}
4747

4848
/** Complete the state machine with the given value. */

src/test/scala/scala/async/ExceptionalTest.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ class ExceptionalTest {
1818
}
1919

2020
@Test
21-
@Ignore // TODO https://github.com/scala/scala-async/issues/243
2221
def nonFatalNotCaughtAsync(): Unit = {
2322
check { implicit ec =>
2423
async {

0 commit comments

Comments
 (0)