Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang: Strange behaviour with function type

Tags:

go

goroutine

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

like image 944
Atmocreations Avatar asked Mar 11 '23 22:03

Atmocreations


1 Answers

This is because you are storing the Parser object by value and returning a Dialog by value from CreateDialog.

The original Parser instance created inside CreateDialog becomes lost when the Dialog instance is returned by value.

It is the original Parser that is parsing, and receives the callback as logged.

func CreateDialog() (Dialog, error) {
    d := Dialog{}
    d.Parser = NewParser()
    d.Parser.StartParsing() // <-- this instance is parsing
    return d, nil
}

func main() {
   dialog, _ := CreateDialog()
   // dialog.Parser <-- this is now a new instance which is NOT parsing
   dialog.OnMessage(func(m Message){
       log.Println("Message: ", m)
   })
}

Therefore to fix it you can do one of three:

1) Call StartParsing in main.

func main() {
    dialog, _ := CreateDialog()
    dialog.Parser.StartParsing();
    dialog.OnMessage(func(m Message){
        log.Println("Message: ", m)
    })
 }

2) Store Parser as a pointer in Dialog:

func NewParser() *Parser {
    p := &Parser{}
    p.test = 100
    p.callbackSet = make(chan bool)
    return p
}

type Dialog struct {
    Parser  *Parser
}

3) Return Dialog as a pointer in from CreateDialog:

func CreateDialog() (*Dialog, error) {
    d := &Dialog{}
    d.Parser = NewParser()
    d.Parser.StartParsing()
    return d, nil
}

That should fix it.

like image 196
kbirk Avatar answered Mar 25 '23 00:03

kbirk