panjf2000 / ants

🐜🐜🐜 ants is the most powerful and reliable pooling solution for Go.
https://ants.andypan.me/
MIT License
12.69k stars 1.36k forks source link

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

Closed xuxiaofan1203 closed 4 months ago

xuxiaofan1203 commented 4 months ago

Actions I've taken before I'm here

What happened?

Hello @panjf2000 , When I ran the test file,https://github.com/panjf2000/ants/blob/34ff2c228286420cfdadf2a6f8b7b7a108952d02/ants_test.go#L53 I found a potential bug. When the Newpool() get executed https://github.com/panjf2000/ants/blob/34ff2c228286420cfdadf2a6f8b7b7a108952d02/ants_test.go#L55 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. https://github.com/panjf2000/ants/blob/34ff2c228286420cfdadf2a6f8b7b7a108952d02/pool.go#L84-L88

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

panjf2000 commented 4 months ago

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().

xuxiaofan1203 commented 4 months ago

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 https://github.com/panjf2000/ants/pull/287 successfully. I suppose it's right in the current version, and maybe it is a false positive reported by goleak.