Browse Source

fix: WaitUntilConnected now respects context cancellation during sleep (#7737)

The WaitUntilConnected function was not properly respecting context
cancellation when sleeping between attempts. The time.Sleep call would
block for up to 200ms even after the context was cancelled.

This fix uses select with time.After to immediately return when the
context is cancelled, rather than waiting for the sleep to complete.

This fixes flaky test behavior where the function would take ~200ms
to return instead of respecting the ~100ms context timeout.
pull/7739/head
Chris Lu 2 days ago
committed by GitHub
parent
commit
eb860752e6
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 9
      weed/wdclient/masterclient.go

9
weed/wdclient/masterclient.go

@ -367,7 +367,14 @@ func (mc *MasterClient) WaitUntilConnected(ctx context.Context) {
if attempts%100 == 0 { // Log every 100 attempts (roughly every 20 seconds)
glog.V(0).Infof("%s.%s WaitUntilConnected still waiting for master connection (attempt %d)...", mc.FilerGroup, mc.clientType, attempts)
}
time.Sleep(time.Duration(rand.Int31n(200)) * time.Millisecond)
// Use select with time.After to respect context cancellation during sleep
sleepDuration := time.Duration(rand.Int31n(200)) * time.Millisecond
select {
case <-ctx.Done():
return
case <-time.After(sleepDuration):
// continue to next iteration
}
}
}
}

Loading…
Cancel
Save