Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid stack overflow error with Future.after
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).
- Loading branch information