I have some Golang code that is, very intermittently (once every few hours), resulting in a panic and I need some guidance understanding how to find the cause. The code is this (with line numbers):
audio-process.go:
var MyChannel chan<- interface{}
var newDatagramList = list.New()
func operateAudioProcessing() {
var channel = make(chan interface{})
MyChannel = channel
newDatagramList.Init()
...
415 go func() {
416 for cmd := range channel {
417 switch msg := cmd.(type) {
418 case *MyThing:
419 {
420 newDatagramList.PushBack(msg)
421 }
422 }
423 }
424 }()
425 }
...and the panic occurs when newDatagramList.PushBack()
is called at line 420. The code sending into this channel is:
audio-in.go:
thing := new(MyThing)
...
MyChannel <- thing
...and, to complete the picture, there is a separate timed go func()
which processes newDatagramList
as follows:
go func() {
var next *list.Element
for _ = range processTicker.C {
for newElement := newDatagramList.Front(); newElement != nil; newElement = next {
next = newElement.Next();
myProcessingFunction(newElement.Value.(*MyThing))
newDatagramList.Remove(newElement)
}
}
The panic output is:
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x5702d1]
goroutine 12 [running]:
panic(0x76e520, 0xc82000e100)
/usr/lib/go-1.6/src/runtime/panic.go:481 +0x3e6
container/list.(*List).PushBack(0xc820054e40, 0x6c4f80, 0xc827294e20, 0xc8273e0b01)
/usr/lib/go-1.6/src/container/list/list.go:139 +0x1c1
main.operateAudioProcessing.func2(0xc8200164e0, 0xc82000f310, 0xc820024078, 0x240)
/home/rob/gocode/src/audio-process.go:420 +0x58b
created by main.operateAudioProcessing
/home/rob/gocode/src/audio-process.go:442 +0x5ba
Which thing is the panic telling me is at fault here? There was no complaint about the allocation and send into the channel in the first place, so I don't see how that can be wrong. newDatagramList
is clearly initialised (and the channel has been running and receiving messages for some time).
How do I go about determining what's making me go bang?