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
base: master
from

Conversation

Projects
None yet
2 participants
@trustin
Member

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

Fix a race condition in DefaultPromise
.. 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

This comment has been minimized.

Show comment
Hide comment
@trustin

trustin Feb 7, 2014

Member

mvn test passes without any problem.

Member

trustin commented Feb 7, 2014

mvn test passes without any problem.

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Feb 7, 2014

Build result for #2186 at 3cfa75f: Failure

ghost commented Feb 7, 2014

Build result for #2186 at 3cfa75f: Failure

@trustin

This comment has been minimized.

Show comment
Hide comment
@trustin

trustin Feb 7, 2014

Member

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

Member

trustin commented Feb 7, 2014

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

@trustin

This comment has been minimized.

Show comment
Hide comment
@trustin

trustin Feb 7, 2014

Member

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

Member

trustin commented Feb 7, 2014

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

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Feb 7, 2014

Build result for #2186 at c97f2d2: Failure

ghost commented Feb 7, 2014

Build result for #2186 at c97f2d2: Failure

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Feb 7, 2014

Build result for #2186 at a53805a: Success

ghost commented Feb 7, 2014

Build result for #2186 at a53805a: Success

@normanmaurer

This comment has been minimized.

Show comment
Hide comment
@normanmaurer

normanmaurer Feb 7, 2014

Member

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

Member

normanmaurer commented Feb 7, 2014

@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 Feb 7, 2014

@trustin trustin self-assigned this Feb 7, 2014

@trustin

This comment has been minimized.

Show comment
Hide comment
@trustin

trustin Feb 7, 2014

Member

Merged via 309ee68. Thanks for reviewing @normanmaurer

Member

trustin commented Feb 7, 2014

Merged via 309ee68. Thanks for reviewing @normanmaurer

normanmaurer pushed a commit that referenced this pull request Feb 20, 2014

Norman Maurer
Fix a regression which could lead to GenericFutureListeners never bee…
…n notifed. Part of [#2186].

This regression was introduced by commit c97f2d2

normanmaurer pushed a commit that referenced this pull request Feb 20, 2014

Norman Maurer
Fix a regression which could lead to GenericFutureListeners never bee…
…n notifed. Part of [#2186].

This regression was introduced by commit c97f2d2

normanmaurer pushed a commit that referenced this pull request Feb 20, 2014

Norman Maurer
Fix a regression which could lead to GenericFutureListeners never bee…
…n notifed. Part of [#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