{"id":"7ab6a6940b4c62ed","repo":"junit-team/junit5","slug":"scheduled-executor-could-not-be-stopped-in-an-orde","errorCode":null,"errorMessage":"Scheduled executor could not be stopped in an orderly manner","messagePattern":"Scheduled executor could not be stopped in an orderly manner","errorType":"exception","errorClass":"JUnitException","httpStatus":null,"severity":"error","filePath":"junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TimeoutInvocationFactory.java","lineNumber":71,"sourceCode":"\tabstract static class ExecutorResource implements Store.CloseableResource, AutoCloseable {\n\n\t\tprivate final ScheduledExecutorService executor;\n\n\t\tExecutorResource(ScheduledExecutorService executor) {\n\t\t\tthis.executor = executor;\n\t\t}\n\n\t\tScheduledExecutorService get() {\n\t\t\treturn executor;\n\t\t}\n\n\t\t@Override\n\t\tpublic void close() throws Exception {\n\t\t\texecutor.shutdown();\n\t\t\tboolean terminated = executor.awaitTermination(5, TimeUnit.SECONDS);\n\t\t\tif (!terminated) {\n\t\t\t\texecutor.shutdownNow();\n\t\t\t\tthrow new JUnitException(\"Scheduled executor could not be stopped in an orderly manner\");\n\t\t\t}\n\t\t}\n\t}\n\n\t@SuppressWarnings(\"try\")\n\tstatic class SingleThreadExecutorResource extends ExecutorResource {\n\n\t\t@SuppressWarnings({ \"unused\", \"ThreadPriorityCheck\" })\n\t\tSingleThreadExecutorResource() {\n\t\t\tsuper(Executors.newSingleThreadScheduledExecutor(runnable -> {\n\t\t\t\tThread thread = new Thread(runnable, \"junit-jupiter-timeout-watcher\");\n\t\t\t\tthread.setPriority(Thread.MAX_PRIORITY);\n\t\t\t\treturn thread;\n\t\t\t}));\n\t\t}\n\t}\n\n}","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/junit-team/junit5/blob/956246301e03d757539f7b76dd5a91f298637563/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TimeoutInvocationFactory.java#L53-L89","documentation":"Thrown by TimeoutInvocationFactory.ExecutorResource.close() when the internal 'junit-jupiter-timeout-watcher' single-thread ScheduledExecutorService does not terminate within 5 seconds of calling shutdown(). After the grace period, shutdownNow() is called and this JUnitException is raised. This executor is used for same-thread timeout enforcement (ThreadMode.SAME_THREAD) to schedule the timeout watcher task.","triggerScenarios":"The executor's scheduled task (the timeout watcher) does not complete within 5 seconds of JUnit shutting down the test context. This can happen if the JVM is under heavy load, if the watched thread is stuck in an uninterruptible operation, or if the test harness is being forcefully torn down.","commonSituations":"Running in a heavily loaded CI environment, a test that hangs in a way that blocks the watcher thread, OOM or resource exhaustion during teardown, or running on a very slow/containerized machine where thread scheduling is delayed beyond 5 seconds.","solutions":["Rerun the test in isolation to rule out transient CI load","Use ThreadMode.SEPARATE_THREAD (@Timeout(threadMode = ThreadMode.SEPARATE_THREAD)) which does not use this same-thread executor","Ensure the test JVM has adequate CPU/memory resources","Check for tests that hang in uninterruptible blocking operations (e.g., native I/O, infinite loops)"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// This error occurs during context teardown, not in user code.\n// Mitigate by using SEPARATE_THREAD mode to avoid the same-thread executor:\n@Timeout(value = 5, unit = TimeUnit.SECONDS, threadMode = ThreadMode.SEPARATE_THREAD)","preventionTips":["Use @Timeout(threadMode = ThreadMode.SEPARATE_THREAD) to avoid the same-thread executor entirely","Ensure CI environments have adequate CPU/memory for the JVM","Avoid tests that hang in uninterruptible blocking operations","Rerun flaky tests to distinguish transient CI load from persistent issues"],"tags":["timeout","executor","teardown","ci-environment","junit-jupiter-engine"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}