10

I'm logging every second the length of a map; I don't care if I have the "exact" value / race conditions (off by one is ok). I'm interested to know if this could cause a panic and if I have to enclose len() with some .RLock()/Unlock() or not.

I'm asking because concurrent reads/writes in a map will cause a panic (Go detects that) but I don't know if reading the length counts as a "read". I have tried with a test program but cannot produce a crash but I'd rather have an exact answer, at least for the sake of it.

If it matters I'm interested in both len for arrays and for maps. Thanks!

Thomas
  • 8,306
  • 8
  • 53
  • 92

2 Answers2

9

It is a race condition. The results are undefined. For example,

racer.go:

package main

func main() {
    m := make(map[int]int)
    l := 0
    go func() {
        for {
            l = len(m)
        }
    }()
    for i := 0; i < 10000; i++ {
        m[i] = i
    }
}

Output:

$ go run -race racer.go
==================
WARNING: DATA RACE
Read at 0x00c00008e000 by goroutine 5:
  main.main.func1()
      /home/peter/gopath/src/racer.go:8 +0x5f

Previous write at 0x00c00008e000 by main goroutine:
  runtime.mapassign_fast64()
      /home/peter/go/src/runtime/map_fast64.go:92 +0x0
  main.main()
      /home/peter/gopath/src/racer.go:12 +0xba

Goroutine 5 (running) created at:
  main.main()
      /home/peter/gopath/src/racer.go:6 +0x92
==================
Found 1 data race(s)
exit status 66
$

References:

Wikipedia: Race condition

The Go Blog: Introducing the Go Race Detector

Go: Data Race Detector

Benign Data Races: What Could Possibly Go Wrong?

rustyx
  • 80,671
  • 25
  • 200
  • 267
peterSO
  • 158,998
  • 31
  • 281
  • 276
  • 1
    So it is a race for the exactness of the answer but if I'm just interested in "acceptable values" and no panics (such as read/write in a map) is it ok? – Thomas Oct 08 '18 at 23:20
  • 1
    @Thomas: The results of a data race are undefined. Don't do it! See my reference for the perils of "benign" data races. – peterSO Oct 08 '18 at 23:24
0

If you accept dirty data of len(), the operation will not cause deadlock, and will not cause any panic.

I met this problem in this scenario: I will decide if I need to clean the expired data in a map by if length of map reach the limitation. But I need not exactly limit the size of the map, because write operation is enclosed by .Lock()/Unlock() and will also check the limitation.

Hope this scenario will help you. However if len() is not a high frequency called, I think enclose that with lock is better.

Cyrus
  • 1