multiformats / go-multistream

an implementation of the multistream protocol in go
MIT License
40 stars 27 forks source link

Race Condition When Using Websockets #49

Closed bonedaddy closed 4 years ago

bonedaddy commented 4 years ago

When peered up with about 15k peers, periodically there is a panic due to a concurrent websockets connection access.

See the following log

panic: concurrent write to websocket connection

goroutine 2809470 [running]:
github.com/gorilla/websocket.(*messageWriter).flushFrame(0xc0201ed680, 0xc020f72b01, 0x0, 0x0, 0x0, 0x7f05400c0f80, 0x0)
    /home/travis/gopath/pkg/mod/github.com/gorilla/websocket@v1.4.1/conn.go:610 +0x612
github.com/gorilla/websocket.(*messageWriter).Close(0xc0201ed680, 0xc008c78cf0, 0xc00f309d00)
    /home/travis/gopath/pkg/mod/github.com/gorilla/websocket@v1.4.1/conn.go:724 +0x62
github.com/gorilla/websocket.(*Conn).beginMessage(0xc01e7889a0, 0xc020f72b70, 0x2, 0xc00f309d68, 0xca427d)
    /home/travis/gopath/pkg/mod/github.com/gorilla/websocket@v1.4.1/conn.go:473 +0x284
github.com/gorilla/websocket.(*Conn).NextWriter(0xc01e7889a0, 0x2, 0xc00f309da8, 0xd5d161, 0xc008c78c40, 0x1001e95d229371c)
    /home/travis/gopath/pkg/mod/github.com/gorilla/websocket@v1.4.1/conn.go:513 +0x53
github.com/gorilla/websocket.(*Conn).WriteMessage(0xc01e7889a0, 0x2, 0xc0248f1580, 0x20, 0x40, 0x10, 0xc01fcfcfe0)
    /home/travis/gopath/pkg/mod/github.com/gorilla/websocket@v1.4.1/conn.go:766 +0x73
github.com/libp2p/go-ws-transport.(*Conn).Write(0xc0201ed320, 0xc0248f1580, 0x20, 0x40, 0x1178340, 0x12eee40, 0x7f0540081ec0)
    /home/travis/gopath/pkg/mod/github.com/libp2p/go-ws-transport@v0.1.2/conn.go:74 +0x59
bytes.(*Buffer).WriteTo(0xc020f72ab0, 0x7f05140b1358, 0xc0201ed5f0, 0x7f0540081ec0, 0xc020f72ab0, 0xcd4101)
    /home/travis/.gimme/versions/go1.13.4.linux.amd64/src/bytes/buffer.go:239 +0xb5
io.copyBuffer(0x7f05140b1358, 0xc0201ed5f0, 0x1606820, 0xc020f72ab0, 0x0, 0x0, 0x0, 0x8, 0x439416, 0x14a65c8)
    /home/travis/.gimme/versions/go1.13.4.linux.amd64/src/io/io.go:384 +0x33f
io.Copy(...)
    /home/travis/.gimme/versions/go1.13.4.linux.amd64/src/io/io.go:364
github.com/multiformats/go-multistream.SelectProtoOrFail.func1(0x132712c, 0xa, 0x7f05140b12c0, 0xc0201ed5f0, 0xc0195a6540)
    /home/travis/gopath/pkg/mod/github.com/multiformats/go-multistream@v0.1.0/client.go:27 +0x163
created by github.com/multiformats/go-multistream.SelectProtoOrFail
    /home/travis/gopath/pkg/mod/github.com/multiformats/go-multistream@v0.1.0/client.go:23 +0x95