You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by "clinmanc (via GitHub)" <gi...@apache.org> on 2025/09/26 03:27:26 UTC

[I] TempFilePOIFSFileSystem.close() deletes temp file before releasing file handles [poi]

clinmanc opened a new issue, #911:
URL: https://github.com/apache/poi/issues/911

   ## Description
   
   When using `TempFilePOIFSFileSystem`, the implementation of `close()` attempts to delete the temporary file before calling `super.close()`.
   This ordering can cause the temporary file deletion to fail, because the underlying `FileBackedDataSource` may still hold an open file handle until `super.close()` is executed. On Windows in particular, this typically results in the temp file remaining on disk after closing.
   
   ### Current Code (POI 5.4.1, `TempFilePOIFSFileSystem.java`)
   
   ```java
   @Override
   public void close() throws IOException {
       if (tempFile != null && tempFile.exists()) {
           if (!tempFile.delete()) {
               LOG.atDebug().log("temp file was already deleted (probably due to previous call to close this resource)");
           }
       }
       super.close();
   }
   ```
   
   ## Problem
   
   - The `delete()` call happens before `super.close()`.
   
   - On platforms with strict file locking (e.g., Windows), the temp file cannot be deleted while still in use.
   
   - This leads to temp files not being cleaned up as expected.
   
   ## Suggested Fix
   
   Change the order: call `super.close()` first (to release handles), then delete the temp file.
   
   ```java
   @Override
   public void close() throws IOException {
       try {
           super.close();
       } finally {
           if (tempFile != null && tempFile.exists()) {
               if (!tempFile.delete()) {
                   LOG.atDebug().log("temp file was already deleted (probably due to previous call to close this resource)");
               }
           }
       }
   }
   ```
   
   ## Environment
   
   Apache POI: 5.4.1
   
   JDK: Java 17
   
   OS: Windows 11 (issue most reproducible), also affects Linux if the file is still mapped.
   
   ## Expected Behavior
   
   Temporary file should always be deleted after closing the `TempFilePOIFSFileSystem`.
   
   ## Actual Behavior
   
   On Windows, temp file remains because deletion happens before file handles are closed.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


Re: [I] TempFilePOIFSFileSystem.close() deletes temp file before releasing file handles [poi]

Posted by Kamal Chandrashekar <sa...@gmail.com>.
Chk previous jre.

On Fri, Sep 26, 2025, 8:59 AM clinmanc (via GitHub) <gi...@apache.org> wrote:

>
> clinmanc opened a new issue, #911:
> URL: https://github.com/apache/poi/issues/911
>
>    ## Description
>
>    When using `TempFilePOIFSFileSystem`, the implementation of `close()`
> attempts to delete the temporary file before calling `super.close()`.
>    This ordering can cause the temporary file deletion to fail, because
> the underlying `FileBackedDataSource` may still hold an open file handle
> until `super.close()` is executed. On Windows in particular, this typically
> results in the temp file remaining on disk after closing.
>
>    ### Current Code (POI 5.4.1, `TempFilePOIFSFileSystem.java`)
>
>    ```java
>    @Override
>    public void close() throws IOException {
>        if (tempFile != null && tempFile.exists()) {
>            if (!tempFile.delete()) {
>                LOG.atDebug().log("temp file was already deleted (probably
> due to previous call to close this resource)");
>            }
>        }
>        super.close();
>    }
>    ```
>
>    ## Problem
>
>    - The `delete()` call happens before `super.close()`.
>
>    - On platforms with strict file locking (e.g., Windows), the temp file
> cannot be deleted while still in use.
>
>    - This leads to temp files not being cleaned up as expected.
>
>    ## Suggested Fix
>
>    Change the order: call `super.close()` first (to release handles), then
> delete the temp file.
>
>    ```java
>    @Override
>    public void close() throws IOException {
>        try {
>            super.close();
>        } finally {
>            if (tempFile != null && tempFile.exists()) {
>                if (!tempFile.delete()) {
>                    LOG.atDebug().log("temp file was already deleted
> (probably due to previous call to close this resource)");
>                }
>            }
>        }
>    }
>    ```
>
>    ## Environment
>
>    Apache POI: 5.4.1
>
>    JDK: Java 17
>
>    OS: Windows 11 (issue most reproducible), also affects Linux if the
> file is still mapped.
>
>    ## Expected Behavior
>
>    Temporary file should always be deleted after closing the
> `TempFilePOIFSFileSystem`.
>
>    ## Actual Behavior
>
>    On Windows, temp file remains because deletion happens before file
> handles are closed.
>
>
> --
> This is an automated message from the Apache Git Service.
> To respond to the message, please log on to GitHub and use the
> URL above to go to the specific comment.
>
> To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org.apache.org
>
> For queries about this service, please contact Infrastructure at:
> users@infra.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
> For additional commands, e-mail: dev-help@poi.apache.org
>
>

Re: [I] TempFilePOIFSFileSystem.close() deletes temp file before releasing file handles [poi]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on issue #911:
URL: https://github.com/apache/poi/issues/911#issuecomment-3340020424

   Thanks @clinmanc - I applied https://github.com/apache/poi/commit/03fc1ddea1be6defeca7de71705def32a5ea740b


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org


Re: [I] TempFilePOIFSFileSystem.close() deletes temp file before releasing file handles [poi]

Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning closed issue #911: TempFilePOIFSFileSystem.close() deletes temp file before releasing file handles
URL: https://github.com/apache/poi/issues/911


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org