{"record":{"id":"9007678ab5dc2c8f","repo":"apache/flink","slug":"stream-closed-900767","errorCode":null,"errorMessage":"Stream closed.","messagePattern":"Stream closed\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/core/fs/RefCountedFileWithStream.java","lineNumber":72,"sourceCode":"            stream.write(b, off, len);\n        }\n    }\n\n    void flush() throws IOException {\n        requireOpened();\n        stream.flush();\n    }\n\n    void closeStream() {\n        if (!closed) {\n            IOUtils.closeQuietly(stream);\n            closed = true;\n        }\n    }\n\n    private void requireOpened() throws IOException {\n        if (closed) {\n            throw new IOException(\"Stream closed.\");\n        }\n    }\n\n    // ------------------------------ Factory methods for initializing a temporary file\n    // ------------------------------\n\n    public static RefCountedFileWithStream newFile(final File file, final OutputStream currentOut)\n            throws IOException {\n        return new RefCountedFileWithStream(file, currentOut, 0L);\n    }\n\n    public static RefCountedFileWithStream restoredFile(\n            final File file, final OutputStream currentOut, final long bytesInCurrentPart) {\n        return new RefCountedFileWithStream(file, currentOut, bytesInCurrentPart);\n    }\n}\n","sourceCodeStart":54,"sourceCodeEnd":89,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/core/fs/RefCountedFileWithStream.java#L54-L89","documentation":"Thrown by RefCountedFileWithStream.requireOpened() when a write/flush/getPos operation is attempted on a reference-counted temp file+stream combo whose `closed` flag is true. This wraps the underlying OutputStream along with the ref-counted file.","triggerScenarios":"Calling write/flush on a RefCountedFileWithStream after closeStream() was invoked (either directly or via final release).","commonSituations":"Sink writer error paths that close the stream then a finally block tries to flush; double-cleanup during task failure; test harness closing too early.","solutions":["Ensure all writes complete before closeStream(); reorder so cleanup is last.","Null out the stream reference after close and guard subsequent access.","Use try-with-resources or a dedicated closeable lifecycle.","In error handlers, branch on a 'closed' flag to avoid post-close writes."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"void safeWrite(RefCountedFileWithStream s, byte[] b) throws IOException {\n    if (s.isClosed()) return;\n    s.write(b);\n}","typeGuard":null,"tryCatchPattern":"try {\n    s.write(buf);\n} catch (IOException e) {\n    if (\"Stream closed.\".equals(e.getMessage())) return;\n    throw e;\n}","preventionTips":["Close only after all writes are drained.","Null out stream refs after closeStream().","Use a single cleanup path to avoid double-close + write."],"tags":["filesystem","stream","reference-counting","use-after-close"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}