Bug 62176 - DefaultTempFileCreationStrategy is not thread-safe
Summary: DefaultTempFileCreationStrategy is not thread-safe
Alias: None
Product: POI
Classification: Unclassified
Component: POI Overall (show other bugs)
Version: 3.17-FINAL
Hardware: PC Linux
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2018-03-13 14:15 UTC by Fabien Chebel
Modified: 2018-03-13 15:13 UTC (History)
1 user (show)


Note You need to log in before you can comment on or make changes to this bug.
Description Fabien Chebel 2018-03-13 14:15:44 UTC
Whe using Apache POI, a single instance of DefaultTempFileCreationStrategy is created and used by the org.apache.poi.util.TempFile static class.

This means that this component is shared by the whole Java application in which POI is used.

Assuming an application is processing two Excel files in parallel via Apache POI, it will end up calling DefaultTempFileCreationStrategy#createTempDirectory(java.io.File) in order to create the '/tmp/poifiles' directory (default name on Linux system).

This code will then be run:

private void createTempDirectory(File directory) throws IOException {
    // create directory if it doesn't exist
    final boolean dirExists = (directory.exists() || directory.mkdirs());

    // snip

Let's say this method is being called at the same time in the same JVM by thread A and thread B

1) Thread A and Thread B execute directory.exists(). Both calls return 'false' as the directory does not exist yet

2) Thread A and Thread B start executing directory.mkdirs() up until the part where the directory existence is checked

3) Thread A creates the directory first: mkdirs() returns true. Thread B fails to create the directory and mkdirs() returns false.

4) An IOException will be thrown in Thread B

To make this method Thread-safe, a synchronized block could be added to the method, with a lock on the whole class.
Comment 1 PJ Fanning 2018-03-13 15:13:36 UTC
Change merged using https://svn.apache.org/viewvc?view=revision&revision=1826655