1
0
mirror of https://github.com/google/gops.git synced 2024-11-24 08:22:25 +02:00

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
This commit is contained in:
Tobias Klauser 2022-07-07 10:49:09 +02:00 committed by Tobias Klauser
parent 8442404056
commit 5ddd2779fb

View File

@ -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") {