Good explanation, Marvin. This is precisely why we have the race detector. 
If all races produced a panic, then it would not be needed. Some races 
behave "correctly" for a long time, and only on rare occasion result in 
silent data corruption that can be almost impossible to debug. Other races 
might result in frequent panics. But no races are "safe", and all code 
should be race free.

On Monday, March 2, 2020 at 8:37:23 AM UTC-5, Marvin Renich wrote:
>
> * Yuan Ting <yuan...@ict.ac.cn <javascript:>> [200301 23:50]: 
> > I write a simple program likes below and it triggers a data race alarm 
> with 
> > -race flag. But no matter how I run, this program will not panic. I know 
> it 
> > is legal to receive messages from nil channels, but I don't quite 
> > understand why this program does not panic in the presence of data 
> races. 
> > 
> > ch := make(chan struct{}) 
> > go func() { 
> > ch = nil 
> > }() 
> > go func() { 
> > <-ch 
> > }() 
>
> I think the other responses missed the obvious here (or at least chose 
> more elaborate, though correct, explanations).  This is a variable 
> assignment in one goroutine concurrent with a variable reference in 
> another.  It is exactly the same (from the point of view of whether it 
> is a race or not) as this: 
>
> var a = 17 
>
> go func() { 
>   a = 987654 
> }() 
>
> go func() { 
>   fmt.Println(a) 
> } 
>
> In the second goroutine, the variable a might have the value 17, it 
> might have the value 987654, and it might have garbage that is neither 
> of these.  This is a race condition, and it is also true of the channel 
> code above. 
>
> One of the other responses very correctly said that a race does not 
> imply that the code could panic.  The integer code above will never 
> panic, even on architectures where a write of an integer to memory 
> concurrent with a read from that memory can possibly allow the read to 
> return partially written data.  However, the above code is clearly a 
> race condition. 
>
> ...Marvin 
>
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/62074448-4ca8-4f49-aa75-dff66b69e579%40googlegroups.com.

Reply via email to