scala.concurrent.Future.firstCompletedOf and java.util.concurrent.Future.cancel timeout integration
0 reputation · 29 Mar 2021, 14:14 UTC
Goal is to combine a long-running scala.concurrent.Future with a timeout signal using Future.firstCompletedOf, where the timeout future completes with a failure after a fixed duration. The pattern is intended to work across Scala 2.13 and Scala 3 standard library.
Constraints involve reliance on the underlying Executor for scheduling the timeout future, and the fact that scala.concurrent.Future does not propagate cancellation automatically to the running computation. java.util.concurrent.Future.cancel can be requested on the underlying task but is not exposed through the standard Future API, and library-specific cancelable wrappers exist outside the standard library.
The unresolved decision is how to treat the original operation after the timeout future wins firstCompletedOf, given that cancellation is not guaranteed and resource cleanup depends on task cooperation and executor behavior.
- Does firstCompletedOf guarantee that the losing future remains runnable on the executor after the timeout wins?
- Is there a documented way to link java.util.concurrent.Future.cancel to a scala.concurrent.Future created with Future { ... }?
- Should timeout handling be considered complete once the timeout future completes, or does the design require explicit abandonment of the original future?