{"id":"21fe7ff0cb4a80b6","repo":"junit-team/junit5","slug":"failed-to-close-xml-events-file","errorCode":null,"errorMessage":"Failed to close XML events file","messagePattern":"Failed to close XML events file","errorType":"exception","errorClass":"UncheckedIOException","httpStatus":null,"severity":"error","filePath":"junit-platform-reporting/src/main/java/org/junit/platform/reporting/open/xml/OpenTestReportGeneratingListener.java","lineNumber":222,"sourceCode":"\t\tgit.getOriginUrl() //\n\t\t\t\t.ifPresent(\n\t\t\t\t\tgitUrl -> infrastructure.append(repository(), repository -> repository.withOriginUrl(gitUrl)));\n\t\tgit.getBranch() //\n\t\t\t\t.ifPresent(branch -> infrastructure.append(branch(branch)));\n\t\tgit.getCommitHash() //\n\t\t\t\t.ifPresent(gitCommitHash -> infrastructure.append(commit(gitCommitHash)));\n\t\tgit.getStatus() //\n\t\t\t\t.ifPresent(statusOutput -> infrastructure.append(status(statusOutput),\n\t\t\t\t\tstatus -> status.withClean(statusOutput.isEmpty())));\n\t}\n\n\t@Override\n\tpublic void testPlanExecutionFinished(TestPlan testPlan) {\n\t\ttry {\n\t\t\teventsFileWriter.close();\n\t\t}\n\t\tcatch (IOException e) {\n\t\t\tthrow new UncheckedIOException(\"Failed to close XML events file\", e);\n\t\t}\n\t\tfinally {\n\t\t\teventsFileWriter = DocumentWriter.noop();\n\t\t}\n\t}\n\n\t@Override\n\tpublic void executionSkipped(TestIdentifier testIdentifier, String reason) {\n\t\tString id = String.valueOf(idCounter.incrementAndGet());\n\t\treportStarted(testIdentifier, id);\n\t\teventsFileWriter.append(finished(id, Instant.now()), //\n\t\t\tfinished -> finished.append(result(Result.Status.SKIPPED), result -> {\n\t\t\t\tif (isNotBlank(reason)) {\n\t\t\t\t\tresult.append(reason(reason));\n\t\t\t\t}\n\t\t\t}));\n\t}\n","sourceCodeStart":204,"sourceCodeEnd":240,"githubUrl":"https://github.com/junit-team/junit5/blob/956246301e03d757539f7b76dd5a91f298637563/junit-platform-reporting/src/main/java/org/junit/platform/reporting/open/xml/OpenTestReportGeneratingListener.java#L204-L240","documentation":"Thrown by OpenTestReportGeneratingListener.testPlanExecutionFinished() as an UncheckedIOException when DocumentWriter.close() raises IOException while finalising the events XML. Occurs at the end of a test plan execution; the writer is reset to noop in a finally block but the IOException is rethrown.","triggerScenarios":"XML reporting was enabled and the events file writer fails to flush/close - e.g. the underlying file/stream was closed externally, the socket (in socket mode) dropped, or the filesystem failed the final write.","commonSituations":"Disk filled during the run. Socket server disconnected mid-stream in socket mode. File deleted or FS unmounted before close. Process received a signal that left the writer half-open.","solutions":["Ensure the output filesystem has capacity and remains mounted for the entire run.","In socket mode, keep the server connected until testPlanExecutionFinished completes.","Inspect the wrapped IOException for the specific failure (broken pipe, no space, etc.).","Disable XML reporting if the environment cannot guarantee a clean close."],"exampleFix":"// before: disk fills during run, close() throws UncheckedIOException\n\n// after: ensure adequate disk space; or disable reporting\njunit.platform.reporting.open.xml.enabled = false","handlingStrategy":"try-catch","validationCode":"// Ensure the output medium stays healthy for the whole run\nlong free = outputDir.toFile().getUsableSpace();\nif (free < MIN_FREE_BYTES) throw new IllegalStateException(\"low disk: \" + free);","typeGuard":"static boolean writerLikelyCloseable(DocumentWriter<?> w) {\n    try { w.flush(); return true; } catch (IOException e) { return false; }\n}","tryCatchPattern":"try {\n    eventsFileWriter.close();\n} catch (IOException e) {\n    // log and continue; the report may be partial but the run need not abort\n    log.warn(\"Failed to close XML events file: {}\", e.getMessage());\n} finally {\n    eventsFileWriter = DocumentWriter.noop();\n}","preventionTips":["Reserve enough disk space for the entire test run before writing reports.","In socket mode, keep the server connected until testPlanExecutionFinished finishes.","Treat a close failure as non-fatal if partial reports are acceptable."],"tags":["reporting","xml","io","lifecycle","junit-platform"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}