Skip to content

Commit 4e4b7a8

Browse files
authored
Merge pull request moby#3881 from tonistiigi/session-sync-fix
2 parents 87afda3 + 64580e7 commit 4e4b7a8

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

session/session.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"net"
66
"strings"
7+
"sync"
78

89
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
910
"github.com/moby/buildkit/identity"
@@ -36,14 +37,16 @@ type Attachable interface {
3637

3738
// Session is a long running connection between client and a daemon
3839
type Session struct {
39-
id string
40-
name string
41-
sharedKey string
42-
ctx context.Context
43-
cancelCtx func()
44-
done chan struct{}
45-
grpcServer *grpc.Server
46-
conn net.Conn
40+
mu sync.Mutex // synchronizes conn run and close
41+
id string
42+
name string
43+
sharedKey string
44+
ctx context.Context
45+
cancelCtx func()
46+
done chan struct{}
47+
grpcServer *grpc.Server
48+
conn net.Conn
49+
closeCalled bool
4750
}
4851

4952
// NewSession returns a new long running session
@@ -99,6 +102,11 @@ func (s *Session) ID() string {
99102

100103
// Run activates the session
101104
func (s *Session) Run(ctx context.Context, dialer Dialer) error {
105+
s.mu.Lock()
106+
if s.closeCalled {
107+
s.mu.Unlock()
108+
return nil
109+
}
102110
ctx, cancel := context.WithCancel(ctx)
103111
s.cancelCtx = cancel
104112
s.done = make(chan struct{})
@@ -118,22 +126,27 @@ func (s *Session) Run(ctx context.Context, dialer Dialer) error {
118126
}
119127
conn, err := dialer(ctx, "h2c", meta)
120128
if err != nil {
129+
s.mu.Unlock()
121130
return errors.Wrap(err, "failed to dial gRPC")
122131
}
123132
s.conn = conn
133+
s.mu.Unlock()
124134
serve(ctx, s.grpcServer, conn)
125135
return nil
126136
}
127137

128138
// Close closes the session
129139
func (s *Session) Close() error {
140+
s.mu.Lock()
130141
if s.cancelCtx != nil && s.done != nil {
131142
if s.conn != nil {
132143
s.conn.Close()
133144
}
134145
s.grpcServer.Stop()
135146
<-s.done
136147
}
148+
s.closeCalled = true
149+
s.mu.Unlock()
137150
return nil
138151
}
139152

0 commit comments

Comments
 (0)