登录
首页 >  Golang >  Go问答

包“fmt”运行时问题

来源:stackoverflow

时间:2024-04-21 19:18:35 144浏览 收藏

golang学习网今天将给大家带来《包“fmt”运行时问题》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习Golang或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!

问题内容

我遇到了一个看似简单但无法重现的问题,因此我无法解释。

这个问题发生在生产中,神秘的是它很少发生(这就是为什么我无法重现它),这可能是我无法举例说明的一个因素,但上下文如下:

type mytype struct {
    field1 string
    field2 int
    field3 time.time
    field4 []float64
    // no pointers fields
}

func main() {
    var mychan = make(chan interface{})

    go func() {
// this routine is reading and parsing messages from a ws dial and writing it in a channel
// there is 2 only possible answer in the channel : a string, or a "mytype" like (with more fields, but no pointers in)     
        var object mytype
        object.field1 = "test"
        // ..

        mychan <- object
    }()

    go func() {
// this routine is sending a message to a ws dial and wait for the answer in a channel fed by another routine :
        var object interface{}
        go func(get *interface{}) {
            *get = <- mychan
        } (&object)
        for object == nil {
            time.sleep(time.nanosecond * 1)
        }
        log.println(fmt.sprint(object)) // panic here from the fmt.sprint() func
    }()
}

紧急堆栈跟踪:

runtime error: invalid memory address or nil pointer dereference
panic(0x87a840, 0xd0ff40)
    X:/Go/GoRoot/src/runtime/panic.go:522 +0x1b5
reflect.Value.String(0x85a060, 0x0, 0xb8, 0xc00001e500, 0xc0006f1a80)
    X:/Go/GoRoot/src/reflect/value.go:1781 +0x45
fmt.(*pp).printValue(0xc006410f00, 0x85a060, 0x0, 0xb8, 0x76, 0x1)
    X:/Go/GoRoot/src/fmt/print.go:747 +0x21c3
fmt.(*pp).printValue(0xc006410f00, 0x8ed5c0, 0x0, 0x99, 0x76, 0x0)
    X:/Go/GoRoot/src/fmt/print.go:796 +0x1b52
fmt.(*pp).printArg(0xc006410f00, 0x8ed5c0, 0x0, 0x76)
    X:/Go/GoRoot/src/fmt/print.go:702 +0x2ba
fmt.(*pp).doPrint(0xc006410f00, 0xc0006f22a0, 0x1, 0x1)
    X:/Go/GoRoot/src/fmt/print.go:1147 +0xfd
fmt.Sprint(0xc0006f22a0, 0x1, 0x1, 0x0, 0x0)
    X:/Go/GoRoot/src/fmt/print.go:250 +0x52

go 版本:1.12.1 windows/amd64

感谢您的宝贵时间,我希望有人能向我解释一下出了什么问题。


解决方案


这里有一场数据竞赛:

var object interface{}
    go func(get *interface{}) {
        *get = <- mychan
    } (&object)
    for object == nil {
        time.sleep(time.nanosecond * 1)
    }

写入变量 object 的 goroutine 不使用锁:它从通道接收数据,当它获得一个值时,将其写入 *get,其中 get == &object,以便写入接口object 的值。

同时,运行 for 循环的 goroutine 从 object 读取,检查是否为零。它在不使用锁的情况下读取,因此可以读取部分写入的值。

实际发生的情况是部分写入的值非零,而没有设置整个值。因此 for 循环停止循环,代码进入下一行:

log.println(fmt.sprint(object)) // panic here from the fmt.sprint() func

由于 object部分写入,因此访问其值会产生不可预测的结果,但在本例中,它会产生恐慌。 (特别是,接口的 type 字段已设置,但 value 字段仍然为零。实际的恐慌来自 src/reflect/value 中的这一行。 go

return *(*string)(v.ptr)

v.ptr 尚未设置。)

不清楚为什么要记录该值,也不清楚为什么使用共享内存进行通信,但如果您这样做,则需要锁定。通常不这样做是明智的。另请参阅 this answerExplain: Don't communicate by sharing memory; share memory by communicating

(或者,更简单地说,为什么不直接使用 object := <-mychan 来代替整个 goroutine-and-spin?)

以上就是《包“fmt”运行时问题》的详细内容,更多关于的资料请关注golang学习网公众号!

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