登录
首页 >  Golang >  Go问答

无法从恐慌中恢复 - 完全跳过延迟

来源:stackoverflow

时间:2024-04-23 20:00:34 120浏览 收藏

“纵有疾风来,人生不言弃”,这句话送给正在学习Golang的朋友们,也希望在阅读本文《无法从恐慌中恢复 - 完全跳过延迟》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新Golang相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!

问题内容

当使用 io.copy 与无效的编写器时,我会感到恐慌 - 这是预料之中的。但在这种情况下我无法恢复。我的延迟恢复被完全绕过了。这是代码:

package main

import (
    "context"
    "fmt"
    "io"
    "log"
    "os"
    "runtime"
    "runtime/debug"

    "cloud.google.com/go/storage"
)

func main() {
    var (
        ctx      = context.background()
        fromfile = "blah.txt"
        bucket   = "blah-bucket"
        path     = "blah-path"
    )

    defer func() {
        if result := recover(); result != nil {
            buf := make([]byte, 1<<16)
            length := runtime.stack(buf, false)
            log.fatalf("panic recover: %v\nstack: \n%s", result, buf[:length])
            debug.printstack()
        }
    }()

    err := fakeupload(ctx, fromfile, bucket, path)
    if err != nil {
        fmt.println(err)
    }

    fmt.println("hello")
}

func fakeupload(ctx context.context, fromfile, tobucket, topath string) (err error) {
    var (
        file   *os.file
        client *storage.client
        wc     *storage.writer
    )

    defer func() {
        for _, c := range []io.closer{wc, file} {
            if c != nil {
                err = c.close()
                if err != nil {
                    return
                }
            }
        }
    }()

    file, err = os.open(fromfile)
    if err != nil {
        err = fmt.errorf("problem opening file %v: %v", fromfile, err)
        return
    }

    wc = client.bucket(tobucket).object(topath).newwriter(ctx)

    _, err = io.copy(wc, file) // the unrecoverable panic happens here

    return
}

恐慌是:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x9e0fa8]

goroutine 21 [running]:
cloud.google.com/go/storage.(*writer).open.func1(0xc000161200, 0xc0002721e0, 0xc000150388, 0xc000290e20, 0x1, 0x1)
    c:/users/xxxx/go/pkg/mod/cloud.google.com/go/[email protected]/writer.go:128 +0x248
created by cloud.google.com/go/storage.(*writer).open
    c:/users/xxxx/go/pkg/mod/cloud.google.com/go/[email protected]/writer.go:118 +0x6ce
process exiting with code: 0
module main

go 1.15

require cloud.google.com/go/storage v1.14.0
go version go1.15.10 windows/amd64

最重要的是,如果它在其他地方发生恐慌,例如我指向一个无效文件,它就会发生恐慌,并且延迟恢复会正确捕获它。

这让我很困惑。有什么想法吗?


正确答案


正如您在上面的评论中看到的那样,答案是 cloud.google.com/go/storage writer 正在创建一个 goroutine,并在其中引发恐慌。 GO 不允许你从另一个 goroutine 中恢复。

终于介绍完啦!小伙伴们,这篇关于《无法从恐慌中恢复 - 完全跳过延迟》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>