Skip to content

fix(sheet): keep CSV writing from closing a stream it does not own - #1211

Merged
delei merged 2 commits into
apache:mainfrom
Sean-Walker0:fix/csv-write-respect-auto-close-stream
Nov 21, 2026
Merged

delei merged 2 commits into
apache:mainfrom
Sean-Walker0:fix/csv-write-respect-auto-close-stream

Conversation

@Sean-Walker0

Copy link
Copy Markdown
Contributor

Purpose of the pull request

Two contract violations in the CSV write path, found by code audit against the read-side sibling and the xlsx path:

  1. autoCloseStream(false) is not honored. CsvSheet.close() closed the CSVPrinter, which closes the PrintWriter wrapping the caller's output stream — regardless of the writer's autoCloseStream setting. Writing CSV into a caller-managed stream (a zip entry, a socket) therefore closed that stream under its owner; the next write on it fails with "Stream closed". The read side already gates the equivalent parser close on autoCloseStream (ExcelAnalyserImpl cleanup), the xlsx path never closes the caller's stream, and WriteContextImpl.finish is the single owner of that decision (WriteWorkbookHolderOutputStreamTest pins the same contract for the init-failure path).
  2. Output failures are silently swallowed. The PrintWriter wrapping the stream never throws IOException (JDK contract — errors only surface via checkError()), so a full disk, a broken pipe or an already-closed target produced a "successful" finish() with a truncated or empty file.

What's changed?

CsvSheet.close() now flushes the printer instead of closing it and surfaces pending output errors through PrintWriter.checkError(), throwing ExcelGenerateException so finish() reports the failure. Closing the caller's stream remains WriteContextImpl's decision, exactly as for xlsx.

Checklist

  • I have read the Contributor Guide.
  • I have written the necessary doc or comment.
  • I have added the necessary unit tests and all cases have passed.

Verifying this change

CsvWriteStreamOwnershipTest (both directions):

  • writeWithAutoCloseStreamFalseKeepsCallerStreamOpen — red before (expected: <false> but was: <true> — the stream was closed), green after, and pins that the data itself is still written;

  • writeSurfacesOutputErrors — red before (nothing was thrown), green after (ExcelGenerateException on a failing stream);

  • writeWithAutoCloseStreamTrueClosesTheStream — pins the default close path, green before and after.

  • Existing CSV tests (CsvFormatTest, CsvRowTest, CsvNoneQuoteTest, …): all pass.

  • ./mvnw clean package -B -Dmaven.test.skip=false -pl fesod-common,fesod-shaded,fesod-sheet: 961 tests, 0 failures, 0 errors.

  • ./mvnw -pl fesod-sheet spotless:check: clean.

Notes

  • Behavior changes: with autoCloseStream(false) the caller's stream now stays open (previously closed — the bug); with the default autoCloseStream(true) nothing changes (WriteContextImpl still closes). A write that fails on the underlying stream now throws instead of silently producing a truncated file — callers that relied on the silent truncation will now see the failure.
  • Orthogonal to open PRs: fix: address CsvRow cells by column and break the CSV model hashCode cycle #1193 and fix: correct CSV metadata contract #1165 touch CsvSheet.java in the imports/field area (~lines 32/75) and getRow (~line 165) respectively — no overlap with close() (~line 659), and the hunks merge cleanly. fix: prevent CSV multi-sheet data loss #1158 touches only CsvWorkbook.
  • Incidental observation (not touched here): flushData()'s catch (IOException e) can effectively never fire because the wrapped PrintWriter swallows output errors; the new checkError() gate in close() is what makes failures visible.

@Sean-Walker0
Sean-Walker0 force-pushed the fix/csv-write-respect-auto-close-stream branch 3 times, most recently from bdee515 to c3f6306 Compare October 15, 2026 06:45
@Sean-Walker0
Sean-Walker0 force-pushed the fix/csv-write-respect-auto-close-stream branch from c3f6306 to 77d449f Compare October 18, 2026 12:24
@Sean-Walker0
Sean-Walker0 force-pushed the fix/csv-write-respect-auto-close-stream branch 2 times, most recently from 594fe4d to 107dbf3 Compare October 29, 2026 22:52
@Sean-Walker0
Sean-Walker0 force-pushed the fix/csv-write-respect-auto-close-stream branch 3 times, most recently from 468b1a7 to 210ab28 Compare November 9, 2026 23:38
CsvSheet.close() closed the CSVPrinter, which closes the PrintWriter
wrapping the caller's output stream - regardless of the writer's
autoCloseStream setting. Writing CSV into a caller-managed stream
(a zip entry, a socket) with autoCloseStream(false) therefore closed
that stream under its owner, and the next write on it failed with
"Stream closed". The read side already gates the equivalent parser
close on autoCloseStream, and the xlsx path never closes the caller's
stream; WriteContextImpl is the single owner of that decision.

The PrintWriter also never throws IOException, so output failures
(disk full, broken pipe, already-closed target) were silently
swallowed and finish() reported success with a truncated file.

Flush the printer instead of closing it and surface pending output
errors through PrintWriter.checkError.

Verified with CsvWriteStreamOwnershipTest (autoCloseStream(false)
keeps the caller stream open and a failing stream fails the write -
both red before the change; the default close path is pinned on both
sides) plus the full fesod-sheet suite (961 tests) and spotless:check.
@Sean-Walker0
Sean-Walker0 force-pushed the fix/csv-write-respect-auto-close-stream branch from 210ab28 to 0dc39bb Compare November 11, 2026 11:26

@delei delei left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@delei
delei merged commit 3473d39 into apache:main Nov 21, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants