golang fatal error: all goroutines are asleep - deadlock!

channel默认上是阻塞的,也就是说,如果Channel满了,就阻塞写,如果Channel空了,就阻塞读。阻塞的含义就是一直等到轮到它为止。单有时候我们会收到 fatal error: all goroutines are asleep - deadlock!  异常,这是如何呢?

代码例子:

package main

import "fmt"

func main() {
    channel := make(chan string, 2)

    fmt.Println("1")
    channel <- "h1"
    fmt.Println("2")
    channel <- "w2"
    fmt.Println("3")
    channel <- "c3"    // 执行到这一步,直接报 error
    fmt.Println("...")
    msg1 := <-channel
    fmt.Println(msg1)
}

执行效果:

参考:

http://stackoverflow.com/questions/26927479/go-language-fatal-error-all-goroutines-are-asleep-deadlock

 

fatal error: all goroutines are asleep - deadlock!

出错信息的意思是:
在main goroutine线,期望从管道中获得一个数据,而这个数据必须是其他goroutine线放入管道的
但是其他goroutine线都已经执行完了(all goroutines are asleep),那么就永远不会有数据放入管道。
所以,main goroutine线在等一个永远不会来的数据,那整个程序就永远等下去了。
这显然是没有结果的,所以这个程序就说“算了吧,不坚持了,我自己自杀掉,报一个错给代码作者,我被deadlock了”

 

这里是系统自动在除了主协程之外的协程都关闭后,做的检查,继而报出的错误, 证明思路如下, 在100秒内, 我们看不到异常, 100秒后,系统报错。

package main

import (
    "fmt"
    "time"
)

func main() {
    channel := make(chan string, 2)

    go func() {
        fmt.Println("sleep 1")
        time.Sleep(100 * time.Second)
        fmt.Println("sleep 2")
    }()

    fmt.Println("1")
    channel <- "h1"
    fmt.Println("2")
    channel <- "w2"

    fmt.Println("3")
    channel <- "c3"

    fmt.Println("...")
    msg1 := <-channel
    fmt.Println(msg1)
}

100秒内执行效果截图:

100秒后执行效果截图:

 

如果避免上面异常抛出呢?这时候我们可以用 select来帮我们处理。

package main

import "fmt"

func main() {
    channel := make(chan string, 2)

    fmt.Println("1")
    channel <- "h1"
    fmt.Println("2")
    channel <- "w2"

    fmt.Println("3")
    select {

    case channel <- "c3":
        fmt.Println("ok")
    default:
        fmt.Println("channel is full !")
    }

    fmt.Println("...")
    msg1 := <-channel
    fmt.Println(msg1)
}

执行效果:

这时候,我们把第三个要写入的 chan 抛弃了。

 

上面的例子中是写的例子, 读的例子也一样,下面的异常是  ws := <-channel 这一行抛出的。

channel := make(chan string, 2)

fmt.Println("begin")
ws := <-channel
fmt.Println(ws)

时间: 2024-08-11 11:35:06

golang fatal error: all goroutines are asleep - deadlock!的相关文章

golang fatal error: concurrent map read and map write

调试程序的时候,为了打印map中的内容 ,直接 使用seelog 的方法打印 map中的内容到日志,结果出现 "concurrent map read and map write"的错误,导致程序异常退出,后来将代码注释后恢复正常.猜想了下是log 打印属于写操作,取出map内容的时候属于读操作,log记录的时候产生lock引发异常. 具体细节就没研究,先mark下.

FATAL ERROR: Could not find ./bin/my_print_default

网上很多方法都是:/usr/local/mysql/scripts/mysql_install_db --user=mysql 但是很有可能报错,找不到bin目录中的my_print_defaults 错误信息: FATALERROR:Couldnotfind./bin/my_print_defaults If you are using a binary release, you must run this script from within the directory the archiv

PHP配置问题:AppServ安装discuz出错 Fatal error:

 Fatal error: Call to undefined function set_magic_quotes_runtime() in  D:\AppServ\www\wp-settings.php on line 27 打开C:\WINDOWS\php.ini 大约在 428 行位置,将magic_quotes_runtime 改为 Off.也就是 magic_quotes_runtime = Off 如果前面有注释的分号,也将它删掉.然后重启 Apache 服务. 打开你的 wp 目录

python pip fatal error in launcher unable to create process using

用pip安装一个包,不知道为啥,就报了这个错误:python pip fatal error in launcher unable to create process using “” 百度了一下,用下面的命令可以解决,搞定 python2 -m pip install XXX  另外,Python3 的pip我用python3 -m pip install --upgrade pip 搞定

【MySQL】Got fatal error 1236原因和解决方法

一 前言  MySQL 的主从复制作为一项高可用特性,用于将主库的数据同步到从库,在维护主从复制数据库集群的时候,作为专职的MySQL DBA,笔者相信大多数人都会遇到"Got fatal error 1236 from master when reading data from binary log" 这类的报错/报警.本文整理了常见的几种 error 1236 报错,并给出相应的解决方法,有所不足之处,当然也希望各位读者朋友指正. 二 常见的error 1236 报错2.1 log

PHP-问题处理Fatal error: Uncaught Error: Call to undefined function simplexml_load_file()

1.问题 今天重新安装了ubuntu,PHP,MySQL,Apache,到测试CMS项目时发生一个错误: Fatal error: Uncaught Error: Call to undefined function simplexml_load_file() 2.解决: 2.1 安装php-xml: sudo apt-get install php-xml 2.2重启apache服务: sudo service apache2 restart

Fatal error: Using $this when not in object context in C:\AppServ\www\ABC\model\Model.class.php on line 7

原文转发:http://hi.baidu.com/zwnjiejie/blog/item/5e6555c23fa302120ff477dd.html Fatal error Using $this when not in object context in D:\xampp\htdocs\test\php\service\FileCommand.php 大致意思是 $this 没有上下文,原因是没有对此类进行实例化. 出现此错误的原因是:在FileCommand.php中使用 $this->方法

“fatal error: hdf5.h: 没有那个文件或目录”解决方法

問題一: Installing Caffe without CUDA: fatal error: cublas_v2.h No such file: 在Makefile.config中修改,將CPU_ONLY := 1打開. 問題二:“fatal error: hdf5.h: 没有那个文件或目录”解决方法 参考自http://blog.csdn.net/hongye000000/article/details/51043913 Step 1 在Makefile.config文件的第85行,添加/

Fatal error: Maximum function nesting level of &#39;100&#39; reached, aborting!

今天访问PHP网站如下以下错误: Fatal error: Maximum function nesting level of '100' reached, aborting! 原因分析: 这个问题是由于你启用了xdebug,而xdebug默认设置了函数最大嵌套数为100   解决办法: 找到php.ini文件,找到xdebug 在最后加上xdebug.max_nesting_level = 500 (这个数最好大于100即可) 转载:http://blog.sina.com.cn/s/blog