{"record":{"id":"5ef60103a360355b","repo":"prestodb/presto","slug":"error-writing-to-output","errorCode":null,"errorMessage":"error writing to output","messagePattern":"error writing to output","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"presto-cli/src/main/java/com/facebook/presto/cli/CsvPrinter.java","lineNumber":71,"sourceCode":"            writer.writeNext(toStrings(row));\n            checkError();\n        }\n    }\n\n    @Override\n    public void finish()\n            throws IOException\n    {\n        printRows(ImmutableList.of(), true);\n        writer.flush();\n        checkError();\n    }\n\n    private void checkError()\n            throws IOException\n    {\n        if (writer.checkError()) {\n            throw new IOException(\"error writing to output\");\n        }\n    }\n\n    private static String[] toStrings(List<?> values)\n    {\n        String[] array = new String[values.size()];\n        for (int i = 0; i < values.size(); i++) {\n            array[i] = formatValue(values.get(i));\n        }\n        return array;\n    }\n\n    static String formatValue(Object o)\n    {\n        if (o == null) {\n            return \"\";\n        }\n","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-cli/src/main/java/com/facebook/presto/cli/CsvPrinter.java#L53-L89","documentation":"CsvPrinter buffers output through a PrintWriter; after writing it calls writer.checkError(), and if the underlying writer flagged an error it throws an IOException 'error writing to output'. This surfaces problems with the output stream (broken pipe, closed stream, disk full) rather than query problems.","triggerScenarios":"checkError(), called from printRows and finish, sees PrintWriter.checkError() true after writing CSV rows — typically the consumer closed stdout early (e.g. piping into head) or the stream failed mid-write.","commonSituations":"presto --output-format CSV | head; writing to a file on a full disk; downstream process in a pipe crashing and closing the pipe.","solutions":["Ensure the downstream consumer of the pipe stays open and reads all output (avoid head on large results, or accept the truncation)","Check free disk space / file permissions for the output target","Redirect to a file instead of a fragile pipe when capturing large result sets"],"exampleFix":"// before\npresto --output-format CSV --execute \"SELECT * FROM huge\" | head -5\n// after\npresto --output-format CSV --execute \"SELECT * FROM huge\" > results.csv","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try (PrintWriter w = new PrintWriter(out)) { printer.finish(); } catch (IOException e) { if (\"error writing to output\".equals(e.getMessage())) { log.warn(\"Consumer closed output stream early\", e); } else throw e; }","preventionTips":["Don't pipe CLI output into tools that close early (head) on large results","Monitor disk space on output targets","Write large exports directly to files instead of long pipes"],"tags":["io","output-stream","broken-pipe"],"backgroundTag":"broken-pipe-output","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}