Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CompletionListenerFuture wakeup for multiple threads, protect against spurious wakeup #320

Closed
cruftex opened this issue Jun 3, 2015 · 5 comments
Assignees
Milestone

Comments

@cruftex
Copy link
Member

cruftex commented Jun 3, 2015

From CompletionListenerFuture:

public void onCompletion() throws IllegalStateException {
  synchronized (this) {
    if (isCompleted) {
      throw new IllegalStateException("Attempted to use a CompletionListenerFuture instance more than once");
    } else {
      isCompleted = true;
      notify();
    }
  }
}

This is notifying only one thread upon completion, all others block indefinitely.

Solution: notify() -> notifyAll()

From: #348

The following code fails to deal with spurious wakeup. It is allowed that the thread is notified without a reason. To deal with this situation, the if(!completed) should be while(!completed). Of course the remaining timeout needs to be corrected.

 @Override
  public Void get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
    synchronized (this) {
      if (!isCompleted) {
        unit.timedWait(this, timeout);
      }

      if (isCompleted) {
        if (exception == null) {
          return null;
        } else {
          throw new ExecutionException(exception);
        }
      } else {
        throw new TimeoutException();
      }
    }
  }
@gregrluck gregrluck added this to the 1.1 milestone May 14, 2016
@pveentjer
Copy link

👍 it should indeed be notifyAll.

@cruftex
Copy link
Member Author

cruftex commented Jun 7, 2016

There are three findings in the CompletionListenerFuture at the moment. Additional ones:

#348
#346

Will probably bundle this.

@cruftex cruftex assigned cruftex and unassigned brianoliver Jun 7, 2016
cruftex added a commit to cruftex/jsr107spec that referenced this issue Jun 10, 2016
@cruftex cruftex changed the title Bug in CompletionListenerFuture when result is requested by multiple threads CompletionListenerFuture wakeup for multiple threads, protect against spurious wakeup Jun 10, 2016
cruftex added a commit to cruftex/jsr107tck that referenced this issue Jun 10, 2016
@cruftex
Copy link
Member Author

cruftex commented Jun 10, 2016

Pull request is ready.

Another Pull Request against the TCK is extending the tests to better check the current behavior so we don't introduce an regression. A test for the bug is also included but optional. See PR comment.

@cruftex
Copy link
Member Author

cruftex commented Jun 10, 2016

The PR on the CompletionListenerFuture also does minor corrections in the JavaDoc, since the existing one was stating that a value is returned.

@gregrluck
Copy link
Member

Closed with PR 371

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants