Skip to content

Commit f404ff1

Browse files
committed
InterruptedException handling for Futures
This fixes scala/bug#8938 for Scala 2.12 in a way that is compatible with the behavior of 2.13 introduced in 4a4dd1d. - Relevant test suite parts taken verbatim from the 2.13 version, with one exception: I did not modify the messages of wrapped exceptions, therefore PromiseTests expects the name "Boxed InterruptedException" (as used in `Promise.resolver` in 2.12) instead of "Boxed Exception". - The actual implementation is not based on 2.13, which was a major rewrite that cannot be applied cleanly to 2.12 and is not fully source or binary compatible. Instead I implemented a strategic fix in as simple a way as possible.
1 parent 10f6889 commit f404ff1

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

library/src/scala/concurrent/impl/Promise.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@ private[concurrent] trait Promise[T] extends scala.concurrent.Promise[T] with sc
2828
import scala.concurrent.Future
2929
import scala.concurrent.impl.Promise.DefaultPromise
3030

31+
private[this] final def wrapFailure(t: Throwable): Throwable = {
32+
if (NonFatal(t)) t
33+
else if (t.isInstanceOf[InterruptedException]) new ExecutionException("Boxed InterruptedException", t)
34+
else throw t
35+
}
36+
3137
override def transform[S](f: Try[T] => Try[S])(implicit executor: ExecutionContext): Future[S] = {
3238
val p = new DefaultPromise[S]()
33-
onComplete { result => p.complete(try f(result) catch { case NonFatal(t) => Failure(t) }) }
39+
onComplete { result => p.complete(try f(result) catch { case t: Throwable => Failure(wrapFailure(t)) }) }
3440
p.future
3541
}
3642

@@ -42,7 +48,7 @@ private[concurrent] trait Promise[T] extends scala.concurrent.Promise[T] with sc
4248
case fut if fut eq this => p complete v.asInstanceOf[Try[S]]
4349
case dp: DefaultPromise[_] => dp.asInstanceOf[DefaultPromise[S]].linkRootOf(p)
4450
case fut => p completeWith fut
45-
} catch { case NonFatal(t) => p failure t }
51+
} catch { case t: Throwable => p.failure(wrapFailure(t)) }
4652
}
4753
p.future
4854
}

0 commit comments

Comments
 (0)