AmarnathCJD / gogram

Full-native implementation of MTProto protocol on Golang.
GNU General Public License v3.0
207 stars 41 forks source link

Panic "send on closed channel" #118

Closed Ivanezko closed 3 months ago

Ivanezko commented 3 months ago

Describe the bug

panic: send on closed channel

goroutine 54774 [running]:
github.com/AmarnathCJD/gogram/internal/transport.(*CancelableReader).Read(0xc000037440, {0xc000574000, 0x188, 0x1146a60?})
        /go/pkg/mod/github.com/AmarnathCJD/gogram@v1.0.6/internal/transport/writer.go:47 +0x45
github.com/AmarnathCJD/gogram/internal/transport.(*tcpConn).Read(0xc0008f9800?, {0xc000574000?, 0x188?, 0x188?})
        /go/pkg/mod/github.com/AmarnathCJD/gogram@v1.0.6/internal/transport/connection.go:76 +0x8a
github.com/AmarnathCJD/gogram/internal/mode.(*abridged).ReadMsg(0xc00003f420)
        /go/pkg/mod/github.com/AmarnathCJD/gogram@v1.0.6/internal/mode/arbiged.go:92 +0x11e
github.com/AmarnathCJD/gogram/internal/transport.(*transport).ReadMsg(0xc000a2de30)
        /go/pkg/mod/github.com/AmarnathCJD/gogram@v1.0.6/internal/transport/transport.go:83 +0x2b
github.com/AmarnathCJD/gogram.(*MTProto).readMsg(0xc0001a3680)
        /go/pkg/mod/github.com/AmarnathCJD/gogram@v1.0.6/mtproto.go:541 +0x34
github.com/AmarnathCJD/gogram.(*MTProto).startReadingResponses.func1()
        /go/pkg/mod/github.com/AmarnathCJD/gogram@v1.0.6/mtproto.go:472 +0xc5
created by github.com/AmarnathCJD/gogram.(*MTProto).startReadingResponses in goroutine 40241
        /go/pkg/mod/github.com/AmarnathCJD/gogram@v1.0.6/mtproto.go:461 +0x90

To Reproduce Steps to reproduce the behavior: Start download videos 5 threads, 1Mb chunk After about 30 minutes and 5-6 downloaded videos get panic

AmarnathCJD commented 3 months ago

Alright I'll take a look

eos175 commented 3 months ago

This happens to me sometimes too, it's a race condition. We could check to send only if the channel is open, but there are other panics that need to be handled differently. I could work on this

func (c *CancelableReader) Read(p []byte) (int, error) {
    select {
    case <-c.ctx.Done():
        return 0, c.ctx.Err()
    case c.sizeWant <- len(p):
    }

    select {
    case <-c.ctx.Done():
    ...
AmarnathCJD commented 3 months ago

sure, for now Ii have added the check on close