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

A goroutine leak in manager/state/raft/transport/transport_test.go #3185

Open
xuxiaofan1203 opened this issue Oct 28, 2024 · 0 comments
Open

Comments

@xuxiaofan1203
Copy link

xuxiaofan1203 commented Oct 28, 2024

When call New() at


A goroutine is created to execute run() in New()

And the run() can return when case <-ctx.Done() get executed
for {
select {
case <-ctx.Done():
return
default:
}

However, there is no tr.Stop() to cancel the context after calling New() sometimes. So the goroutine will block forever and leak.

func (t *Transport) Stop() {
t.cancel()
<-t.done
}

How to reproduce:
You can use goleak testing the function to reproduce the bug. For the test function , The newMockRaft( ) is called four times, but only one c.Get(3).Stop() get exectued. So goroutines leak.

func TestUpdatePeerAddr(t *testing.T) {

The partial output is as follows:

leaks.go:78: found unexpected goroutines:
        [Goroutine 9 in state select, with github.com/moby/swarmkit/v2/manager/state/raft/transport.(*Transport).run on top of the stack:
        goroutine 9 [select]:
        github.com/moby/swarmkit/v2/manager/state/raft/transport.(*Transport).run(0xc0000365f0, {0xe26d20, 0xc0000365a0})
        	/home/song2048/桌面/goProject/src/GoPV/GoPV/testdata/real-GoProjects/src/github.com/swarmkit/manager/state/raft/transport/transport.go:111 +0x1e7
        created by github.com/moby/swarmkit/v2/manager/state/raft/transport.New in goroutine 8
        	/home/song2048/桌面/goProject/src/GoPV/GoPV/testdata/real-GoProjects/src/github.com/swarmkit/manager/state/raft/transport/transport.go:80 +0x29c

How to fix the bug:
For the test function, we should call stop() three times at the end of the function to avoid leaking goroutines.

c.Get(1).Stop()
c.Get(2).Stop()
nr.Stop()
@xuxiaofan1203 xuxiaofan1203 changed the title A goroutine leak in manager/state/raft/transport/mock_raft_test.go A goroutine leak in manager/state/raft/transport/transport_test.go Oct 28, 2024
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

No branches or pull requests

1 participant