猿问

无法理解 go test -race : RACE: DATA WARNING 堆栈跟踪

我在测试我的项目时遇到了 DATA RACE 警告,想知道是否有人愿意帮助我破译这个问题。我过去从未尝试过测试 go 例程,我发现很难全神贯注于数据竞争。


我在未解决问题的描述中提供了一个链接,并在问题描述中提供了跟踪。


我真的很感激一些帮助,只是从学习调试类似问题和为将来的 go 例程编写更好的测试方面。


https://github.com/nitishm/vegeta-server/issues/52


下面还提供了跟踪的片段


=== RUN   Test_dispatcher_Cancel_Error_completed

INFO[0000] creating new dispatcher                       component=dispatcher

INFO[0000] starting dispatcher                           component=dispatcher

INFO[0000] dispatching new attack                        ID=d63a79ac-6f51-486e-845d-077c8c76168a Status=scheduled component=dispatcher

==================

WARNING: DATA RACE

Read at 0x00c0000f8d68 by goroutine 8:

  vegeta-server/internal/dispatcher.(*task).Complete()

      /Users/nitishm/vegeta-server/internal/dispatcher/task.go:116 +0x61

  vegeta-server/internal/dispatcher.run()

      /Users/nitishm/vegeta-server/internal/dispatcher/task.go:213 +0x17a


Previous write at 0x00c0000f8d68 by goroutine 7:

  vegeta-server/internal/dispatcher.(*task).Run()

      /Users/nitishm/vegeta-server/internal/dispatcher/task.go:107 +0x12a

  vegeta-server/internal/dispatcher.(*dispatcher).Run()

      /Users/nitishm/vegeta-server/internal/dispatcher/dispatcher.go:109 +0xb5f


Goroutine 8 (running) created at:

  vegeta-server/internal/dispatcher.(*task).Run()

      /Users/nitishm/vegeta-server/internal/dispatcher/task.go:105 +0x11c

  vegeta-server/internal/dispatcher.(*dispatcher).Run()

      /Users/nitishm/vegeta-server/internal/dispatcher/dispatcher.go:109 +0xb5f


Goroutine 7 (running) created at:

  vegeta-server/internal/dispatcher.Test_dispatcher_Cancel_Error_completed()

      /Users/nitishm/vegeta-server/internal/dispatcher/dispatcher_test.go:249 +0x545

  testing.tRunner()

      /usr/local/go/src/testing/testing.go:827 +0x162


元芳怎么了
浏览 110回答 1
1回答

交互式爱情

据我所知:Read at 0x00c0000f8d68 by goroutine 8:和Previous write at 0x00c0000f8d68 by goroutine 7意味着 goroutines8和7正在读取和写入相同的位置。如果您查看错误指向的行:goroutine 8上116:if t.status != models.AttackResponseStatusRunning {goroutine 7上107:t.status = models.AttackResponseStatusRunning您可以看到 goroutinestask在没有任何同步的情况下访问 的状态,正如您已经知道的那样,这可能会导致竞争条件。因此,如果您的程序允许多个 goroutine 访问单个任务,您需要确保不会发生数据竞争,例如使用互斥锁。
随时随地看视频慕课网APP

相关分类

Go
我要回答