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

event, p2p/simulations/adapters: use buffered channel to avoid goroutine leak #20657

Merged
merged 3 commits into from
Feb 12, 2020
Merged

event, p2p/simulations/adapters: use buffered channel to avoid goroutine leak #20657

merged 3 commits into from
Feb 12, 2020

Conversation

BurtonQin
Copy link
Contributor

subscribe() in event/subscribe.go and Stop() in p2p/simulations/adapters/exec.go share a same problem: some goroutines in these functions will be leaked in certain cases (unsub for subscribe(), and timeout for Stop()).
In subscribe(), the send operation subscribed <- err on L115 will be blocked forever when unsub is selected. Thus the child goroutine leaks and always occupies the memory.
Adding one buffer to the channel will make the send operation never block the child goroutine,
and it will not change semantic.
The same is true for Stop().
In Stop(), although Kill() is called on timeout,
I am not sure if the child goroutine will be in the same process or not. Here is the annotation of Kill(), in /os/exec.go:

// Kill causes the Process to exit immediately. Kill does not wait until
// the Process has actually exited. This only kills the Process itself,
// not any other processes it may have started.
func (p *Process) Kill() error {
    return p.kill()
}

Copy link
Contributor

@fjl fjl left a comment

Choose a reason for hiding this comment

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

Very nice finding!

@@ -143,7 +143,7 @@ func (s *resubscribeSub) loop() {
}

func (s *resubscribeSub) subscribe() Subscription {
subscribed := make(chan error)
subscribed := make(chan error, 1)
Copy link
Contributor

@fjl fjl Feb 12, 2020

Choose a reason for hiding this comment

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

I think it would be better to fix this case by waiting on the subscribed channel when the subscribe loop exits. This way, Unsubscribe will block until the goroutine running s.fn has exited.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, I have now applied the fix myself.

@@ -287,7 +287,7 @@ func (n *ExecNode) Stop() error {
if err := n.Cmd.Process.Signal(syscall.SIGTERM); err != nil {
return n.Cmd.Process.Kill()
}
waitErr := make(chan error)
waitErr := make(chan error, 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

This fix is OK as-is.

@fjl fjl added this to the 1.9.11 milestone Feb 12, 2020
@fjl fjl merged commit a9614c3 into ethereum:master Feb 12, 2020
enriquefynn pushed a commit to enriquefynn/go-ethereum that referenced this pull request Mar 10, 2021
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.

2 participants