Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data race #11

Open
mholt opened this issue Mar 22, 2023 · 0 comments
Open

Data race #11

mholt opened this issue Mar 22, 2023 · 0 comments

Comments

@mholt
Copy link

mholt commented Mar 22, 2023

These statistic variables at the package scope are not atomic:

npAllocs, npFrees, npHits, npReturns int64

So I'm getting data races when processing multiple separate files concurrently, each in their own goroutine:

WARNING: DATA RACE
Read at 0x000008332ea0 by goroutine 1243:
  github.com/trimmer-io/go-xmp/xmp.NewNode()
      /home/matt/go/pkg/mod/github.com/trimmer-io/[email protected]/xmp/node.go:83 +0xcb
  github.com/trimmer-io/go-xmp/xmp.copyNode()
      /home/matt/go/pkg/mod/github.com/trimmer-io/[email protected]/xmp/node.go:121 +0xa8
  github.com/trimmer-io/go-xmp/xmp.copyNodes()
      /home/matt/go/pkg/mod/github.com/trimmer-io/[email protected]/xmp/node.go:133 +0x169
  github.com/trimmer-io/go-xmp/xmp.copyNode()
      /home/matt/go/pkg/mod/github.com/trimmer-io/[email protected]/xmp/node.go:126 +0x42c
  github.com/trimmer-io/go-xmp/xmp.(*Decoder).decodeNode()
      /home/matt/go/pkg/mod/github.com/trimmer-io/[email protected]/xmp/unmarshal.go:175 +0x693
  github.com/trimmer-io/go-xmp/xmp.(*Decoder).Decode()
      /home/matt/go/pkg/mod/github.com/trimmer-io/[email protected]/xmp/unmarshal.go:133 +0x1a84
  github.com/trimmer-io/go-xmp/xmp.Unmarshal()
      /home/matt/go/pkg/mod/github.com/trimmer-io/[email protected]/xmp/unmarshal.go:60 +0xce
...

Previous write at 0x000008332ea0 by goroutine 2555:
  github.com/trimmer-io/go-xmp/xmp.NewNode()
      /home/matt/go/pkg/mod/github.com/trimmer-io/[email protected]/xmp/node.go:83 +0xe8
  github.com/trimmer-io/go-xmp/xmp.(*Node).UnmarshalXML()
      /home/matt/go/pkg/mod/github.com/trimmer-io/[email protected]/xmp/node.go:379 +0x3f4
  github.com/trimmer-io/go-xmp/xmp.(*Node).UnmarshalXML()
      /home/matt/go/pkg/mod/github.com/trimmer-io/[email protected]/xmp/node.go:380 +0x4a9
  encoding/xml.(*Decoder).unmarshalInterface()
      /usr/local/go/src/encoding/xml/read.go:211 +0x1ac
  encoding/xml.(*Decoder).unmarshal()
      /usr/local/go/src/encoding/xml/read.go:363 +0xc6d
  encoding/xml.(*Decoder).DecodeElement()
      /usr/local/go/src/encoding/xml/read.go:156 +0x2a4
  encoding/xml.(*Decoder).Decode()
      /usr/local/go/src/encoding/xml/read.go:140 +0x84
  github.com/trimmer-io/go-xmp/xmp.(*Decoder).Decode()
      /home/matt/go/pkg/mod/github.com/trimmer-io/[email protected]/xmp/unmarshal.go:81 +0x204
  github.com/trimmer-io/go-xmp/xmp.Unmarshal()
      /home/matt/go/pkg/mod/github.com/trimmer-io/[email protected]/xmp/unmarshal.go:60 +0xce

Fortunately, these are int64, so the easy solution is to use the atomic package, for example to add 1 to a variable, you simply do atomic.AddInt64(&npAllocs, 1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant