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

[Bug]: A potential goroutine leak in pool.go #323

Closed
3 tasks done
xuxiaofan1203 opened this issue Apr 27, 2024 · 2 comments
Closed
3 tasks done

[Bug]: A potential goroutine leak in pool.go #323

xuxiaofan1203 opened this issue Apr 27, 2024 · 2 comments
Assignees
Labels
bug Something isn't working waiting for response waiting for the response from commenter

Comments

@xuxiaofan1203
Copy link

xuxiaofan1203 commented Apr 27, 2024

Actions I've taken before I'm here

  • I've thoroughly read the documentations on this issue but still have no clue.
  • I've searched the current list of Github issues but didn't find any duplicate issues that have been solved.
  • I've searched the internet with this issue, but haven't found anything helpful.

What happened?

Hello @panjf2000 , When I ran the test file,

func TestAntsPoolWaitToGetWorker(t *testing.T) {
I found a potential bug. When the Newpool() get executed
p, _ := NewPool(AntsSize)

The goroutine will block at the select statement, I think the reason is that maybe there is no cancelFunc to awaken the ctx.Done(), which results in the select blocking. So the goroutine leak.

ants/pool.go

Lines 84 to 88 in 34ff2c2

select {
case <-ctx.Done():
return
case <-ticker.C:
}

Major version of ants

v2

Specific version of ants

latest version

Operating system

Linux

OS version

Ubuntu 22.04.3 LTS

Go version

Go 1.21.7

Relevant log output

=== RUN   TestAntsPoolWaitToGetWorker
    ants_test.go:54: found unexpected goroutines:
        [Goroutine 6 in state select, with github.com/panjf2000/ants/v2.(*Pool).purgeStaleWorkers on top of the stack:
        github.com/panjf2000/ants/v2.(*Pool).purgeStaleWorkers(0xc00010abe0, {0x64ec08, 0xc000016320})
        	/home/song2048/桌面/goProject/src/awesomeProject/src/testdata/real-apps/src/github.com.new/ants/pool.go:84 +0xf8
        created by github.com/panjf2000/ants/v2.(*Pool).goPurge in goroutine 1
        	/home/song2048/桌面/goProject/src/awesomeProject/src/testdata/real-apps/src/github.com.new/ants/pool.go:149 +0xd8
         Goroutine 7 in state select, with github.com/panjf2000/ants/v2.(*Pool).ticktock on top of the stack:
        github.com/panjf2000/ants/v2.(*Pool).ticktock(0xc00010abe0, {0x64ec08, 0xc000016370})
        	/home/song2048/桌面/goProject/src/awesomeProject/src/testdata/real-apps/src/github.com.new/ants/pool.go:127 +0x13f
        created by github.com/panjf2000/ants/v2.(*Pool).goTicktock in goroutine 1
        	/home/song2048/桌面/goProject/src/awesomeProject/src/testdata/real-apps/src/github.com.new/ants/pool.go:156 +0x10b
        ]
    ants_test.go:67: pool, running workers number:1000
    ants_test.go:71: memory usage:4 MB
--- FAIL: TestAntsPoolWaitToGetWorker (10.56s)

FAIL

Code snippets (optional)

No response

How to Reproduce

You can use goleak to reproduce the bug easily like this
1714224116230

Does this issue reproduce with the latest release?

It can reproduce with the latest release

@xuxiaofan1203 xuxiaofan1203 added the bug Something isn't working label Apr 27, 2024
@panjf2000
Copy link
Owner

panjf2000 commented Apr 28, 2024

It's supposed to be blocking when a pool starts, and then loop periodically to clean up idle workers. The context will be cancelled when you call Release().

@panjf2000 panjf2000 added the waiting for response waiting for the response from commenter label Apr 28, 2024
@xuxiaofan1203
Copy link
Author

It's supposed to be blocking when a pool starts, and then loop periodically to clean up idle workers. The context will be cancelled when you call Release().

Thank you for your reply. I read the relative codes carefully again, a potential goleak due to lacking of cancelFunc in Release() has been fixed in #287 successfully. I suppose it's right in the current version, and maybe it is a false positive reported by goleak.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working waiting for response waiting for the response from commenter
Projects
None yet
Development

No branches or pull requests

2 participants