How to debug my stupid goroutine and chan?

1,931 views
Skip to first unread message

dlin

unread,
Mar 1, 2011, 11:19:03 AM3/1/11
to golang-nuts
The following program will cause
panic: runtime error: send to nil channel

But, I don't know why. And the error message seems stop on
"time.Sleep(20 * 1e9)" this line.


package main // I try to run one goroutine after one goroutine
import "time"

func run(name string, sec int64, in_ch, out_ch chan int64) {
<-in_ch
for i:=0; i<10*1e5; i++ {
time.Sleep(sec * 1e9)
println(name)
}
out_ch<- sec
}

func main() {
var c1,c2 chan int64
go run("Boy2", 2, c1, c2)
go run("Girl3", 3, c2, c1)
println("waiting...")
c1<- 1
time.Sleep(20 * 1e9)
println("done...")
}

roger peppe

unread,
Mar 1, 2011, 11:26:36 AM3/1/11
to dlin, golang-nuts
On 1 March 2011 16:19, dlin <dli...@gmail.com> wrote:
> The following program will cause
> panic: runtime error: send to nil channel

you need to "make" your channels before using them.


> But, I don't know why.  And the error message seems stop on
> "time.Sleep(20 * 1e9)" this line.
>
>
> package main // I try to run one goroutine after one goroutine
> import "time"
>
> func run(name string, sec int64, in_ch, out_ch chan int64) {
>        <-in_ch
>        for i:=0; i<10*1e5; i++ {
>                time.Sleep(sec * 1e9)
>                println(name)
>        }
>        out_ch<- sec
> }
>
> func main() {
>        var c1,c2 chan int64

c1 := make(chan int64)
c2 := make(chan int64)

Daniel Lin

unread,
Mar 1, 2011, 7:51:24 PM3/1/11
to roger peppe, golang-nuts
Thank you, it works.
But, I don't know how to get this answer from the runtime error line.
Does the error line point to the wrong line?
I guess it should be minus one line.

2011/3/2 roger peppe <rogp...@gmail.com>

Anschel

unread,
Mar 1, 2011, 8:33:34 PM3/1/11
to golang-nuts
On Mar 1, 7:51 pm, Daniel Lin <dlin...@gmail.com> wrote:
> Thank you, it works.
> But, I don't know how to get this answer from the runtime error line.
> Does the error line point to the wrong line?
> I guess it should be minus one line.

Hard to say without seeing the actual error message, but IMO the
runtime error should be at either "c1<- 1" or "out_ch<- sec", i.e.
when you send something to a nil channel. If that doesn't match up you
might be dealing with a bug in go.

Daniel Lin

unread,
Mar 1, 2011, 8:58:43 PM3/1/11
to Steven, golang-nuts
Thank you, I understood more about the runtime error messages.

2011/3/2 Steven <stev...@gmail.com>
On Tue, Mar 1, 2011 at 7:51 PM, Daniel Lin <dlin.tw@gmail.com> wrote:
Thank you, it works.
But, I don't know how to get this answer from the runtime error line.
Does the error line point to the wrong line?
I guess it should be minus one line.

Well, since it says that there is a nil channel being sent on, its fairly obvious what the problem is in this case to someone who knows they have to initialize a channel before using it. 

More generally, the error says that you are sending on a nil chan. That means you want to look for a channel send operation when the channel isn't initialized. To find out which channel isn't initialized, you'd have to look at the stack trace (the part that's printed after the panic line). For example, here's the trace from golang.org/doc/play:
runtime.panic+0xa7 /sandbox/go/src/pkg/runtime/proc.c:1023
	runtime.panic(0x424744, 0xf840001210)
runtime.panicstring+0xa3 /sandbox/go/src/pkg/runtime/runtime.c:92
	runtime.panicstring(0x464753, 0x28)
runtime.chansend1+0x1a /sandbox/go/src/pkg/runtime/chan.c:398
	runtime.chansend1(0x0, 0x1)
main.main+0xc7 /tmp/gosandbox-b8203b88_ca284eb3_5ba4591f_37c4c5cd_973dc7ac/prog.go:18
	main.main()
runtime.mainstart+0xf /sandbox/go/src/pkg/runtime/amd64/asm.s:77
	runtime.mainstart()
runtime.goexit /sandbox/go/src/pkg/runtime/proc.c:149
	runtime.goexit()

goroutine 3 [1]:
main.run /tmp/gosandbox-b8203b88_ca284eb3_5ba4591f_37c4c5cd_973dc7ac/prog.go:4
	main.run(0x41c4c8, 0x5, 0x3, 0x0, 0x0, ...)
runtime.goexit /sandbox/go/src/pkg/runtime/proc.c:149
	runtime.goexit()

goroutine 2 [1]:
main.run /tmp/gosandbox-b8203b88_ca284eb3_5ba4591f_37c4c5cd_973dc7ac/prog.go:4
	main.run(0x41c2e0, 0x4, 0x2, 0x0, 0x0, ...)
runtime.goexit /sandbox/go/src/pkg/runtime/proc.c:149
	runtime.goexit()
You can see that you have the two extra goroutines you started that haven't really started running yet. The interesting part is the first goroutine. Ignore the lines in package runtime (they're not your code). The top (and only) stack frame in your code is main.main, and it shows where in the function the program was when the error occurred. Here its says its at line 18 of prog.go (which is just where goplay saves your code to compile it). That line is 
` c1<- 1`, which tells you that c1 needs to be initialized. So you look back and see where the value comes from in order to fix the problem. While checking, you may also notice you have to fix c2, or you might not notice until the program complains.



Dave Cheney

unread,
Mar 1, 2011, 9:01:20 PM3/1/11
to Daniel Lin, roger peppe, golang-nuts
Works fine for me,

lucky(/tmp) % 6g prog.go && 6l prog.6 && ./6.out 2>&1 | head -n2
waiting...


panic: runtime error: send to nil channel

runtime.chansend1+0x1a /Users/dave/go/src/pkg/runtime/chan.c:398
runtime.chansend1(0x0, 0x1)
main.main+0xc7 /private/tmp/prog.go:18

18: c1<- 1

What release OS and arch are you using ?

On Wed, Mar 2, 2011 at 11:51 AM, Daniel Lin <dli...@gmail.com> wrote:
> Thank you, it works.
> But, I don't know how to get this answer from the runtime error line.
> Does the error line point to the wrong line?
> I guess it should be minus one line.
> 2011/3/2 roger peppe <rogp...@gmail.com>
>>

Daniel Lin

unread,
Mar 1, 2011, 9:10:39 PM3/1/11
to Dave Cheney, roger peppe, golang-nuts
Thank you, I've got the correct error line.
I just misunderstood the error messages.

2011/3/2 Dave Cheney <da...@cheney.net>
Works fine for me,

lucky(/tmp) % 6g prog.go && 6l prog.6 && ./6.out 2>&1 | head -n2
waiting...
panic: runtime error: send to nil channel

runtime.chansend1+0x1a /Users/dave/go/src/pkg/runtime/chan.c:398
       runtime.chansend1(0x0, 0x1)
main.main+0xc7 /private/tmp/prog.go:18

18: c1<- 1

What release OS and arch are you using ?

Alexander Sychev

unread,
Mar 2, 2011, 4:00:54 AM3/2/11
to golang-nuts, dlin
Hello!

Channels must be created :-)

c1 = make(chan int64)
c2 = make(chan int64)


--
Best regards,
santucco

Reply all
Reply to author
Forward
0 new messages