Bug 64477 - [PATCH] prevent duplicate delete of temporary file of SXSSF SheetDataWriter
Summary: [PATCH] prevent duplicate delete of temporary file of SXSSF SheetDataWriter
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: SXSSF (show other bugs)
Version: 4.1.2-FINAL
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-05-28 08:45 UTC by neo.wcng
Modified: 2020-05-28 09:26 UTC (History)
0 users



Attachments
patch to prevent duplicate call to delete temporary file (656 bytes, text/plain)
2020-05-28 08:45 UTC, neo.wcng
Details

Note You need to log in before you can comment on or make changes to this bug.
Description neo.wcng 2020-05-28 08:45:12 UTC
Created attachment 37272 [details]
patch to prevent duplicate call to delete temporary file

TL;DR: 
if we call the SXSSFWorkbook.dispose(), the finalizer of SheetDataWriter will attempt to delete the already deleted temporary files. The POI library will create an error log which in fact is a false alarm.
The submitted patch is to check the existence of the file before calling the File.delete() method.



Long version: 
According to the doc, 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Note that SXSSF allocates temporary files that you must always clean up explicitly, by calling the dispose method.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In our app, after we write the excel file using SXSSFWorkbook, we follow the guideline to call the method SXSSFWorkbook.dispose(). 

It will then iterate the SXSSFSheet and call SXSSFSheet.dispose() which in turn call the SheetDataWriter.dispose() method. This method will remove the temporary file created.

    boolean dispose() throws IOException {
        final boolean ret;
        try {
            _out.close();
        } finally {
            ret = _fd.delete();
        }
        return ret;
    }


But the problem is, when the JVM GC run, it will call the SheetDataWriter.finalize() method, which will remove the same file again.

    @Override
    protected void finalize() throws Throwable {
        if (!_fd.delete()) {
            logger.log(POILogger.ERROR, "Can't delete temporary encryption file: "+_fd);
        }

        super.finalize();
    }

Although the app will not crash, this log is annoying and create a false alarm.
Comment 1 PJ Fanning 2020-05-28 09:26:15 UTC
Comment on attachment 37272 [details]
patch to prevent duplicate call to delete temporary file

Thanks. Merged with https://svn.apache.org/repos/asf/poi/trunk@1878227