Skip to content

Commit 2276c4a

Browse files
committed
Interrupt current thread after completing Future
1 parent f404ff1 commit 2276c4a

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +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 wrapFailure(t: Throwable): Throwable = {
32-
if (NonFatal(t)) t
33-
else if (t.isInstanceOf[InterruptedException]) new ExecutionException("Boxed InterruptedException", t)
34-
else throw t
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
3537
}
3638

3739
override def transform[S](f: Try[T] => Try[S])(implicit executor: ExecutionContext): Future[S] = {
3840
val p = new DefaultPromise[S]()
39-
onComplete { result => p.complete(try f(result) catch { case t: Throwable => Failure(wrapFailure(t)) }) }
41+
onComplete { result =>
42+
try { p.complete(f(result)) } catch { case t: Throwable => completeWithFailure(p, t) }
43+
}
4044
p.future
4145
}
4246

@@ -48,7 +52,7 @@ private[concurrent] trait Promise[T] extends scala.concurrent.Promise[T] with sc
4852
case fut if fut eq this => p complete v.asInstanceOf[Try[S]]
4953
case dp: DefaultPromise[_] => dp.asInstanceOf[DefaultPromise[S]].linkRootOf(p)
5054
case fut => p completeWith fut
51-
} catch { case t: Throwable => p.failure(wrapFailure(t)) }
55+
} catch { case t: Throwable => completeWithFailure(p, t) }
5256
}
5357
p.future
5458
}

0 commit comments

Comments
 (0)