Race condition in stopper.go when shutting down
Hi,
When running go test ./utils -race the race detector is raising those warning below.
I think in practice it shouldn't be an issue.
But some project using goflow2 as library might have CI tests (with race detector enabled) failing due to this.
I think it might worth fixing this theoretical race condition, what do you think ?
$ go test ./utils -race
==================
WARNING: DATA RACE
Write at 0x00c000226c30 by goroutine 27:
github.com/netsampler/goflow2/utils.(*stopper).Shutdown()
/go/src/github.com/goflow2/utils/stopper.go:32 +0xc4
github.com/netsampler/goflow2/utils.TestStopper()
/go/src/github.com/goflow2/utils/stopper_test.go:16 +0x112
testing.tRunner()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1259 +0x22f
testing.(*T).Run·dwrap·21()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1306 +0x47
Previous read at 0x00c000226c30 by goroutine 28:
github.com/netsampler/goflow2/utils.(*routine).StartRoutine.func1()
/go/src/github.com/goflow2/utils/stopper_test.go:46 +0x45
Goroutine 27 (running) created at:
testing.(*T).Run()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1306 +0x726
testing.runTests.func1()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1598 +0x99
testing.tRunner()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1259 +0x22f
testing.runTests()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1596 +0x7ca
testing.(*M).Run()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1504 +0x9d1
main.main()
_testmain.go:49 +0x22b
Goroutine 28 (finished) created at:
github.com/netsampler/goflow2/utils.(*routine).StartRoutine()
/go/src/github.com/goflow2/utils/stopper_test.go:44 +0x118
github.com/netsampler/goflow2/utils.TestStopper()
/go/src/github.com/goflow2/utils/stopper_test.go:14 +0xbc
testing.tRunner()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1259 +0x22f
testing.(*T).Run·dwrap·21()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1306 +0x47
==================
==================
WARNING: DATA RACE
Read at 0x00c000226c50 by goroutine 30:
github.com/netsampler/goflow2/utils.TestStopper.func1()
/go/src/github.com/goflow2/utils/stopper_test.go:18 +0x34
github.com/stretchr/testify/assert.Eventually.func1()
/go/pkg/mod/github.com/stretchr/[email protected]/assert/assertions.go:1655 +0x39
Previous write at 0x00c000226c50 by goroutine 28:
github.com/netsampler/goflow2/utils.(*routine).StartRoutine.func1()
/go/src/github.com/goflow2/utils/stopper_test.go:47 +0x64
Goroutine 30 (running) created at:
github.com/stretchr/testify/assert.Eventually()
/go/pkg/mod/github.com/stretchr/[email protected]/assert/assertions.go:1655 +0x3b7
github.com/netsampler/goflow2/utils.TestStopper()
/go/src/github.com/goflow2/utils/stopper_test.go:17 +0x196
testing.tRunner()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1259 +0x22f
testing.(*T).Run·dwrap·21()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1306 +0x47
Goroutine 28 (finished) created at:
github.com/netsampler/goflow2/utils.(*routine).StartRoutine()
/go/src/github.com/goflow2/utils/stopper_test.go:44 +0x118
github.com/netsampler/goflow2/utils.TestStopper()
/go/src/github.com/goflow2/utils/stopper_test.go:14 +0xbc
testing.tRunner()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1259 +0x22f
testing.(*T).Run·dwrap·21()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1306 +0x47
==================
--- FAIL: TestStopper (0.00s)
testing.go:1152: race detected during execution of test
==================
WARNING: DATA RACE
Read at 0x00c000226e00 by goroutine 34:
github.com/netsampler/goflow2/utils.TestCancelUDPRoutine.func3()
/go/src/github.com/goflow2/utils/utils_test.go:41 +0x5e
github.com/netsampler/goflow2/utils.TestCancelUDPRoutine()
/go/src/github.com/goflow2/utils/utils_test.go:51 +0x318
testing.tRunner()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1259 +0x22f
testing.(*T).Run·dwrap·21()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1306 +0x47
Previous write at 0x00c000226e00 by goroutine 35:
github.com/netsampler/goflow2/utils.(*dummyFlowProcessor).FlowRoutine()
/go/src/github.com/goflow2/utils/utils_test.go:74 +0x90
github.com/netsampler/goflow2/utils.TestCancelUDPRoutine.func1()
/go/src/github.com/goflow2/utils/utils_test.go:20 +0x53
Goroutine 34 (running) created at:
testing.(*T).Run()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1306 +0x726
testing.runTests.func1()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1598 +0x99
testing.tRunner()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1259 +0x22f
testing.runTests()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1596 +0x7ca
testing.(*M).Run()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1504 +0x9d1
main.main()
_testmain.go:49 +0x22b
Goroutine 35 (running) created at:
github.com/netsampler/goflow2/utils.TestCancelUDPRoutine()
/go/src/github.com/goflow2/utils/utils_test.go:19 +0x1e4
testing.tRunner()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1259 +0x22f
testing.(*T).Run·dwrap·21()
/usr/local/Cellar/go/1.17.6/libexec/src/testing/testing.go:1306 +0x47
==================
--- FAIL: TestCancelUDPRoutine (0.10s)
testing.go:1152: race detected during execution of test
FAIL
FAIL github.com/netsampler/goflow2/utils 0.463s
FAIL
Hello,
Thanks for the report. I'm a bit late on this. I'm able to reproduce. Will see how I can fix.
The race condition does not like closing or changing the allocation outside of a select.
cc @mariomac
I believe this was fixed with https://github.com/netsampler/goflow2/pull/118.
@lspgn I'm still seeing the same WARNING: DATA RACE when running go test ./utils -race using the latest code on main branch. Shall we reopen the issue?
Should be fixed as part of #150