go-runtime不会将对象从缓冲通道中推出。

huangapple go评论90阅读模式
英文:

go-runtime doesn't push the object out of the buffered channel

问题

我有一个缓冲通道,其中包含一个自定义类型(用户定义的结构)。偶尔我会发现,尽管将对象推送到通道中,但并没有接收到。

在Delve调试器中,当我打印通道时,我看到1/100,表示通道中存在一个对象。此外,监视该通道的Go协程正在运行(在Delve调试器中转储的Go协程列表中可见)。Go运行时是否存在已知问题?

示例代码:

func sender() {
    myChan := make(chan mystruct, 10)
    myChan <- mystruct
    app.Debug(appctx.LogTagGen, "Posted mystruct to myChan")
}

func goroutinereceiver() {
    for {
        select {
        case mystruct := <-myChan:
            funcx(mystruct)
        }
    }
}

我可以在日志中看到消息"Posted mystruct to myChan"。此外,在Delve调试器中,我可以打印此通道以查看通道中存在的对象。同时,我看到接收器Go协程正在运行:

(dlv) print myChan :-
myChan {mystruct: chan *mystruct 1/100}

英文:

I have a buffered channel of a custom type(user defined structure). Occasionally I see that the object though pushed into the channel is not being received.

In delve debugger When i print the channel I see 1/100, meaning one object is present in the channel. Also, the go routines which is watching this channel is running(seen in the list of go-routines dumped in delve debugger). Is there a known issue with Go-Runtime ?

Sample code:-

func sender() {
    myChan := make(chan mystruct, 10)
	myChan &lt;- mystruct
	app.Debug(appctx.LogTagGen, &quot;Posted mystruct to myChan&quot;)
}


func goroutinereceiver() {
for {
	select {
	case mystruct := &lt;-myChan:
		funcx(mystruct)
	}
}
}

I can see the message "Posted mystruct to myChan" in the logs. Also, in the delve debugger I can print this channel to see the object present in the channel. Also, i see the receiver goroutine is running:-

> (dlv) print myChan :-
> myChan {mystruct: chan *mystruct 1/100}

答案1

得分: 0

发现问题:每当接收的 goroutine 在创建通道的 goroutine 之前执行时,当它遇到一个“nil 通道”时,接收的 goroutine 将进入无限休眠状态。这就是问题所在。尽管通道是在稍后创建的,但接收的 goroutine 已经处于无限休眠状态!这个问题不一定会一直出现,因为它取决于哪个 goroutine 先被调度。

英文:

Found out the issue:- Whenever the receiving goroutine is executed before the the goroutine which creates the channel. The receiving go routine will go for indefinite sleep when it hits a "nil channel". And that is the issue. Though the channel is created at a later point of time, the receiving goroutine is already in indefinite sleep! The issue was not consistently seen, as it depends on which goroutine is scheduled first.

huangapple
  • 本文由 发表于 2021年8月3日 20:23:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/68635973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定