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
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.