Skip to content

fix: release writer resources when finish fails - #1279

Open
csurong wants to merge 3 commits into
apache:mainfrom
csurong:fix/write-finish-resource-cleanup
Open

csurong wants to merge 3 commits into
apache:mainfrom
csurong:fix/write-finish-resource-cleanup

Conversation

@csurong

@csurong csurong commented Oct 28, 2026

Copy link
Copy Markdown

Purpose of the pull request

Release writer resources when a completion callback or workbook write fails.

Closes #1278

What's changed?

Completion callbacks now run inside an exception boundary. If one fails, cleanup still runs, workbook output is skipped, and the original exception is rethrown with any cleanup failures suppressed.

Workbook writing and closing use separate try blocks so a write failure cannot skip Workbook.close(). Stream ownership and callback order are unchanged.

Added regression tests for callback failures and write failures in both in-memory and streaming XLSX modes, repeated finish calls, suppressed cleanup failures, and autoCloseStream(false).

Validation on JDK 21: 41 tests passed across the cleanup, handler, encryption, template, and output-stream test classes. Checkstyle passed during the build; Spotless passed for both changed files. The multi-JDK CI matrix has not been run locally.

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.

@alaahong
alaahong requested a balanced review from Copilot October 28, 2026 13:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

2 open issues
What changed in this PR

This PR hardens WriteContextImpl.finish() to reliably release writer resources even when completion callbacks or workbook writing fails, ensuring cleanup still runs and the original failure is preserved (with cleanup failures suppressed).

Changes:

  • Run completion callbacks inside an exception boundary; on callback failure, skip workbook output but continue cleanup and rethrow the original exception with suppressed cleanup errors.
  • Split workbook write and workbook close into separate try blocks so a write failure can’t skip Workbook.close().
  • Add regression tests covering callback failures, write failures (in-memory + streaming), repeated finish calls, suppressed cleanup failures, and autoCloseStream(false).
File Description
fesod-sheet/​src/​main/​java/​org/​apache/​fesod/​sheet/​context/​WriteContextImpl.java Wrapes finish callbacks with exception handling, adjusts write/close control flow, and ensures cleanup failures are suppressed onto the primary failure.
fesod-sheet/​src/​test/​java/​org/​apache/​fesod/​sheet/​context/​WriteContextFinishCleanupTest.java Adds regression tests to validate resource cleanup and exception/suppression behavior across failure modes and configurations.

Review effort: Lite

* Invokes completion callbacks before writing the workbook.
*/
private void finishCallbacks(boolean onException) {
// executes the callback after all sheets has been fully written.
Comment on lines +97 to +103
} finally {
workbook.close();
if (workbook instanceof SXSSFWorkbook) {
((SXSSFWorkbook) workbook).dispose();
}
output.close();
}

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 are not closed when finish fails

3 participants