Skip to content

Commit 491a329

Browse files
authored
Merge pull request scala/scala#10379 from szeiger/wip/future-interrupt
[backport] InterruptedException handling for Futures
2 parents 7d2b16b + 2276c4a commit 491a329

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,19 @@ 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 completeWithFailure(p: Promise[_], t: Throwable): Unit = {
32+
if (NonFatal(t)) p.complete(Failure(t))
33+
else if (t.isInstanceOf[InterruptedException]) {
34+
if (p.tryComplete(Failure(new ExecutionException("Boxed InterruptedException", t))))
35+
Thread.currentThread.interrupt()
36+
} else throw t
37+
}
38+
3139
override def transform[S](f: Try[T] => Try[S])(implicit executor: ExecutionContext): Future[S] = {
3240
val p = new DefaultPromise[S]()
33-
onComplete { result => p.complete(try f(result) catch { case NonFatal(t) => Failure(t) }) }
41+
onComplete { result =>
42+
try { p.complete(f(result)) } catch { case t: Throwable => completeWithFailure(p, t) }
43+
}
3444
p.future
3545
}
3646

@@ -42,7 +52,7 @@ private[concurrent] trait Promise[T] extends scala.concurrent.Promise[T] with sc
4252
case fut if fut eq this => p complete v.asInstanceOf[Try[S]]
4353
case dp: DefaultPromise[_] => dp.asInstanceOf[DefaultPromise[S]].linkRootOf(p)
4454
case fut => p completeWith fut
45-
} catch { case NonFatal(t) => p failure t }
55+
} catch { case t: Throwable => completeWithFailure(p, t) }
4656
}
4757
p.future
4858
}

0 commit comments

Comments
 (0)