{"record":{"id":"a57edade899677a6","repo":"apache/flink","slug":"unable-to-write-to-output-stream","errorCode":null,"errorMessage":"Unable to write to output stream.","messagePattern":"Unable to write to output stream\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/util/IOUtils.java","lineNumber":66,"sourceCode":"     * @param out OutputStream to write to\n     * @param buffSize the size of the buffer\n     * @param close whether or not close the InputStream and OutputStream at the end. The streams\n     *     are closed in the finally clause.\n     * @throws IOException thrown if an error occurred while writing to the output stream\n     */\n    public static void copyBytes(\n            final InputStream in, final OutputStream out, final int buffSize, final boolean close)\n            throws IOException {\n\n        @SuppressWarnings(\"resource\")\n        final PrintStream ps = out instanceof PrintStream ? (PrintStream) out : null;\n        final byte[] buf = new byte[buffSize];\n        try {\n            int bytesRead = in.read(buf);\n            while (bytesRead >= 0) {\n                out.write(buf, 0, bytesRead);\n                if ((ps != null) && ps.checkError()) {\n                    throw new IOException(\"Unable to write to output stream.\");\n                }\n                bytesRead = in.read(buf);\n            }\n        } finally {\n            if (close) {\n                out.close();\n                in.close();\n            }\n        }\n    }\n\n    /**\n     * Copies from one stream to another. <strong>closes the input and output streams at the\n     * end</strong>.\n     *\n     * @param in InputStream to read from\n     * @param out OutputStream to write to\n     * @throws IOException thrown if an I/O error occurs while copying","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/util/IOUtils.java#L48-L84","documentation":"IOUtils.copyBytes treats the output stream specially when it is a PrintStream: PrintStream swallows I/O errors, so after each write the code calls ps.checkError() and throws IOException(\"Unable to write to output stream.\") if the underlying stream failed. Without this the copy would silently produce truncated output.","triggerScenarios":"Calling IOUtils.copyBytes with System.out/System.err or another PrintStream as destination whose sink has failed — e.g. the pipe reading the process output closed (head/broken less), a redirected stdout to a full/closed file, or a dead terminal on the other end.","commonSituations":"CLI jobs piping Flink output into `head`, log redirection where the disk filled, running in containers where stdout is drained by a logger daemon that died.","solutions":["Check what consumed the process stdout — a closed pipe on the reader side is the usual cause; keep the consumer alive or buffer output","Write to a regular FileOutputStream/BufferedOutputStream instead of a PrintStream so failures surface as normal IOExceptions with a cause","If truncation is acceptable (e.g. piping into head), catch this IOException and ignore it deliberately"],"exampleFix":"// before\nIOUtils.copyBytes(in, System.out, 4096, false);\n// after (failures carry a real cause)\ntry (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {\n    IOUtils.copyBytes(in, out, 4096, true);\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { IOUtils.copyBytes(in, System.out, 4096, false); } catch (IOException e) { /* reader closed the pipe (e.g. | head): exit quietly */ }","preventionTips":["Prefer real output streams over PrintStream for programmatic copies","When piping CLI output to short-lived consumers, tolerate this error explicitly"],"tags":["io","stream","process-output","printstream"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}