Go十大常见错误第7篇:不使用-race选项做并发竞争检测

2022年8月26日 276点热度 0人点赞 0条评论

前言

这是Go十大常见错误系列的第7篇:不使用-race选项做并发竞争检测。素材来源于Go布道者,现Docker公司资深工程师Teiva Harsanyi[1]

本文涉及的源代码全部开源在:Go十大常见错误源代码[2],欢迎大家关注公众号,及时获取本系列最新更新。

背景

并发编程里很容易遇到并发访问冲突的问题。

Go语言里多个goroutine同时操作某个共享变量的时候,如果一个goroutine对该变量做写操作,其它goroutine做读操作,假设没有做好并发访问控制,就容易出现并发访问冲突,导致程序crash。

大家可以看如下的代码示例:

package main

import (
 "fmt"
)

func main() {
 c := make(chan bool)
 m := make(map[string]string)
 go func() {
  m["1"] = "a" // First conflicting access.
  c <- true
 }()
 m["2"] = "b" // Second conflicting access.
 <-c
 for k, v := range m {
  fmt.Println(k, v)
 }
}

上面的代码会出现并发访问冲突,2个goroutine同时对共享变量m做写操作。

通过-race选项,我们就可以利用编译器帮我们快速发现问题。

$ go run -race main.go 
==================
WARNING: DATA RACE
Write at 0x00c000074180 by goroutine 7:
  runtime.mapassign_faststr()
      /usr/local/opt/go/libexec/src/runtime/map_faststr.go:202 +0x0
  main.main.func1()
      /Users/xxx/github/go-tutorial/workspace/senior/p28/data-race/main.go:11 +0x5d

Previous write at 0x00c000074180 by main goroutine:
  runtime.mapassign_faststr()
      /usr/local/opt/go/libexec/src/runtime/map_faststr.go:202 +0x0
  main.main()
      /Users/xxx/github/go-tutorial/workspace/senior/p28/data-race/main.go:14 +0xcb

Goroutine 7 (running) created at:
  main.main()
      /Users/xxx/github/go-tutorial/workspace/senior/p28/data-race/main.go:10 +0x9c
==================
1 a
2 b
Found 1 data race(s)
exit status 66

常见问题

一个常见的错误是开发者测试Go程序的时候,不使用-race选项。

尽管Go语言设计的目的之一是为了让并发编程更简单、更不容易出错,但Go语言开发者还是会遇到并发问题。

因此,大家在测试Go程序的时候,应该开启-race选项,及时发现代码里的并发访问冲突问题。

$ go test -race mypkg    // to test the package
$ go run -race mysrc.go  // to run the source file
$ go build -race mycmd   // to build the command
$ go install -race mypkg // to install the package

推荐阅读

开源地址

文章和示例代码开源在GitHub: Go语言初级、中级和高级教程[4]

公众号:coding进阶。关注公众号可以获取最新Go面试题和技术栈。

个人网站:Jincheng's Blog[5]

知乎:无忌[6]

福利

我为大家整理了一份后端开发学习资料礼包,包含编程语言入门到进阶知识(Go、C++、Python)、后端开发技术栈、面试题等。

关注公众号「coding进阶」,发送消息 backend 领取资料礼包,这份资料会不定期更新,加入我觉得有价值的资料。还可以发送消息「进群」,和同行一起交流学习,答疑解惑。

References

  • https://itnext.io/the-top-10-most-common-mistakes-ive-seen-in-go-projects-4b79d4f6cd65
  • https://go.dev/doc/articles/race_detector
  • https://medium.com/@val_deleplace/does-the-race-detector-catch-all-data-races-1afed51d57fb

参考资料

[1]

Teiva Harsanyi: https://teivah.medium.com/

[2]

Go十大常见错误源代码: https://github.com/jincheng9/go-tutorial/tree/main/workspace/senior/p28

[3]

Go编译器的race detector可以发现所有的并发冲突么?: https://medium.com/@val_deleplace/does-the-race-detector-catch-all-data-races-1afed51d57fb

[4]

Go语言初级、中级和高级教程: https://github.com/jincheng9/go-tutorial

[5]

Jincheng's Blog: https://jincheng9.github.io/

[6]

无忌: https://www.zhihu.com/people/thucuhkwuji

84240Go十大常见错误第7篇:不使用-race选项做并发竞争检测

这个人很懒,什么都没留下

文章评论