Fix stack overflow on long AwaitAllWaitHandle chains#9650
Open
mszabo-wikia wants to merge 1 commit intofacebook:masterfrom
Open
Fix stack overflow on long AwaitAllWaitHandle chains#9650mszabo-wikia wants to merge 1 commit intofacebook:masterfrom
mszabo-wikia wants to merge 1 commit intofacebook:masterfrom
Conversation
|
@facebook-github-bot has imported this pull request. If you are a Meta employee, you can view this in D84352565. (Because this pull request was imported automatically, there will not be any future comments.) |
5e11458 to
5a24156
Compare
Contributor
|
@mszabo-wikia has updated the pull request. You must reimport the pull request before landing. |
When an `AwaitAllWaitHandle` is unblocked, it checks whether all its
children are finished, and unblocks its parent if so. This can lead
to a stack overflow if an extremely long chain of `AwaitAllWaitHandle`s
has formed. This is very easy to trigger e.g. by using a HSL `Semaphore`
with a slow task:
```hack
async function bar(): Awaitable<void> {
$s = new \HH\Lib\Async\Semaphore(10, async (bool $should_wait) ==> {
if ($should_wait) {
await Asio\usleep(100000 * 100000);
} else {
await Asio\later();
}
});
concurrent {
await $s->waitForAsync(true);
await async {
for ($i = 0; $i < 1000000; $i++) {
await $s->waitForAsync(false);
}
};
}
}
<<__EntryPoint>>
function mymain(): void {
Asio\join(bar());
}
```
When this runs, we either crash in `AsioBlockableChain::unblock`
as we recursively try to unblock each handle in the chain,
or in the native destructor for `AwaitAllWaitHandle` as we recursively
try to free the entire chain. `ConcurrentWaitHandle` is implemented
in a similar vein and may thus also suffer from the same issue.
So:
* When freeing `AwaitAllWaitHandle`s or `ConcurrentWaitHandle`s, iteratively
decrement the refcount for any children that are themselves of the same type.
and free their backing memory in a separate loop instead of deeply recursing
via `decRefObj()`. Handle other children as before.
* Avoid recursion in `AsioBlockableChain::unblock` via a worklist.
Open questions:
* Should we also handle a chain that consists of mixed `AwaitAllWaitHandle`s
and `ConcurrentWaitHandle`s? I haven't been able to create a reproducer
that'd create such a chain.
* The iterative `AsioBlockableChain::unblock` implementation now also leaves
`m_lastParent` untouched, which is also how it was before `f80acc289feb1a76029d08447c993138db397a29`.
(D3053017). If I'm reading the code correctly, this should not have an
effect for most `parentChain` calls because those were operating on a
copied value to begin with, but I'm not very confident about this.
5a24156 to
12a54cc
Compare
Contributor
|
@mszabo-wikia has updated the pull request. You must reimport the pull request before landing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When an
AwaitAllWaitHandleis unblocked, it checks whether all its children are finished, and unblocks its parent if so. This can lead to a stack overflow if an extremely long chain ofAwaitAllWaitHandles has formed. This is very easy to trigger e.g. by using a HSLSemaphorewith a slow task:When this runs, we either crash in
AsioBlockableChain::unblockas we recursively try to unblock each handle in the chain, or in the native destructor forAwaitAllWaitHandleas we recursively try to free the entire chain.ConcurrentWaitHandleis implemented in a similar vein and may thus also suffer from the same issue.So:
AwaitAllWaitHandles orConcurrentWaitHandles, iteratively decrement the refcount for any children that are themselves of the same type. and free their backing memory in a separate loop instead of deeply recursing viadecRefObj(). Handle other children as before.AsioBlockableChain::unblockvia a worklist.Open questions:
AwaitAllWaitHandles andConcurrentWaitHandles? I haven't been able to create a reproducer that'd create such a chain.AsioBlockableChain::unblockimplementation now also leavesm_lastParentuntouched, which is also how it was beforef80acc289feb1a76029d08447c993138db397a29. (D3053017). If I'm reading the code correctly, this should not have an effect for mostparentChaincalls because those were operating on a copied value to begin with, but I'm not very confident about this.