Skip to content

Commit

Permalink
Interrupt current thread after completing Future
Browse files Browse the repository at this point in the history
  • Loading branch information
szeiger committed Jun 2, 2023
1 parent cb7862f commit a446709
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/library/scala/concurrent/impl/Promise.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ private[concurrent] trait Promise[T] extends scala.concurrent.Promise[T] with sc
import scala.concurrent.Future
import scala.concurrent.impl.Promise.DefaultPromise

private[this] final def wrapFailure(t: Throwable): Throwable = {
if (NonFatal(t)) t
else if (t.isInstanceOf[InterruptedException]) new ExecutionException("Boxed InterruptedException", t)
else throw t
private[this] final def completeWithFailure(p: Promise[_], t: Throwable): Unit = {
if (NonFatal(t)) p.complete(Failure(t))
else if (t.isInstanceOf[InterruptedException]) {
if (p.tryComplete(Failure(new ExecutionException("Boxed InterruptedException", t))))
Thread.currentThread.interrupt()
} else throw t
}

override def transform[S](f: Try[T] => Try[S])(implicit executor: ExecutionContext): Future[S] = {
val p = new DefaultPromise[S]()
onComplete { result => p.complete(try f(result) catch { case t: Throwable => Failure(wrapFailure(t)) }) }
onComplete { result =>
try { p.complete(f(result)) } catch { case t: Throwable => completeWithFailure(p, t) }
}
p.future
}

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

0 comments on commit a446709

Please sign in to comment.