From bad7afa6ae3198b8e574c94614e41267dbe6e005 Mon Sep 17 00:00:00 2001 From: Victor Maznyuk Date: Sat, 26 Sep 2026 09:45:58 +0200 Subject: [PATCH] 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) --- internal/handler/session/sandbox_desktop_ws.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/internal/handler/session/sandbox_desktop_ws.go b/internal/handler/session/sandbox_desktop_ws.go index 586d8aa8..72af0b00 100644 --- a/internal/handler/session/sandbox_desktop_ws.go +++ b/internal/handler/session/sandbox_desktop_ws.go @@ -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 }