Skip to content

Commit 153e78a

Browse files
committed
fix: add id lock to fix map leak
Signed-off-by: 宁明晓10296073 <ning.mingxiao@zte.com.cn>
1 parent 4b166fb commit 153e78a

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.25.0
55
require (
66
github.com/containerd/log v0.1.0
77
github.com/golang/protobuf v1.5.4
8+
github.com/moby/locker v1.0.1
89
github.com/prometheus/procfs v0.6.0
910
golang.org/x/sys v0.46.0
1011
google.golang.org/genproto/googleapis/rpc v0.0.0-20260226221140-a57be14db171

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6
88
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
99
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
1010
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
11+
github.com/moby/locker v1.0.1 h1:fOXqR41zeveg4fFODix+1Ch4mj/gT0NE1XJbp/epuBg=
12+
github.com/moby/locker v1.0.1/go.mod h1:S7SDdo5zpBK84bzzVlKr2V0hz+7x9hWbYC/kq7oQppc=
1113
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1214
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1315
github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4=

server.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package ttrpc
1919
import (
2020
"context"
2121
"errors"
22+
"fmt"
2223
"io"
2324
"math/rand"
2425
"net"
@@ -28,6 +29,7 @@ import (
2829
"time"
2930

3031
"github.com/containerd/log"
32+
"github.com/moby/locker"
3133
"google.golang.org/grpc/codes"
3234
"google.golang.org/grpc/status"
3335
)
@@ -289,6 +291,7 @@ func (s *Server) newConn(conn net.Conn, handshake interface{}) (*serverConn, err
289291
conn: conn,
290292
handshake: handshake,
291293
shutdown: make(chan struct{}),
294+
locker: locker.New(),
292295
}
293296
c.setState(connStateIdle)
294297
if err := s.addConnection(c); err != nil {
@@ -306,6 +309,7 @@ type serverConn struct {
306309

307310
shutdownOnce sync.Once
308311
shutdown chan struct{} // forced shutdown, used by close
312+
locker *locker.Locker
309313
}
310314

311315
func (c *serverConn) getState() (connState, bool) {
@@ -478,17 +482,22 @@ func (c *serverConn) run(sctx context.Context) {
478482
}
479483
return nil
480484
}
485+
lockID := fmt.Sprintf("%d", id)
486+
c.locker.Lock(lockID)
481487
sh, err := c.server.services.handle(ctx, &req, respond)
482488
if err != nil {
483489
status, _ := status.FromError(err)
484490
if !sendStatus(mh.StreamID, status) {
491+
c.locker.Unlock(lockID)
485492
return
486493
}
494+
c.locker.Unlock(lockID)
487495
continue
488496
}
489497

490498
streams.Store(id, sh)
491499
atomic.AddInt32(&active, 1)
500+
c.locker.Unlock(lockID)
492501
}
493502
// TODO: else we must ignore this for future compat. log this?
494503
}
@@ -547,8 +556,11 @@ func (c *serverConn) run(sctx context.Context) {
547556
// The ttrpc protocol currently does not support the case where
548557
// the server is localClosed but not remoteClosed. Once the server
549558
// is closing, the whole stream may be considered finished
559+
lockID := fmt.Sprintf("%d", response.id)
560+
c.locker.Lock(lockID)
550561
streams.Delete(response.id)
551562
atomic.AddInt32(&active, -1)
563+
c.locker.Unlock(lockID)
552564
}
553565
case err := <-recvErr:
554566
// TODO(stevvooe): Not wildly clear what we should do in this

0 commit comments

Comments
 (0)