You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by "Przemko Robakowski (Jira)" <ji...@apache.org> on 2019/10/08 19:33:00 UTC

[jira] [Created] (LUCENE-9001) Race condition in SetOnce

Przemko Robakowski created LUCENE-9001:
------------------------------------------

             Summary: Race condition in SetOnce
                 Key: LUCENE-9001
                 URL: https://issues.apache.org/jira/browse/LUCENE-9001
             Project: Lucene - Core
          Issue Type: Bug
            Reporter: Przemko Robakowski


There is race condition in SetOnce that can cause code below fail with NullPointerException:
{code:java}
SetOnce<String> setOnce = new SetOnce<>();
new Thread(() -> setOnce.set("thread")).start();
try{
    setOnce.set("main");
} catch (SetOnce.AlreadySetException e){
    setOnce.get().hashCode(); //possible NPE!
}
{code}
This is caused by 2 separate write operations - 1 for set marker field and 1 for actual object. So it's possible that marker is already set to true (causing AlreadySetException on second write attempt) but object is still not set (causing NullPointerException).

This can be avoided by using single AtomicReference instead to serve both purposes.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org