dragonflyoss / Dragonfly

This repository has be archived and moved to the new repository https://github.com/dragonflyoss/Dragonfly2.
https://d7y.io
Apache License 2.0
6k stars 774 forks source link

Fix goroutine leak bug in DoDownloadTimeout #1565

Open timmyyuan opened 2 years ago

timmyyuan commented 2 years ago

Ⅰ. Describe what this PR did

This PR fix a goroutine leak bug in DoDownloadTimeout

Ⅱ. Does this pull request fix one issue?

NONE

Ⅲ. Why don't you add test cases (unit test/integration test)?

This fix is already covered by TestDoDownloadTimeout

Ⅳ. Describe how to verify it

In the original code, DoDownloadTimeout has a potential goroutine leak in rare goroutine intervening. It can be verified by using https://github.com/uber-go/goleak if we instrument the entry of func Test(t *testing.T) in dfget/core/downloader/downloader_test.go and then run TestDoDownloadTimeout enough times:

go test github.com/dragonflyoss/Dragonfly/dfget/core/downloader -gocheck.f=TestDoDownloadTimeout

This command results in :

time="2021-11-01T13:37:27+08:00" level=warning msg="invalid download timeout(0.000s), use default:(300.000s)"
leaks.go:78: found unexpected goroutines:
    [Goroutine 8 in state chan send, with github.com/dragonflyoss/Dragonfly/dfget/core/downloader.DoDownloadTimeout.func1 on top of the stack:
    goroutine 8 [chan send]:
    github.com/dragonflyoss/Dragonfly/dfget/core/downloader.DoDownloadTimeout.func1()
            /home/yuanting/work/dev/goprojects/dragonfly/dfget/core/downloader/downloader.go:56 +0x12f
    created by github.com/dragonflyoss/Dragonfly/dfget/core/downloader.DoDownloadTimeout
            /home/yuanting/work/dev/goprojects/dragonfly/dfget/core/downloader/downloader.go:55 +0x205
        ]

Ⅴ. Special notes for reviews