Wait for all go routines to finish before function returns #19
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.
Goals
Make sure than when EnumerateChildrenAsync returns, all of it's go routines have completed, even in the case of a context cancel
Implementation
Add a waitGroup that waits for all of the graph traversal child go routines to terminate before the function terminates
For Discussion
Reasoning:
See ipfs/go-unixfs#39 (comment)
In the case of a context cancel, the select loop in the main thread of
EnumerateChildrenAsyncDepth
can receive the cancel before it's processed by the go routines enumerating children. This can cause theEnumerateChildrenAsyncDepth
to return with these enumeration routines still in progress, and therefore for getLinks() to get called up to one time per channel after the overall routine returns.The consequences for the caller of the function can be super confusing, since getLinks is provided by the caller, who make not be expecting getLinks to get called after the function returns. A panic can arise if getLinks attempts to write to a channel the caller has closed after EnumerateChildrenAsyncDepth has returned.
child of ipfs/kubo#5600