Bug 64441 - WorkbookFactory.create(InputStream inp) nullpointer error in multithreding
Summary: WorkbookFactory.create(InputStream inp) nullpointer error in multithreding
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 4.1.2-FINAL
Hardware: PC All
: P2 critical (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-05-14 16:43 UTC by Eugene
Modified: 2020-05-17 17:31 UTC (History)
1 user (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Eugene 2020-05-14 16:43:38 UTC
When calling a WorkbookFactory.create(InputStream inp) method from multiple threads sometimes following error appears:

Caused by: java.lang.NullPointerException
    at org.apache.poi.ss.usermodel.WorkbookFactory.create (WorkbookFactory.java:167)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create (WorkbookFactory.java:112)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create (WorkbookFactory.java:253)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create (WorkbookFactory.java:221)

the reason for this is that in HSSFWorkbookFactory class WorkbookFactory.createHssfByNode field is null in this block:

 try {
            initHssf();
            return createHssfByNode.apply(root);
        }

first thread starts initialization:

    private static void initHssf() throws IOException {
        if (createHssfFromScratch == null) {
            // HSSF is part of the main jar, so this shouldn't fail ...
            initFactory("org.apache.poi.hssf.usermodel.HSSFWorkbookFactory", "poi-*.jar");
        }
    }

And sometimes the second thread does not go into this condition and thinks that 123 is already initialized, which is why this error comes in.
Comment 1 Eugene 2020-05-14 16:46:45 UTC
*And sometimes the second thread does not go into this condition and thinks that createHssfByNode is already initialized, which is why this error comes in.

Sorry, for some reason I can’t edit the description.
Comment 2 PJ Fanning 2020-05-14 17:55:08 UTC
as a workaround, you should be able to use write a wrapper that has synchronization

you could also try using `new HSSFWorkbook(stream)`

ideally, the code should support concurrent use but any fix we do could take months to appear in a release
Comment 3 PJ Fanning 2020-05-14 20:49:12 UTC
I added this test https://github.com/apache/poi/commit/24345edd5e7a98089abac93417522cced20025f0 but it passes.
Comment 4 Dominik Stadler 2020-05-15 05:22:49 UTC
The test is very unlikely to trigger it as it only can happen for the first two executions of the future, after that the variable is initialized, 

Maybe we can somehow "uninitialize" it again, otherwise it will fail very very rarely.
Comment 5 PJ Fanning 2020-05-15 08:36:58 UTC
I added some synchronization in https://svn.apache.org/viewvc?view=revision&revision=1877776
Comment 6 Eugene 2020-05-15 11:57:19 UTC
Thanks for the quick answers and fix! Do you think this fix can be included in 4.1.3 release?
Comment 7 PJ Fanning 2020-05-15 12:22:11 UTC
If no bugs found in it, it will be in the next release but there is no date yet for that release.