Skip to content

Commit

Permalink
server: Fix connection leak when receiving ECONNRESET
Browse files Browse the repository at this point in the history
The ttrpc server somtimes receives `ECONNRESET` rather than `EOF` when
the client is exited. Such as reading from a closed connection. Handle
it properly to avoid goroutine and connection leak.

Change-Id: If32711cfc1347dd2da27ca846dd13c3f5af351bb
Signed-off-by: liyuxuan.darfux <liyuxuan.darfux@bytedance.com>
(cherry picked from commit a03aa04)
Signed-off-by: Derek McGowan <derek@mcg.dev>
  • Loading branch information
darfux authored and dmcgowan committed Mar 24, 2023
1 parent 0247db1 commit 8977f59
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net"
"sync"
"sync/atomic"
"syscall"
"time"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -467,14 +468,12 @@ func (c *serverConn) run(sctx context.Context) {
// branch. Basically, it means that we are no longer receiving
// requests due to a terminal error.
recvErr = nil // connection is now "closing"
if err == io.EOF || err == io.ErrUnexpectedEOF {
if err == io.EOF || err == io.ErrUnexpectedEOF || errors.Is(err, syscall.ECONNRESET) {
// The client went away and we should stop processing
// requests, so that the client connection is closed
return
}
if err != nil {
logrus.WithError(err).Error("error receiving message")
}
logrus.WithError(err).Error("error receiving message")
case <-shutdown:
return
}
Expand Down

0 comments on commit 8977f59

Please sign in to comment.