Skip to content

Commit

Permalink
cannot close twice for grpc
Browse files Browse the repository at this point in the history
  • Loading branch information
isayme committed May 14, 2024
1 parent aa50c8b commit 39882e8
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions cmd/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"io"
"net"
"sync"
"time"
Expand Down Expand Up @@ -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")

Expand All @@ -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() {
Expand Down

0 comments on commit 39882e8

Please sign in to comment.