From 8977f59dbda8a5a97d0801669e4c0d9f5a7088dd Mon Sep 17 00:00:00 2001 From: "liyuxuan.darfux" Date: Wed, 21 Sep 2022 16:26:41 +0800 Subject: [PATCH] server: Fix connection leak when receiving ECONNRESET 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 (cherry picked from commit a03aa0459192f20913133e3d1e71419414431c6f) Signed-off-by: Derek McGowan --- server.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/server.go b/server.go index b0e48073e..e4c07b60f 100644 --- a/server.go +++ b/server.go @@ -24,6 +24,7 @@ import ( "net" "sync" "sync/atomic" + "syscall" "time" "github.com/sirupsen/logrus" @@ -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 }