{"id":"8355948a330e650c","repo":"junit-team/junit5","slug":"failed-to-close-extension-context","errorCode":null,"errorMessage":"Failed to close extension context","messagePattern":"Failed to close extension context","errorType":"exception","errorClass":"JUnitException","httpStatus":null,"severity":"error","filePath":"junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/JupiterEngineExecutionContext.java","lineNumber":54,"sourceCode":"\tprivate boolean beforeAllMethodsExecuted = false;\n\n\tpublic JupiterEngineExecutionContext(EngineExecutionListener executionListener, JupiterConfiguration configuration,\n\t\t\tLauncherStoreFacade launcherStoreFacade) {\n\t\tthis(new State(executionListener, configuration, launcherStoreFacade));\n\t}\n\n\tprivate JupiterEngineExecutionContext(State state) {\n\t\tthis.state = state;\n\t}\n\n\tpublic void close() throws Exception {\n\t\tExtensionContext extensionContext = getExtensionContext();\n\t\tif (extensionContext instanceof @SuppressWarnings(\"resource\") AutoCloseable closeable) {\n\t\t\ttry {\n\t\t\t\tcloseable.close();\n\t\t\t}\n\t\t\tcatch (Exception e) {\n\t\t\t\tthrow new JUnitException(\"Failed to close extension context\", e);\n\t\t\t}\n\t\t}\n\t}\n\n\tpublic EngineExecutionListener getExecutionListener() {\n\t\treturn this.state.executionListener;\n\t}\n\n\tpublic JupiterConfiguration getConfiguration() {\n\t\treturn this.state.configuration;\n\t}\n\n\tpublic LauncherStoreFacade getLauncherStoreFacade() {\n\t\treturn this.state.launcherStoreFacade;\n\t}\n\n\tpublic TestInstancesProvider getTestInstancesProvider() {\n\t\treturn requireNonNull(this.state.testInstancesProvider);","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/junit-team/junit5/blob/956246301e03d757539f7b76dd5a91f298637563/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/JupiterEngineExecutionContext.java#L36-L72","documentation":"Thrown by JupiterEngineExecutionContext.close() when the ExtensionContext (which implements AutoCloseable) throws an Exception from its close() method. The ExtensionContext.close() cascades through its NamespacedHierarchicalStore, closing every stored CloseableResource/AutoCloseable, so any single resource that fails to close surfaces here.","triggerScenarios":"An extension stored a CloseableResource (or AutoCloseable) in the ExtensionContext Store whose close() throws an Exception (e.g. a database connection, network socket, or file handle that fails on shutdown).","commonSituations":"A Store.CloseableResource wrapping a JDBC connection whose close() throws SQLException; an AutoCloseable test fixture whose close() fails after a test error; resources registered via getStore(...).put(key, resource) where resource.close() is not exception-safe.","solutions":["Inspect the cause Exception — it identifies which stored resource's close() failed.","Make your stored resource's close() swallow/ log non-fatal exceptions and only throw on truly unrecoverable errors.","If a resource need not be auto-closed, do not implement AutoCloseable/CloseableResource, or disable auto-close via the configuration flag isClosingStoredAutoCloseablesEnabled.","Close heavy resources explicitly in an @AfterEach/@AfterAll rather than relying on context shutdown."],"exampleFix":"// before\nclass ConnResource implements Store.CloseableResource {\n    Connection conn;\n    public void close() throws Exception { conn.close(); } // SQLException bubbles up\n}\n// after\nclass ConnResource implements Store.CloseableResource, AutoCloseable {\n    Connection conn;\n    public void close() {\n        try { conn.close(); } catch (SQLException e) { /* log, do not rethrow */ }\n    }\n}","handlingStrategy":"try-catch","validationCode":"// Validate each stored CloseableResource closes cleanly in a test\nStore.CloseableResource r = createResource();\ntry { r.close(); } catch (Exception e) { throw new AssertionError(\"Resource close leaks exception\", e); }","typeGuard":null,"tryCatchPattern":"// Wrap resource close so it never propagates from context shutdown\nclass SafeResource implements Store.CloseableResource, AutoCloseable {\n    public void close() {\n        try { delegate.close(); } catch (Exception e) { log.warn(\"close failed\", e); }\n    }\n}","preventionTips":["Make stored resources' close() exception-safe (catch and log internally).","Close heavy resources in @AfterEach/@AfterAll rather than relying on context shutdown.","Disable auto-close of stored AutoCloseables if you manage their lifecycle yourself."],"tags":["junit-jupiter","extension-context","resource-cleanup","store"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}