dongli7870 2016-06-20 17:44
浏览 38
已采纳

Golang:具有函数类型的奇怪行为

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

  • 写回答

1条回答 默认 最新

  • dousui6193 2016-06-20 18:11
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败