Uploaded image for project: 'Lucene - Core'
  1. Lucene - Core
  2. LUCENE-9001

Race condition in SetOnce

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 8.4
    • Component/s: None
    • Labels:
      None
    • Lucene Fields:
      New

      Description

      There is race condition in SetOnce that can cause code below fail with NullPointerException:

      SetOnce<String> setOnce = new SetOnce<>();
      new Thread(() -> setOnce.set("thread")).start();
      try{
          setOnce.set("main");
      } catch (SetOnce.AlreadySetException e){
          setOnce.get().hashCode(); //possible NPE!
      }
      

      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.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                probakowski Przemko Robakowski
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 20m
                  20m