Skip to content

Commit 860e282

Browse files
authored
fix: Prevent race between provisionerd connect and close (#6206)
* fix: Prevent race between provisionerd connect and close * test: Add detection for provisioner creation after test completion
1 parent cde7ff8 commit 860e282

File tree

2 files changed

+126
-31
lines changed

2 files changed

+126
-31
lines changed

provisionerd/provisionerd.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,17 @@ func (p *Server) connect(ctx context.Context) {
177177
p.opts.Logger.Warn(context.Background(), "failed to dial", slog.Error(err))
178178
continue
179179
}
180+
// Ensure connection is not left hanging during a race between
181+
// close and dial succeeding.
182+
p.mutex.Lock()
183+
if p.isClosed() {
184+
client.DRPCConn().Close()
185+
p.mutex.Unlock()
186+
break
187+
}
180188
p.clientValue.Store(client)
189+
p.mutex.Unlock()
190+
181191
p.opts.Logger.Debug(context.Background(), "connected")
182192
break
183193
}
@@ -390,7 +400,8 @@ func retryable(err error) bool {
390400
// is not retryable() or the context expires.
391401
func (p *Server) clientDoWithRetries(
392402
ctx context.Context, f func(context.Context, proto.DRPCProvisionerDaemonClient) (any, error)) (
393-
any, error) {
403+
any, error,
404+
) {
394405
for retrier := retry.New(25*time.Millisecond, 5*time.Second); retrier.Wait(ctx); {
395406
client, ok := p.client()
396407
if !ok {

0 commit comments

Comments
 (0)