Fix close unused xlsx sheet and workbook XML streams - #1070
Conversation
Signed-off-by: lance <leehaut@gmail.com>
Signed-off-by: lance <li6151770@126.com>
There was a problem hiding this comment.
Hi @leehaut. The changes look good to me. Could you update the hidden-sheet test to also verify that the skipped sheet's stream is closed?
That test currently passes without the fix because it only checks that the hidden sheet is excluded from the sheet list.
|
FYI,
Remedy: try (InputStream inputStream = sharedStringsTablePackagePart.getInputStream()) {
size = inputStream.available();
} catch (IOException e) {
log.warn("Unable to get file size, default used MapCache");
return new MapCache();
}This may relate to this PR, so you might consider fixing it here as well. |
Thanks, that’s a fair point. The current assertions only prove the hidden sheet is omitted from sheetList/sheetMap, which already held before this fix. I’ll add a regression that records streams closed during construction and asserts the skipped hidden sheet stream is closed. |
Thanks for catching that. I’ll close that SST InputStream with try-with-resources in this PR as well, since it is the same class of leak on the XLSX read path. |
Signed-off-by: lance <li6151770@126.com>
skytin1004
left a comment
There was a problem hiding this comment.
Thanks for the update, @leehaut.
The new test addresses my earlier concern. LGTM!
Purpose of the pull request
Fix InputStream leaks in
XlsxSaxAnalyserwhen reading XLSX files.Hidden sheets skipped via
ignoreHiddenSheet, sheets that are opened but never matched, andworkbook.xmlstreams were not always closed. Over many files this can hold zip/package entry streams longer than needed.What's changed?
InputStreamimmediately when the constructor skips a sheet (ctSheet == null), instead of dropping the handle oncontinue.sheetMapfromexecute()'sfinallyblock. Matched sheets are already closed byparseXmlSource; closing again is safe.xssfReader.getWorkbookData()with try-with-resources inanalysisUse1904WindowDateandanalysisCtSheetMap.matchedSheetinstead of reassigning the loop variable.No public API change. Read results are unchanged.
Test plan
XlsxSaxAnalyserSheetStreamCloseTest:ignoreHiddenSheet=truedoes not retain hidden-sheet streams and closes the visible ones after readHiddenSheetsTest,XlsxSaxAnalyserReadOpcPackageTest, andDateWindowingTestpassedmvn -pl fesod-sheet test -Dmaven.test.skip=false -Dtest=XlsxSaxAnalyserSheetStreamCloseTest,HiddenSheetsTest,XlsxSaxAnalyserReadOpcPackageTest,DateWindowingTestChecklist