Skip to content

panic: read websocket after it was closed #14

@keeword

Description

@keeword

I write a example like that:

package main

import (
	aria2 "github.com/siku2/arigo"
)

func Get() error {
	url := "ws://127.0.0.1:6800/jsonrpc"
	conn, err := aria2.Dial(url, "")
	if err != nil {
		return err
	}
	defer conn.Close()

	if _, err := conn.TellActive("gid"); err != nil {
		return err
	}

	return nil
}

func main() {
	for {
		if err := Get(); err != nil {
			panic(err)
		}
	}
}

And got panic:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0xc0 pc=0x63757d]

goroutine 34 [running]:
github.com/gorilla/websocket.(*Conn).NextReader(0x0)
        /root/go/pkg/mod/github.com/gorilla/websocket@v1.5.0/conn.go:1000 +0x1d
github.com/siku2/arigo/internal/pkg/wsrpc.(*ReadWriteCloser).Read(0xc0002061b0, {0xc0001a0200, 0x200, 0x200})
        /root/go/pkg/mod/github.com/siku2/arigo@v0.2.1-0.20221203162025-97cc8d79dcf4/internal/pkg/wsrpc/rwc.go:26 +0x45
encoding/json.(*Decoder).refill(0xc00022e000)
        /usr/local/go/src/encoding/json/stream.go:165 +0x188
encoding/json.(*Decoder).readValue(0xc00022e000)
        /usr/local/go/src/encoding/json/stream.go:140 +0xbb
encoding/json.(*Decoder).Decode(0xc00022e000, {0x652b20, 0xc000230020})
        /usr/local/go/src/encoding/json/stream.go:63 +0x78
github.com/cenkalti/rpc2/jsonrpc.(*jsonCodec).ReadHeader(0xc000230000, 0xc0000121c8, 0xc0000121e0)
        /root/go/pkg/mod/github.com/cenkalti/rpc2@v0.0.0-20180727162946-9642ea02d0aa/jsonrpc/jsonrpc.go:93 +0x85
github.com/cenkalti/rpc2.(*Client).readLoop(0xc000216070)
        /root/go/pkg/mod/github.com/cenkalti/rpc2@v0.0.0-20180727162946-9642ea02d0aa/client.go:87 +0x12d
github.com/cenkalti/rpc2.(*Client).Run(...)
        /root/go/pkg/mod/github.com/cenkalti/rpc2@v0.0.0-20180727162946-9642ea02d0aa/client.go:62
github.com/siku2/arigo.(*Client).Run(0x0?)
        /root/go/pkg/mod/github.com/siku2/arigo@v0.2.1-0.20221203162025-97cc8d79dcf4/client.go:97 +0x1c
created by github.com/siku2/arigo.DialContext
        /root/go/pkg/mod/github.com/siku2/arigo@v0.2.1-0.20221203162025-97cc8d79dcf4/client.go:82 +0x40a
exit status 2

Maybe we shoule use a Mutex to protect the websocket from reading after it was closed?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions