Skip to content

Commit 893d05d

Browse files
authored
Optimize cancellable fork when it's cancelled before starting (#338)
Closes #161
1 parent d481b20 commit 893d05d

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

core/src/main/scala/ox/fork.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import java.util.concurrent.Semaphore
77
import java.util.concurrent.atomic.AtomicBoolean
88
import scala.concurrent.ExecutionException
99
import scala.util.control.NonFatal
10+
import scala.util.control.NoStackTrace
1011

1112
/** Starts a fork (logical thread of execution), which is guaranteed to complete before the enclosing [[supervised]] or [[supervisedError]]
1213
* block completes.
@@ -199,7 +200,9 @@ def forkCancellable[T](f: => T)(using OxUnsupervised): CancellableFork[T] =
199200
// will cause the scope to end, interrupting the task if it hasn't yet finished (or potentially never starting it)
200201
done.release()
201202
if !started.getAndSet(true)
202-
then result.completeExceptionally(new InterruptedException("fork was cancelled before it started")).discard
203+
then result.completeExceptionally(new InterruptedException("fork was cancelled before it started") with NoStackTrace).discard
204+
// #161: not generating the stack trace in case the task was cancelled before it started, as this isn't going to
205+
// contain any useful information anyway (no user code was executed), and it provides better performance
203206
end new
204207
end forkCancellable
205208

0 commit comments

Comments
 (0)