apache/flink · error · TimeoutException

Could not finish execution of tasks within time.

Error message

Could not finish execution of tasks within time.

What it means

The timed invokeAny(tasks, timeout, unit) of DirectExecutorService iterates callables while wall-clock time remains. If the deadline passes with tasks still untried, it throws TimeoutException with this message — note the already-tried tasks' failures are discarded in this branch.

Source

Thrown at flink-core/src/main/java/org/apache/flink/util/concurrent/DirectExecutorService.java:237

        long end = System.currentTimeMillis() + unit.toMillis(timeout);
        Exception exception = null;

        Iterator<? extends Callable<T>> iterator = tasks.iterator();

        while (end > System.currentTimeMillis() && iterator.hasNext()) {
            Callable<T> callable = iterator.next();

            try {
                return callable.call();
            } catch (Exception e) {
                // ignore exception and try next
                exception = e;
            }
        }

        if (iterator.hasNext()) {
            throw new TimeoutException("Could not finish execution of tasks within time.");
        } else {
            throw new ExecutionException("No tasks finished successfully.", exception);
        }
    }

    @Override
    public void execute(@Nonnull Runnable command) {
        throwRejectedExecutionExceptionIfShutdown();

        command.run();
    }

    private void throwRejectedExecutionExceptionIfShutdown() {
        if (isShutdown() && triggerRejectedExecutionException) {
            throw new RejectedExecutionException(
                    "The ExecutorService is shut down already. No Callables can be executed.");
        }
    }

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Increase the timeout to cover the sum of worst-case task durations, since DirectExecutorService executes them sequentially.
  2. Reduce per-task latency or the number of tasks.
  3. If parallel attempts are required, use a real thread-pool executor instead of DirectExecutorService.

Example fix

// before
T r = directExecutor.invokeAny(tasks, 1, TimeUnit.SECONDS);

// after
T r = directExecutor.invokeAny(tasks, 30, TimeUnit.SECONDS);
Defensive patterns

Strategy: validation

Validate before calling

long worstCaseMs = tasks.stream().mapToLong(t -> estimateMs(t)).sum();
if (worstCaseMs > unit.toMillis(timeout)) throw new IllegalStateException("Timeout below sequential worst case");

Try / catch

catch (TimeoutException e) { /* retry with larger budget or fall back */ }

Prevention

When it happens

Trigger: Calling invokeAny(tasks, timeout, unit) where each callable.call() is slow (they run on the caller thread, so their time counts against the deadline) and the loop exits due to time before reaching a success or exhausting the list.

Common situations: Code assuming invokeAny runs tasks in parallel (with a direct executor they are sequential, so total time is the sum); tight timeouts with several slow callables; blocking I/O inside callables.

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/573480c84aabf28c. Report an issue: GitHub.