英文:
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 <- mystruct
app.Debug(appctx.LogTagGen, "Posted mystruct to myChan")
}
func goroutinereceiver() {
for {
select {
case mystruct := <-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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论