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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]