[go-nuts] [Help] Seeing an occasional data race on a closed channel

2017-04-24 Thread Tamás Gulácsi
after calling canceled, but before sending on bus, you can close the channels.

Put i in one select:
select {
case <-done:
default:
  select {
case <-done:
case bus<-r:
  }
}

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] [Help] Seeing an occasional data race on a closed channel

2017-04-24 Thread Owen Waller
Hi All,

I am stumped by this one, so I am hoping that someone on the list can
spot the bug in this piece of code.

The code is here:

https://play.golang.org/p/c3356cVtWa

The code occasionally (roughly 1 in 10 to 1 in 5 on my hardware) fails
with the data race shown below.

The code uses a fairly high frequency timer (order of tens of
milliseconds) as a data pump to push runes into a channel. That channel
is then read and displayed to the terminal. This code that the example
is derived from is a simulation of data being written to/read from a
i2c data bus. Hence the high data rate.

My understanding is that when a key is pressed that will terminate the
program. This closes the quit channel, which causes the deferred
closeChans() to be called. That in turn closes the done and the bus
channels in that order. Closing the done channel causes the select in
simBus() to complete in the <-done case. This causes the bus channel to
drain. The next call to writeToBus, which is triggered by the timer
going off - should then fail. The cancelled() function should return
true at this point so no attempt should be made to write to the bus
channel.

As I understand the data race below, this logic is flawed. Attempts are
being made to write to the bus channel after it has been closed.

So far I can't work out why, so I'm hoping someone else can.

Many thanks

Owen

Go version 1.8.1 on Linux/AMD64

==
WARNING: DATA RACE
Write at 0x00c420074060 by main goroutine:
  runtime.closechan()
  /home/owen/golang/go/src/runtime/chan.go:320 +0x0
  main.closeChans()
  /home/owen/go/src/github.com/owenwaller/tcellchanpanic/tcellchanp
anic.go:24 +0x6a
  runtime.call32()
  /home/owen/golang/go/src/runtime/asm_amd64.s:514 +0x47

Previous read at 0x00c420074060 by goroutine 7:
  runtime.chansend()
  /home/owen/golang/go/src/runtime/chan.go:128 +0x0
  main.writeToBus()
  /home/owen/go/src/github.com/owenwaller/tcellchanpanic/tcellchanp
anic.go:49 +0x88
  main.dataPump()
  /home/owen/go/src/github.com/owenwaller/tcellchanpanic/tcellchanp
anic.go:39 +0x11f

Goroutine 7 (running) created at:
  main.main()
  /home/owen/go/src/github.com/owenwaller/tcellchanpanic/tcellchanp
anic.go:87 +0x16e
==
panic: end of program - only one go routine

goroutine 1 [running]:
main.main()
/home/owen/go/src/github.com/owenwaller/tcellchanpanic/tcellcha
npanic.go:91 +0x21b

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.