prestodb/presto · error · IOException

error writing to output

Error message

error writing to output

What it means

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.

Source

Thrown at presto-cli/src/main/java/com/facebook/presto/cli/CsvPrinter.java:71

            writer.writeNext(toStrings(row));
            checkError();
        }
    }

    @Override
    public void finish()
            throws IOException
    {
        printRows(ImmutableList.of(), true);
        writer.flush();
        checkError();
    }

    private void checkError()
            throws IOException
    {
        if (writer.checkError()) {
            throw new IOException("error writing to output");
        }
    }

    private static String[] toStrings(List<?> values)
    {
        String[] array = new String[values.size()];
        for (int i = 0; i < values.size(); i++) {
            array[i] = formatValue(values.get(i));
        }
        return array;
    }

    static String formatValue(Object o)
    {
        if (o == null) {
            return "";
        }

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Ensure the downstream consumer of the pipe stays open and reads all output (avoid head on large results, or accept the truncation)
  2. Check free disk space / file permissions for the output target
  3. Redirect to a file instead of a fragile pipe when capturing large result sets

Example fix

// before
presto --output-format CSV --execute "SELECT * FROM huge" | head -5
// after
presto --output-format CSV --execute "SELECT * FROM huge" > results.csv
Defensive patterns

Strategy: try-catch

Try / catch

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; }

Prevention

When it happens

Trigger: 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.

Common situations: presto --output-format CSV | head; writing to a file on a full disk; downstream process in a pipe crashing and closing the pipe.

Related errors


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/5ef60103a360355b. Report an issue: GitHub.