Skip to content

lock,cli: renew Redis lock leases and lengthen suspend/resume timeout - #490

Merged
siyuanfoundation merged 2 commits into
google:mainfrom
siyuanfoundation:lock-lease-renewal
Oct 16, 2026
Merged

siyuanfoundation merged 2 commits into
google:mainfrom
siyuanfoundation:lock-lease-renewal

Conversation

@siyuanfoundation

Copy link
Copy Markdown
Collaborator

Summary

Split out from #474 per review discussion (#474 (comment)).

Suspend/resume hold the per-task lock across Substrate snapshot/restore, which for microVM sandboxes can exceed both the CLI's 15s deadline and the 30s Redis lock TTL. Once the TTL lapsed, another request could take the lock while the first was still mutating the task.

  • Redis lock renewal: RedisLocker now renews a held lease every TTL/3 (token-checked via Lua script) until unlock, so the TTL only bounds how long a crashed holder's lock lingers. The rationale for renewal over a single long TTL is documented on RedisLocker.
  • CLI timeout: Raise the CLI suspend/resume RPC timeout to 5 minutes (lifecycleRPCTimeout).
  • Tests: Add a Redis-backed renewal test (TestRedisLocker_RenewsLeaseWhileHeld), skipped unless REDIS_ADDR is set.

Relates to #483.

Suspend/resume hold the per-task lock across Substrate snapshot/restore,
which for microVM sandboxes can exceed both the CLI's 60s deadline and
the 30s Redis lock TTL. Once the TTL lapsed, another request could take
the lock while the first was still mutating the task.

- RedisLocker now renews a held lease every TTL/3 (token-checked) until
  unlock, so the TTL only bounds how long a crashed holder's lock lingers.
  The rationale for renewal over a single long TTL is documented on
  RedisLocker.
- Raise the CLI suspend/resume RPC timeout to 5 minutes.
- Add a Redis-backed renewal test, skipped unless REDIS_ADDR is set.

Signed-off-by: siyuanfoundation <[email protected]>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a background lease renewal mechanism for RedisLocker to keep held locks alive, and increases the suspend/resume RPC timeouts to 5 minutes. The review feedback points out a potential blocking issue in the unlock function if the Redis client is slow or hung, because the renewal goroutine uses a non-cancelable context for its Redis calls. It is recommended to pass a cancelable context to the renewal goroutine instead of a stop channel to ensure active Redis operations can be canceled immediately during unlock.

Comment thread internal/lock/lock.go Resolved
Comment thread internal/lock/lock.go Resolved

@rich7420 rich7420 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified the renewal/unlock ordering and test error paths locally against Redis. These suggestions address the confirmed cases; paired suggestions should be applied in one batch.

Comment thread internal/lock/lock.go Resolved
Comment thread internal/lock/lock.go Resolved
Comment thread internal/lock/lock_test.go Resolved
Comment thread internal/lock/lock_test.go Resolved

@mayawang mayawang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @siyuanfoundation, renewal with the token check is the right fix, and documenting the trade-off and the fencing limitation is really helpful. Approving.

- Pass a cancelable context into RedisLocker.renew instead of a stop channel
  so unlock() immediately cancels any in-flight renewal RPC and exits quietly.
- Keep <-done synchronization so the renewal goroutine finishes before unlock
  returns.
- Assert context.DeadlineExceeded on contender lock attempt in
  TestRedisLocker_RenewsLeaseWhileHeld.
@siyuanfoundation

Copy link
Copy Markdown
Collaborator Author

Thanks @mayawang and @rich7420 for the reviews!

Updated in 96709cb:

  • Cancelable renewal context (internal/lock/lock.go): Replaced the stop channel with a cancelable context.Context passed into renew, deriving the per-renewal RPC timeout from it and returning quietly when ctx.Err() != nil (while keeping <-done synchronization so the renewal goroutine always terminates before unlock() returns).
  • Tighter test assertion (internal/lock/lock_test.go): Asserted errors.Is(err, context.DeadlineExceeded) on the contender's lock attempt while the lease is held.

@mayawang mayawang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approving with the cancelable renewal context, tests pass on main including -race on the lock package. Thanks @rich7420 for testing this against Redis. On keeping the wait in unlock: I'm fine with it, since the release itself goes to the same Redis and would stall on a hung connection anyway, and the token checks keep both orders safe.

@siyuanfoundation
siyuanfoundation merged commit 8d2ccf3 into google:main Oct 16, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants