{"record":{"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/f070c699a08b5d8393df9afd147af5c5e90bb21b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/JupiterEngineExecutionContext.java#L36-L72","documentation":"Thrown when closing the JupiterEngineExecutionContext at teardown: if the ExtensionContext implements AutoCloseable, its close() method is invoked and any Exception it raises is wrapped into a JUnitException. This surfaces failures from extension context lifecycle cleanup (closing stores, extension-scoped resources).","triggerScenarios":"JupiterEngineExecutionContext.close() calls ((AutoCloseable) extensionContext).close() and the close() throws - e.g. an extension registered a CloseableResource in the Store whose close() failed, or the extension context's own cleanup (store disposal) raised.","commonSituations":"Extension stores a resource (file handle, DB connection, mock control) via ExtensionContext.Store.put(key, CloseableResource) and that resource's close() throws IOException; cleanup-after-failure path where the test already threw and cleanup also fails.","solutions":["Inspect getCause() for the underlying close() exception and fix the resource's close method (close defensively, swallow/log secondary errors).","Make your Store.CloseableResource implementations idempotent and silent on the second close.","Use try-with-resources inside test bodies rather than relying on Store cleanup for resources that can fail."],"exampleFix":"// before\ncontext.getStore(NAMESPACE).put(\"conn\", new CloseableResource() {\n    @Override public void close() throws Throwable { connection.close(); } // may throw\n});\n// after\ncontext.getStore(NAMESPACE).put(\"conn\", new CloseableResource() {\n    @Override public void close() {\n        try { connection.close(); } catch (SQLException e) { log.warn(\"close failed\", e); }\n    }\n});","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    context.close();\n} catch (JUnitException e) {\n    Throwable cause = e.getCause();\n    log.error(\"extension context close failed: {}\", cause);\n    throw e;\n}","preventionTips":["Make Store.CloseableResource.close() idempotent and swallow secondary failures.","Use try-with-resources inside test bodies for resources that may fail to close.","Log (do not rethrow) inside custom close implementations."],"tags":["extension-context","lifecycle","resource-cleanup","closeable"],"backgroundTag":null,"analyzedSha":"f070c699a08b5d8393df9afd147af5c5e90bb21b","analyzedAt":"2026-08-11T20:31:00.530Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}