Obviously, I have a race condition in my go code. But I cannot find it as I'm pretty sure to be synchronizing correctly. After hours of debugging, you probably can help me find it.
First of all, here's my (very simplified) code:
package main
import (
"log"
"time"
)
type Parser struct {
callback Callback
callbackSet chan bool
test int
}
func NewParser() Parser {
p := Parser{}
p.test = 100
p.callbackSet = make(chan bool)
return p
}
func (p *Parser) SetCallback(newCallback Callback) {
log.Println("=> SET CALLBACK: ", newCallback)
p.test = 100
p.callback = newCallback
log.Println("=> SETTING CALLBACK DONE")
p.callbackSet <- true
}
func (p *Parser) StartParsing() {
go p.parse()
}
func (p *Parser) parse() {
cb := <-p.callbackSet
_ = cb
log.Println("Verify Callback: ", p.callback)
log.Println("Verify Test Variable: ", p.test)
funcDone := make(chan bool)
go func() {
time.Sleep(3 * time.Second) // Some io-Operation here
funcDone <- true
}()
_ = <-funcDone
}
type Callback func(Message)
type Message int
type Dialog struct {
Parser Parser
}
func CreateDialog() (Dialog, error) {
d := Dialog{}
d.Parser = NewParser()
d.Parser.StartParsing()
return d, nil
}
func (d *Dialog) OnMessage(callback Callback) {
log.Println("dialog.OnMessage: ", callback)
time.Sleep(3 * time.Second) // This sleep is just to prove the synchronization. It could be removed.
d.Parser.SetCallback(callback)
}
func main() {
dialog, _ := CreateDialog()
dialog.OnMessage(func(m Message){
log.Println("Message: ", m)
})
time.Sleep(5 * time.Second) // Not clean but just to await all of the output
}
The big question now is: Why is p.callback
<nil>
in p.parse
whereas p.test
isn't, although these two are set at the very same time?
And the stuff should be synchronized using the channel p.callbackSet
?!
Fully runnable example at https://play.golang.org/p/14vn5Tie5Y
I tried replacing the main function by a simpler one. I suspect the bug to be somewhere in the Dialog
struct. When I circumvent its usage I'm unable to reproduce the issue:
func main() {
p := NewParser()
p.StartParsing()
p.SetCallback(func (m Message) {
log.Println("Message: ", m)
})
time.Sleep(5 * time.Second) // Not clean but just to await all of the output
}
The rest of the code remains the same. Another playable example of the modified (working) version here: https://play.golang.org/p/0Y0nKbfcrv