You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by GitBox <gi...@apache.org> on 2019/10/08 20:23:46 UTC

[GitHub] [lucene-solr] probakowski opened a new pull request #931: LUCENE-9001: Fix race condition in SetOnce

probakowski opened a new pull request #931: LUCENE-9001: Fix race condition in SetOnce
URL: https://github.com/apache/lucene-solr/pull/931
 
 
   # Description
   
   This change fixes race condition in SetOnce class that can cause code like below throw `NullPointerException`
   ```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!
   }
   ```
   It also adds `boolean trySet(T obj)` method that doesn't throw exception when something was set before, it returns false in that case.
   
   # Solution
   
   `AtomicBoolean` and `volatile T obj` was replaced with single `AtomicReference` that serves both purposes: checking if something was set and actually storing object. That way update is atomic - no race condition can occur.
   `Wrapper` is used to allow null object to be stored, the same way it was possible before.
   
   # Tests
   
   All tests from `TestSetOnce` pass, I've also added test for `trySet` method
   
   # Checklist
   
   Please review the following and check all that apply:
   
   - [x] I have reviewed the guidelines for [How to Contribute](https://wiki.apache.org/solr/HowToContribute) and my code conforms to the standards described there to the best of my ability.
   - [x] I have created a Jira issue and added the issue ID to my pull request title.
   - [x] I am authorized to contribute this code to the ASF and have removed any code I do not have a license to distribute.
   - [x] I have given Solr maintainers [access](https://help.github.com/en/articles/allowing-changes-to-a-pull-request-branch-created-from-a-fork) to contribute to my PR branch. (optional but recommended)
   - [x] I have developed this patch against the `master` branch.
   - [x] I have run `ant precommit` and the appropriate test suite.
   - [x] I have added tests for my changes.
   - [ ] I have added documentation for the [Ref Guide](https://github.com/apache/lucene-solr/tree/master/solr/solr-ref-guide) (for Solr changes only).
   

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

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