go-nats icon indicating copy to clipboard operation
go-nats copied to clipboard

Fix data race

Open noahlevenson opened this issue 1 year ago • 0 comments

When importing github.com/enobufs/go-nats/nats as a library and compiling my program with the -race flag, I consistently received the following data race warning:

==================                                                             
WARNING: DATA RACE                                                                                                                                             
Write at 0x00c0001cf4c0 by goroutine 421:  
  github.com/enobufs/go-nats/nats.(*NATS).performTransactionWith.func1()
      /home/noah/go/pkg/mod/github.com/enobufs/[email protected]/nats/discover.go:329 +0x230
                                                                               
Previous write at 0x00c0001cf4c0 by goroutine 422:
  github.com/enobufs/go-nats/nats.(*NATS).performTransactionWith.func1()
      /home/noah/go/pkg/mod/github.com/enobufs/[email protected]/nats/discover.go:335 +0x357
                                                                                                                                                               
Goroutine 421 (running) created at:                                            
  github.com/enobufs/go-nats/nats.(*NATS).performTransactionWith()       
      /home/noah/go/pkg/mod/github.com/enobufs/[email protected]/nats/discover.go:318 +0x28c
  github.com/enobufs/go-nats/nats.(*NATS).discoverFilteringBehavior.func1() 
      /home/noah/go/pkg/mod/github.com/enobufs/[email protected]/nats/discover.go:266 +0x139
                                                                               
Goroutine 422 (running) created at:                                            
  github.com/enobufs/go-nats/nats.(*NATS).performTransactionWith()
      /home/noah/go/pkg/mod/github.com/enobufs/[email protected]/nats/discover.go:318 +0x28c
  github.com/enobufs/go-nats/nats.(*NATS).discoverFilteringBehavior.func1()
      /home/noah/go/pkg/mod/github.com/enobufs/[email protected]/nats/discover.go:271 +0x16c
==================

Putting a lock on NATS.dfErr makes it go away.

noahlevenson avatar Jul 19 '23 19:07 noahlevenson