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

Avoid stack overflow error with Future.after #29

Merged
merged 1 commit into from
Aug 8, 2015
Merged

Conversation

grddev
Copy link
Collaborator

@grddev grddev commented Jul 14, 2015

This is an attempt at solving #28, without introducing the need to protecting the compound nil future with locks. It should be noted that I have not (yet) performed any extensive tests with respect to the thread safety of the solution.

With a long list of resolved futures, the previous implementation caused a stack-overflow exception, as each call to await_next happened within the previous call (synchronously).

This changes the behavior to loop over futures instead of using recursion while on_complete dispatches synchronously in the same thread. To avoid code duplication we always dispatch in the terminating cases,
even if it introduces an extra stack frame. The looping condition protects against cases where a future would be resolved at a later point by the calling thread (which is what the tests do), and the last part of the condition protects against dispatching in other threads.

While the algorithm looks like a loop that adds multiple concurrent on_complete listeners, the intention is in fact still to only ever have one concurrent listener, making sure that the combined nil future is only ever accessed from one thread at a time (without using locks).

With a long list of resolved futures, the previous implementation
caused a stack-overflow exception, as each call to await_next happened
within the previous call (synchronously).

This changes the behavior to loop over futures instead of using
recursion while on_complete dispatches synchronously in the same thread.
To avoid code duplication we always dispatch in the terminating cases,
even if it introduces an extra stack frame. The looping condition
protects against cases where a future would be resolved at a later
point by the calling thread (which is what the tests do), and the last
part of the condition protects against dispatching in other threads.

While the algorithm looks like a loop that adds multiple concurrent
on_complete listeners, the intention is in fact still to only ever have
one concurrent listener, making sure that the combined nil future is
only ever accessed from one thread at a time (without using locks).
@iconara iconara merged commit 9c12c9c into master Aug 8, 2015
@iconara iconara deleted the stack-safe-after branch October 30, 2015 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants