parfeval and cancel: When does a worker become available after cancelling a Future?
0 reputation · 23 Jun 2020, 14:02 UTC
When using parfeval to run long computations asynchronously, a common pattern is to enforce a wall-clock limit by polling the State property of the returned Future object and calling cancel once a timeout threshold is exceeded. The cancel function is documented to terminate execution on the worker and transition the future to the cancelled state, but the exact point at which the underlying worker process releases resources and becomes eligible for new tasks is not clearly bounded.
This ambiguity matters when the worker is executing a non-interruptible built-in C/C++ routine, or when the main thread uses pause inside a polling loop, because MATLAB's single-threaded event queue can delay state observation. Without a deterministic boundary between the cancelled state and actual worker availability, capacity planning for a fixed-size parallel pool becomes uncertain.
Assuming a current MATLAB release with Parallel Computing Toolbox, is there a documented guarantee that a worker is immediately reusable after cancel returns, or must the State property be polled further to confirm cleanup? Does the presence of a non-interruptible built-in function change the timing of resource release? If fetchNext is used to collect cancelled futures from a queue, does it provide a stronger signal of worker readiness than checking State directly?