From 39882e83d1dd22907dbddbd123915d2bfb6632c3 Mon Sep 17 00:00:00 2001 From: isayme Date: Tue, 14 May 2024 23:47:11 +0800 Subject: [PATCH] cannot close twice for grpc --- cmd/local.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/cmd/local.go b/cmd/local.go index 92a379f..929f038 100644 --- a/cmd/local.go +++ b/cmd/local.go @@ -2,6 +2,7 @@ package cmd import ( "context" + "io" "net" "sync" "time" @@ -66,7 +67,12 @@ func handleConnection(conn net.Conn, tc tunnel.Client) { logger.Errorw("connect tunnel server fail", "err", err) return } - defer remote.Close() + var once sync.Once + defer func() { + once.Do(func() { + remote.Close() + }) + }() logger.Debug("connect tunnel server ok") @@ -80,7 +86,11 @@ func handleConnection(conn net.Conn, tc tunnel.Client) { var n int64 n, err = util.CopyBuffer(remote, conn) logger.Debugw("copy from client end", "n", n, "err", err) - remote.Close() + if err != nil && err != io.EOF { + once.Do(func() { + remote.Close() + }) + } }() go func() {