Fix stack overflow on long AwaitAllWaitHandle chains #9650
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
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 ofAwaitAllWaitHandle
s has formed. This is very easy to trigger e.g. by using a HSLSemaphore
with a slow task: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 forAwaitAllWaitHandle
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:
AwaitAllWaitHandle
s orConcurrentWaitHandle
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 viadecRefObj()
. Handle other children as before.AsioBlockableChain::unblock
via a worklist.Open questions:
AwaitAllWaitHandle
s andConcurrentWaitHandle
s? I haven't been able to create a reproducer that'd create such a chain.AsioBlockableChain::unblock
implementation now also leavesm_lastParent
untouched, which is also how it was beforef80acc289feb1a76029d08447c993138db397a29
. (D3053017). If I'm reading the code correctly, this should not have an effect for mostparentChain
calls because those were operating on a copied value to begin with, but I'm not very confident about this.