{"record":{"id":"320125dc3bf018e7","repo":"karatelabs/karate","slug":"cannot-reset-while-shutting-down","errorCode":null,"errorMessage":"cannot reset while shutting down","messagePattern":"cannot reset while shutting down","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/common/KarateLifecycle.java","lineNumber":227,"sourceCode":"\n    /** Where the register currently is — see {@link Phase}. */\n    public static Phase phase() {\n        synchronized (LOCK) {\n            return phase;\n        }\n    }\n\n    /**\n     * Return the register to {@link Phase#RUNNING} so it accepts registrations again. Meant for\n     * tests, and for an application that deliberately restarts Karate inside a live JVM; a normal\n     * shutdown never needs it. Anything left over from the previous life is dropped, not stopped.\n     *\n     * @throws IllegalStateException if a shutdown is in flight\n     */\n    public static void reset() {\n        synchronized (LOCK) {\n            if (phase == Phase.SHUTTING_DOWN) {\n                throw new IllegalStateException(\"cannot reset while shutting down\");\n            }\n            phase = Phase.RUNNING;\n            results = List.of();\n        }\n        synchronized (REGISTERED) {\n            REGISTERED.clear();\n        }\n    }\n\n    /**\n     * Stop everything registered, in reverse registration order, bounded by\n     * {@link #DEFAULT_TIMEOUT}.\n     */\n    public static List<StopResult> shutdownAll() {\n        return shutdownAll(DEFAULT_TIMEOUT);\n    }\n\n    /**","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/common/KarateLifecycle.java#L209-L245","documentation":"KarateLifecycle.reset() restores the singleton lifecycle to its initial RUNNING state (clearing results and registrations). It refuses to run when the lifecycle is currently in the SHUTTING_DOWN phase, throwing IllegalStateException — resetting mid-shutdown would resurrect state that a concurrent shutdown is actively tearing down.","triggerScenarios":"Calling KarateLifecycle.reset() from a hook, test listener, or parallel thread while a shutdown is in flight (e.g. JVM shutdown hook executing, or after Karate.run() completion while shutdown runs); a test-fixture @AfterAll racing with the framework's own shutdown.","commonSituations":"Custom test runners that call reset() in cleanup code; reusing the same JVM across suite runs where cleanup hooks overlap; parallel test suites where one suite's shutdown coincides with another's reset.","solutions":["Ensure reset() is called before shutdown begins or after it fully completes — sequence your fixture teardown accordingly","Check lifecycle state before resetting (isShutdown in progress / current phase) and skip reset if shutting down","Move reset() out of shutdown hooks and into explicit pre-run setup","Serialize suite lifecycle: wait for the previous run's shutdown to finish before resetting"],"exampleFix":"// before\n// in @AfterAll / shutdown hook\nKarateLifecycle.reset();\n// after\nif (!KarateLifecycle.isShutdownInProgress()) { // guard against SHUTTING_DOWN phase\n    KarateLifecycle.reset();\n}","handlingStrategy":"try-catch","validationCode":"// Java: check shutdown state before resetting\n// if lifecycle exposes phase/state, only reset when not shutting down\nif (!lifecycle.isShutdownInProgress()) {\n    KarateLifecycle.reset();\n}","typeGuard":null,"tryCatchPattern":"try {\n    KarateLifecycle.reset();\n} catch (IllegalStateException e) {\n    if (!e.getMessage().contains(\"cannot reset while shutting down\")) throw e;\n    // skip or defer reset until after shutdown completes\n}","preventionTips":["Call reset() only from controlled setup code, never from shutdown hooks or @AfterAll racing teardown","Serialize lifecycle operations across parallel suites","Check phase/state before mutating lifecycle"],"tags":["lifecycle","illegal-state","concurrency"],"backgroundTag":"invalid-state-transition","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}