{"record":{"id":"573480c84aabf28c","repo":"apache/flink","slug":"could-not-finish-execution-of-tasks-within-time","errorCode":null,"errorMessage":"Could not finish execution of tasks within time.","messagePattern":"Could not finish execution of tasks within time\\.","errorType":"exception","errorClass":"TimeoutException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/util/concurrent/DirectExecutorService.java","lineNumber":237,"sourceCode":"\n        long end = System.currentTimeMillis() + unit.toMillis(timeout);\n        Exception exception = null;\n\n        Iterator<? extends Callable<T>> iterator = tasks.iterator();\n\n        while (end > System.currentTimeMillis() && iterator.hasNext()) {\n            Callable<T> callable = iterator.next();\n\n            try {\n                return callable.call();\n            } catch (Exception e) {\n                // ignore exception and try next\n                exception = e;\n            }\n        }\n\n        if (iterator.hasNext()) {\n            throw new TimeoutException(\"Could not finish execution of tasks within time.\");\n        } else {\n            throw new ExecutionException(\"No tasks finished successfully.\", exception);\n        }\n    }\n\n    @Override\n    public void execute(@Nonnull Runnable command) {\n        throwRejectedExecutionExceptionIfShutdown();\n\n        command.run();\n    }\n\n    private void throwRejectedExecutionExceptionIfShutdown() {\n        if (isShutdown() && triggerRejectedExecutionException) {\n            throw new RejectedExecutionException(\n                    \"The ExecutorService is shut down already. No Callables can be executed.\");\n        }\n    }","sourceCodeStart":219,"sourceCodeEnd":255,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/util/concurrent/DirectExecutorService.java#L219-L255","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Increase the timeout to cover the sum of worst-case task durations, since DirectExecutorService executes them sequentially.","Reduce per-task latency or the number of tasks.","If parallel attempts are required, use a real thread-pool executor instead of DirectExecutorService."],"exampleFix":"// before\nT r = directExecutor.invokeAny(tasks, 1, TimeUnit.SECONDS);\n\n// after\nT r = directExecutor.invokeAny(tasks, 30, TimeUnit.SECONDS);","handlingStrategy":"validation","validationCode":"long worstCaseMs = tasks.stream().mapToLong(t -> estimateMs(t)).sum();\nif (worstCaseMs > unit.toMillis(timeout)) throw new IllegalStateException(\"Timeout below sequential worst case\");","typeGuard":null,"tryCatchPattern":"catch (TimeoutException e) { /* retry with larger budget or fall back */ }","preventionTips":["With a direct executor, tasks run sequentially — budget the SUM of durations.","Move parallel attempts to a real thread pool.","Keep callables non-blocking or short when under a direct executor."],"tags":["concurrency","executor","timeout","invoke-any"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}