Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic on server shutdown #633

Merged
merged 2 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions pkg/router/route_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ type RouteGroup struct {
// 'readCh' reads in incoming packets of this route group.
// - Router should serve call '(*transport.Manager).ReadPacket' in a loop,
// and push to the appropriate '(RouteGroup).readCh'.
readCh chan []byte // push reads from Router
readChMu sync.Mutex
readBuf bytes.Buffer // for read overflow
readCh chan []byte // push reads from Router
readBuf bytes.Buffer // for read overflow

readDeadline deadline.PipeDeadline
writeDeadline deadline.PipeDeadline
Expand Down Expand Up @@ -558,12 +557,8 @@ func (rg *RouteGroup) close(code routing.CloseCode) error {
if closeInitiator {
close(rg.closed)
}

rg.setRemoteClosed()

rg.readChMu.Lock()
close(rg.readCh)
rg.readChMu.Unlock()
})

return nil
Expand Down Expand Up @@ -621,6 +616,11 @@ func (rg *RouteGroup) handleDataPacket(packet routing.Packet) error {
select {
case <-rg.closed:
return io.ErrClosedPipe
case <-rg.remoteClosed:
// in this case remote is already closed, and `readCh` is closed too,
// but some packets may still reach the rg causing panic on writing
// to `readCh`, so we simple omit such packets
return nil
case rg.readCh <- packet.Payload():
}

Expand Down
3 changes: 0 additions & 3 deletions pkg/router/route_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,9 +593,6 @@ func safeSend(ctx context.Context, to *RouteGroup, payload []byte) (keepSending
}
}()

to.readChMu.Lock()
defer to.readChMu.Unlock()

select {
case <-ctx.Done():
return false
Expand Down