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

Fix a race condition in DefaultPromise #2186

Closed
wants to merge 3 commits into from
Closed

Fix a race condition in DefaultPromise #2186

wants to merge 3 commits into from

Conversation

trustin
Copy link
Member

@trustin trustin commented Feb 7, 2014

.. which occurs when a user adds a listener from different threads after the promise is done and the notifications for the listeners, that were added before the promise is done, is in progress. For instance:

  1. Thread-1: p.addListener(listenerA);
  2. Thread-1: p.setSuccess(null);
  3. Thread-2: p.addListener(listenerB);
  4. Thread-2: p.executor.execute(taskNotifyListenerB);
  5. Thread-1: p.executor.execute(taskNotifyListenerA);

taskNotifyListenerB should not really notify listenerB until taskNotifyListenerA is finished.

To fix this issue:

  • Change the semantic of (listeners == null) to determine if the early listeners [1] were notified
  • If a late listener is added before the early listeners are notified, the notification of the late listener is deferred until the early listeners are notified (i.e. until listeners == null)
  • The late listeners with deferred notifications are stored in a lazily instantiated queue to preserve ordering, and then are notified once the early listeners are notified.

[1] the listeners that were added before the promise is done
[2] the listeners that were added after the promise is done

.. which occurs when a user adds a listener from different threads after the promise is done and the notifications for the listeners, that were added before the promise is done, is in progress.  For instance:

   Thread-1: p.addListener(listenerA);
   Thread-1: p.setSuccess(null);
   Thread-2: p.addListener(listenerB);
   Thread-2: p.executor.execute(taskNotifyListenerB);
   Thread-1: p.executor.execute(taskNotifyListenerA);

 taskNotifyListenerB should not really notify listenerB until taskNotifyListenerA is finished.

 To fix this issue, a new volatile flag 'listenersNotified' were added.  'listenersNotified' is set once all listeners added before the promise is done are notified.  If 'listenersNotified' is not set, the notifications for the listeners added after the promise is done will be rescheduled for later execution.
@trustin
Copy link
Member Author

trustin commented Feb 7, 2014

mvn test passes without any problem.

@ghost
Copy link

ghost commented Feb 7, 2014

Build result for #2186 at 3cfa75f: Failure

@trustin
Copy link
Member Author

trustin commented Feb 7, 2014

As you noticed, this didn't work out. Thinking of a proper fix.

@trustin
Copy link
Member Author

trustin commented Feb 7, 2014

With c97f2d2, the test did not fail with 20000000 runs. I'd say it's fixed really.

@ghost
Copy link

ghost commented Feb 7, 2014

Build result for #2186 at c97f2d2: Failure

@ghost
Copy link

ghost commented Feb 7, 2014

Build result for #2186 at a53805a: Success

@normanmaurer
Copy link
Member

@trustin looks good... Please squash and cherry-pick or should I take care for you so you can get some sleep ;) ?

@trustin trustin closed this Feb 7, 2014
@trustin trustin deleted the promise_race3 branch February 7, 2014 06:09
@trustin trustin self-assigned this Feb 7, 2014
@trustin
Copy link
Member Author

trustin commented Feb 7, 2014

Merged via 309ee68. Thanks for reviewing @normanmaurer

normanmaurer pushed a commit that referenced this pull request Feb 20, 2014
…n notifed. Part of [#2186].

This regression was introduced by commit c97f2d2
normanmaurer pushed a commit that referenced this pull request Feb 20, 2014
…n notifed. Part of [#2186].

This regression was introduced by commit c97f2d2
normanmaurer pushed a commit that referenced this pull request Feb 20, 2014
…n notifed. Part of [#2186].

This regression was introduced by commit c97f2d2
pulllock pushed a commit to pulllock/netty that referenced this pull request Oct 19, 2023
…n notifed. Part of [netty#2186].

This regression was introduced by commit c97f2d2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants