Skip to content

fix: close writer resources when a data supplier fails - #1288

Open
csurong wants to merge 1 commit into
apache:mainfrom
csurong:fix/write-supplier-resource-cleanup
Open

csurong wants to merge 1 commit into
apache:mainfrom
csurong:fix/write-supplier-resource-cleanup

Conversation

@csurong

@csurong csurong commented Oct 30, 2026

Copy link
Copy Markdown

Purpose of the pull request

When a Supplier throws in sheet().doWrite(...), the writer has already been created, but the exception bypasses cleanup. The same problem affects table writes and sheet fills using Suppliers.

Closes #1287

What's changed?

Route Supplier evaluation in these builder methods through a shared, package-private helper. If evaluation fails, finish the writer through the existing exception path and rethrow the original exception. Any cleanup failure is attached as a suppressed exception.

Successful writes are unchanged. The fix respects autoCloseStream(false) and writeExcelOnException and does not change the lower-level ExcelWriter Supplier overloads.

Added 24 regression cases covering all four entry points: successful output, runtime exceptions, errors, caller-owned streams, cleanup failures, and writing existing data when writeExcelOnException(true) is enabled.

Validation on JDK 21:

  • 56 tests passed, including the new cases and existing writer, handler, template, and encryption tests.
  • Spotless check passed for the changed Java files.
  • git diff --check passed.
mvn -B -ntp -pl fesod-sheet -am \
  -Dtest=SupplierWriteCleanupTest,WriteContextImplTest,WriteHandlerTest,TemplateDataTest,WriteWorkbookHolderOutputStreamTest,EncryptDataTest \
  -Dsurefire.failIfNoSpecifiedTests=false package

mvn -B -ntp -pl fesod-sheet spotless:check \
  '-DspotlessFiles=.*(ExcelWriterSheetBuilder|ExcelWriterTableBuilder|SupplierWriteSupport|SupplierWriteCleanupTest).java'

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.

This branch has not been deployed

No deployments
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.

[Bug] Writer resources remain open when a data supplier throws

1 participant