From 5ddd2779fbac001cf9fa9f7dcae600cd030b31dd Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Thu, 7 Jul 2022 10:49:09 +0200 Subject: [PATCH] agent: fix data race in Listen Fix the following data race in agent.Listen: ================== WARNING: DATA RACE Write at 0x0000007620a0 by goroutine 9: github.com/google/gops/agent.Listen() /home/tklauser/go/src/github.com/google/gops/agent/agent.go:112 +0x207 github.com/google/gops/agent.TestAgentClose() /home/tklauser/go/src/github.com/google/gops/agent/agent_test.go:21 +0x6e testing.tRunner() /usr/local/go/src/testing/testing.go:1439 +0x213 testing.(*T).Run.func1() /usr/local/go/src/testing/testing.go:1486 +0x47 Previous read at 0x0000007620a0 by goroutine 8: github.com/google/gops/agent.listen() /home/tklauser/go/src/github.com/google/gops/agent/agent.go:130 +0x5a Goroutine 9 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:1486 +0x724 testing.runTests.func1() /usr/local/go/src/testing/testing.go:1839 +0x99 testing.tRunner() /usr/local/go/src/testing/testing.go:1439 +0x213 testing.runTests() /usr/local/go/src/testing/testing.go:1837 +0x7e4 testing.(*M).Run() /usr/local/go/src/testing/testing.go:1719 +0xa71 main.main() _testmain.go:57 +0x2e4 Goroutine 8 (finished) created at: github.com/google/gops/agent.Listen() /home/tklauser/go/src/github.com/google/gops/agent/agent.go:123 +0x43b github.com/google/gops/agent.TestListen() /home/tklauser/go/src/github.com/google/gops/agent/agent_test.go:13 +0x57 testing.tRunner() /usr/local/go/src/testing/testing.go:1439 +0x213 testing.(*T).Run.func1() /usr/local/go/src/testing/testing.go:1486 +0x47 ================== --- FAIL: TestAgentClose (0.00s) testing.go:1312: race detected during execution of test FAIL FAIL github.com/google/gops/agent 0.026s --- agent/agent.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index 2af03e5..de6d90f 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -120,14 +120,14 @@ func Listen(opts Options) error { return err } - go listen() + go listen(listener) return nil } -func listen() { +func listen(l net.Listener) { buf := make([]byte, 1) for { - fd, err := listener.Accept() + fd, err := l.Accept() if err != nil { // No great way to check for this, see https://golang.org/issues/4373. if !strings.Contains(err.Error(), "use of closed network connection") {