Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for all go routines to finish before function returns #19

Merged
merged 1 commit into from
Nov 8, 2018

Conversation

hannahhoward
Copy link
Contributor

@hannahhoward hannahhoward commented Nov 8, 2018

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 the EnumerateChildrenAsyncDepth 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

@ghost ghost assigned hannahhoward Nov 8, 2018
@ghost ghost added the status/in-progress In progress label Nov 8, 2018
@hannahhoward hannahhoward requested review from whyrusleeping and Stebalien and removed request for Stebalien November 8, 2018 22:04
@Stebalien Stebalien merged commit b1b06bd into master Nov 8, 2018
@ghost ghost removed the status/in-progress In progress label Nov 8, 2018
Copy link
Contributor

@schomatis schomatis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are waiting on all goroutines shouldn't getLinks be called with the fetchersCtx context? If there's an error the cancel issued won't make the goroutine(s) inside getLinks return, the whole function will be waiting on that.

This should help to clean up the emitResult function, right?

@schomatis
Copy link
Contributor

Oh, this was already merged, nevermind.

@hannahhoward
Copy link
Contributor Author

@schomatis we can cleanup emitResult now.

I don't believe the issue you're describing applies. The panic arises from a higher level context cancel(), not fetchersCtx()

@Stebalien Stebalien deleted the bugs/fix-leftover-go-routines branch November 8, 2018 23:13
@schomatis
Copy link
Contributor

The panic arises from a higher level context cancel(), not fetchersCtx()

What panic? I'm not following, I meant (but I may be off here) that if for whatever reason one of the gorotuines fails (sending a message to errChan) EnumerateChildrenAsyncDepth will try to return but it will be stuck waiting for the rest of the goroutines that might still be inside getLinks, the cancel() being issued won't get them out of there, just out of the loop (which is already terminated by closing feed).

Jorropo pushed a commit to ipfs/boxo that referenced this pull request Mar 15, 2023
…o-routines

Wait for all go routines to finish before function returns

This commit was moved from ipfs/go-merkledag@b1b06bd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants