taosdata / driver-go

taos go driver
MIT License
85 stars 34 forks source link

Tao驱动直接空指针 #228

Open yuelwish opened 7 months ago

yuelwish commented 7 months ago

环境: github.com/taosdata/driver-go/v3 v3.5.0 Ubuntu 22.04 LTS tdengine/tdengine:3.1.0.0

docker启动

复现场景: 当我td所在的服务器,我go build 过程中卡死之后, 就存在一定几率这样空指针。

初始化代码:


import (
    "database/sql"
    _ "github.com/taosdata/driver-go/v3/taosRestful"
    _ "github.com/taosdata/driver-go/v3/taosWS"
)

var DB *sql.DB

func Init(driverName, dataSourceName string) (err error) {
    DB, err = sql.Open(driverName, dataSourceName)
    if err != nil {
        return err
    }
    DB.SetMaxOpenConns(20)
    DB.SetMaxIdleConns(3)

    return nil
}

func Close() error {
    if DB == nil {
        return nil
    }
    defer func() { DB = nil }()
    return DB.Close()
}
2023/12/26 11:08:42.507356 [ants]: worker exits from panic: runtime error: invalid memory address or nil pointer dereference
goroutine 619618 [running]:
runtime/debug.Stack()
        runtime/debug/stack.go:24 +0x5e
github.com/panjf2000/ants/v2.(*goWorker).run.func1.1()
        github.com/panjf2000/ants/v2@v2.9.0/worker.go:56 +0x85
panic({0x849300?, 0xc47aa0?})
        runtime/panic.go:914 +0x21f
github.com/gorilla/websocket.(*Conn).SetWriteDeadline(...)
        github.com/gorilla/websocket@v1.5.0/conn.go:788
github.com/taosdata/driver-go/v3/taosWS.(*taosConn).writeText(0xc00023a740, {0xc000123200, 0x2b6, 0x900})
        github.com/taosdata/driver-go/v3@v3.5.0/taosWS/connection.go:264 +0x42
github.com/taosdata/driver-go/v3/taosWS.(*taosConn).execCtx(0xc00023a740, {0x0?, 0x0?}, {0xc0000e0781?, 0xc0001af568?}, {0xc84820?, 0x0?, 0xffffffffffffffff?})
        github.com/taosdata/driver-go/v3@v3.5.0/taosWS/connection.go:143 +0x1fb
github.com/taosdata/driver-go/v3/taosWS.(*taosConn).ExecContext(0x66000000822401?, {0x9700d0?, 0xc00007a230?}, {0xc0000e0781?, 0x4a0a65?}, {0xc84820?, 0x8224e0?, 0x43100000198?})
        github.com/taosdata/driver-go/v3@v3.5.0/taosWS/connection.go:110 +0x27
database/sql.ctxDriverExec({0x9700d0?, 0xc00007a230?}, {0x7f540080c398?, 0xc00023a740?}, {0x0?, 0x0?}, {0xc0000e0781?, 0xc0001af700?}, {0xc84820, 0x0, ...})
        database/sql/ctxutil.go:31 +0xd7
database/sql.(*DB).execDC.func2()
        database/sql/sql.go:1675 +0x165
database/sql.withLock({0x96e988, 0xc0003d9290}, 0xc0001af820)
        database/sql/sql.go:3502 +0x82
database/sql.(*DB).execDC(0xc0001af901?, {0x9700d0, 0xc00007a230}, 0xc0003d9290, 0x1?, {0xc0000e0781, 0x254}, {0x0, 0x0, 0x0})
        database/sql/sql.go:1670 +0x251
database/sql.(*DB).exec(0x81ea40?, {0x9700d0, 0xc00007a230}, {0xc0000e0781, 0x254}, {0x0, 0x0, 0x0}, 0x60?)
        database/sql/sql.go:1655 +0xdb
database/sql.(*DB).ExecContext.func1(0xfe?)
        database/sql/sql.go:1634 +0x4f
database/sql.(*DB).retry(0xc0001afa18?, 0xc0001afa18)
        database/sql/sql.go:1538 +0x42
database/sql.(*DB).ExecContext(0xc0000e0780?, {0x9700d0?, 0xc00007a230?}, {0xc0000e0781?, 0xf?}, {0x0?, 0x8013cc?, 0xc000096398?})
        database/sql/sql.go:1633 +0xc8
mana-script/example/fushan_temp/handler.tdSaveInstPower({0x9700d0, 0xc00007a230}, 0xc00010a5a0)
        mana-script/example/fushan_temp/handler/mqtt_sub_handler.go:143 +0x58d
mana-script/example/fushan_temp/handler.SubSaveHandler.func2({0x409512?, 0xc0014ac8a0?}, {0x971f08, 0xc00007aff0})
        mana-script/example/fushan_temp/handler/mqtt_sub_handler.go:37 +0x2d4
mana-script/pkg/xmqtt.Subscribe.func1.1()
        mana-script/pkg/xmqtt/mqtt.go:115 +0x2b
github.com/panjf2000/ants/v2.(*goWorker).run.func1()
        github.com/panjf2000/ants/v2@v2.9.0/worker.go:67 +0x8d
created by github.com/panjf2000/ants/v2.(*goWorker).run in goroutine 619595
        github.com/panjf2000/ants/v2@v2.9.0/worker.go:48 +0x5c
yinheli commented 2 months ago

看着是网络不稳定或者其他 case 导致了底层的 conn 实例被 close 导致的。我通过配置一个中间代理模拟网络很糟糕的情况可以复现你的问题。

我看了下代码这个缺相关的检查 我觉得可以参考 mysql 这样的健壮 driver 做类似的调整

https://github.com/go-sql-driver/mysql/blob/master/connection.go#L372

@huskar-t assign 给我试试?

huskar-t commented 2 months ago

@yinheli greet!you can create a pull request to the 3.0 branch and i will review it