Run go program with race flag never returned

Hi experts,

I was trying to create a deadlock error and was working with the following code:

package main

import "fmt"

func main() {
	channel := make(chan int)
	go func(channel chan int) {
		for i := 0; i < 100; i++ {
			channel <- i
		}
	}(channel)

	for val := range channel {
		fmt.Println(val)
	}
}

When I run the program with the following command

go run -race main.go

It never returns, but the same program when I run without the race flag returns a deadlock error.

Please help me on the same.

As far as I understand, it is a quite old issue on the Go side, please see runtime: program with deadlock doesn't fail if use -race flag · Issue #20588 · golang/go · GitHub

1 Like

Hi all,

Is this really a deadlock ? He is merely reading from a channel which never gets closed, I think this behavior is both normal and as intended.

@telo_tade Please correct me if I am wrong. The deadlock in general is “waiting for something that will never happen.” In this case, the consumer will wait for value to come from the channel, but it will never happen, and there will be no other goroutines to execute, hence it will throw a deadlock error. Isn’t it?

Hi @yhgupta,

I do not think it matters much, but the wikipedia definition talks about the Coffman conditions: Deadlock - Wikipedia

Clearly these 4 conditions are not happening in your example.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.