fix(sandbox): pass desktop slot timing to the renew goroutine

renewDesktopSlot read desktopSlotRenew and desktopSlotLease from its own
goroutine. release() stops that goroutine without waiting for it, so the
read is unordered with the test cleanup that restores the values, and
`go test -race ./internal/handler/session/` fails in
TestDesktopSlotCancelsHoldWhenKeyExpires on every run.

acquireDesktopSlot now reads both durations before starting the goroutine
and passes them in; the go statement orders the reads, and the renew loop
no longer touches the package variables. Behaviour is unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
main
Victor Maznyuk 2026-09-26 09:45:58 +02:00 committed by lyingbug
parent 675b06addc
commit bad7afa6ae
1 changed files with 7 additions and 4 deletions

View File

@ -450,7 +450,9 @@ func (h *Handler) acquireDesktopSlot(
hold, cancelHold := context.WithCancelCause(context.Background())
renewCtx, stopRenew := context.WithCancel(context.WithoutCancel(ctx))
go renewDesktopSlot(renewCtx, cancelHold, h.redis, key, token)
// The timing is read here, before the goroutine starts, so the renew loop
// never reads the package-level settings concurrently with a writer.
go renewDesktopSlot(renewCtx, cancelHold, h.redis, key, token, desktopSlotRenew, desktopSlotLease)
var once sync.Once
return func() {
@ -469,8 +471,9 @@ func renewDesktopSlot(
cancelHold context.CancelCauseFunc,
client redis.UniversalClient,
key, token string,
renewEvery, lease time.Duration,
) {
ticker := time.NewTicker(desktopSlotRenew)
ticker := time.NewTicker(renewEvery)
defer ticker.Stop()
lastOK := time.Now()
for {
@ -478,13 +481,13 @@ func renewDesktopSlot(
case <-ctx.Done():
return
case <-ticker.C:
ok, rerr := redislock.Renew(ctx, client, key, token, desktopSlotLease)
ok, rerr := redislock.Renew(ctx, client, key, token, lease)
if rerr != nil {
if stderrors.Is(rerr, context.Canceled) && ctx.Err() != nil {
return
}
logger.Warnf(ctx, "[sandbox-desktop] slot renew failed key=%s: %v", key, rerr)
if time.Since(lastOK) >= desktopSlotLease {
if time.Since(lastOK) >= lease {
cancelHold(rerr)
return
}