Skip to content

server,controller: bound task reconciliation deadline under the task lock TTL - #483

Closed
aprajitaarora13 wants to merge 1 commit into
google:mainfrom
aprajitaarora13:fix/issue-473-task-lock-reconcile-deadline
Closed

aprajitaarora13 wants to merge 1 commit into
google:mainfrom
aprajitaarora13:fix/issue-473-task-lock-reconcile-deadline

Conversation

@aprajitaarora13

Copy link
Copy Markdown
Collaborator

Summary

Follow-up from #463 (review) and #468.

Because retryOnCodes in internal/substrate/client.go can retry codes.Unavailable and codes.Aborted for up to ~6s–20s per RPC, a single task reconciliation that encounters Substrate flapping across multiple sequential RPCs (EnsureAtespace, EnsureActorTemplateWithImage, EnsureActor, SuspendActor/ResumeActor, plus workspace polling) could exceed the fixed 30s Redis task lock TTL (lock.DefaultTTL) while still mutating Substrate and store state.

Changes

  • internal/lock/lock.go: Export lock.DefaultTTL = 30 * time.Second.
  • internal/server/server.go:
    • Define server.DefaultReconcileTimeout = lock.DefaultTTL - 5*time.Second (25s, configurable via server.Options.ReconcileTimeout).
    • Derive context.WithTimeout(ctx, s.reconcileTimeout) for Reconcile and ReconcileDelete in CreateTask, ResumeTask, SuspendTask, and DeleteTask after acquiring the task lock so an in-flight reconciliation aborts with context.DeadlineExceeded before the 30s lock expires, leaving headroom to persist the resulting task status while the lock is still held.
  • internal/controller/reconciler.go:
    • When SuspendActor fails transiently (or hits context.DeadlineExceeded) during initial task creation, record Phase: Pending and Ready=False (Reason: SubstrateUnavailable).
    • Exit the deleteTaskTemplates retry loop immediately when the context is canceled or expires.
  • internal/server/server_test.go & internal/controller/reconciler_test.go:
    • Add TestServer_ReconcileTimeoutBoundedUnderLockTTL covering reconcile timeout behavior across CreateTask, ResumeTask, SuspendTask, and DeleteTask.
    • Add reconcile deadline coverage in TestTaskReconciler_TransientSubstrateUnavailable.

Fixes #473

@aprajitaarora13 aprajitaarora13 self-assigned this Oct 15, 2026

@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 per-reconcile context deadlines to bound task reconciliation operations under the distributed lock TTL, preventing lock expiration issues. It also handles transient substrate errors during actor suspension by transitioning the task to a "Pending" phase with a "SubstrateUnavailable" status. Feedback on the changes suggests returning early from deleteTaskTemplates when the context is canceled or expired to avoid unnecessary subsequent RPC calls, rather than just breaking the inner retry loop.

Comment thread internal/controller/reconciler.go Outdated
Comment on lines 444 to 461
retryLoop:
for attempt := 0; attempt < 5; attempt++ {
delErr = r.client.DeleteActorTemplate(ctx, atespace, name)
if delErr == nil || status.Code(delErr) == codes.NotFound {
delErr = nil
break
}
if ctx.Err() != nil {
delErr = ctx.Err()
break
}
select {
case <-ctx.Done():
delErr = ctx.Err()
break
break retryLoop
case <-time.After(500 * time.Millisecond):
}
}

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.

medium

When the context is canceled or expired, continuing to iterate over the remaining templates in the outer loop is inefficient and will result in unnecessary, immediately-failing RPC calls. Instead of breaking the inner retry loop, we should return early from deleteTaskTemplates with the accumulated errors and the context error. This also allows us to simplify the code by removing the retryLoop label.

		for attempt := 0; attempt < 5; attempt++ {
			delErr = r.client.DeleteActorTemplate(ctx, atespace, name)
			if delErr == nil || status.Code(delErr) == codes.NotFound {
				delErr = nil
				break
			}
			if ctx.Err() != nil {
				return errors.Join(append(errs, ctx.Err())...)
			}
			select {
			case <-ctx.Done():
				return errors.Join(append(errs, ctx.Err())...)
			case <-time.After(500 * time.Millisecond):
			}
		}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done — updated deleteTaskTemplates to return early with errors.Join(append(errs, ctx.Err())...) when the context is canceled or expired.

…lock TTL

When Substrate flaps across multiple sequential RPCs during a single
reconciliation, in-flight retry loops (`retryOnCodes`) can accumulate
beyond the fixed 30s Redis task lock TTL (`lock.DefaultTTL`) while the
reconcile is still mutating Substrate and store state.

- Export `lock.DefaultTTL` (30s) and define `server.DefaultReconcileTimeout`
  (`lock.DefaultTTL - 5*time.Second` = 25s, configurable via
  `server.Options.ReconcileTimeout`).
- Bound `Reconcile` and `ReconcileDelete` calls in `CreateTask`,
  `ResumeTask`, `SuspendTask`, and `DeleteTask` with a derived
  `context.WithTimeout` so an in-flight reconciliation aborts with
  `context.DeadlineExceeded` before the task lock expires, leaving
  headroom to persist the resulting task status while the lock is still
  held.
- Mark initial task creation as `Pending` (`SubstrateUnavailable`) if
  `SuspendActor` fails transiently or hits the reconcile deadline, and
  exit the template cleanup loop immediately when the context expires.

Fixes google#473
@mayawang

Copy link
Copy Markdown
Collaborator

Thanks @aprajitaarora13, this is clean. Heads up that #474 adds lock-lease renewal, which changes the premise here (the lock no longer expires while work is running). Could you two agree on the combination? I'd lean to renewal as the base and this deadline as a looser cap, so legitimate long suspends aren't cut off.

@mayawang

Copy link
Copy Markdown
Collaborator

@aprajitaarora13 #490 makes the lock renew while work is in flight and raises the CLI's suspend/resume timeout to 5 minutes for slow microVM snapshots, so a 25s server-side cap would now cut those off. Could you close this in favor of #490, or keep only a much looser cap as a safety net? Your call.

@mayawang

Copy link
Copy Markdown
Collaborator

Closing this in favor of #490, which renews the lock while work is in flight. Thanks @aprajitaarora13, the deadline analysis was useful in getting there.

@mayawang mayawang closed this Oct 21, 2026
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.

server,controller: bound task reconciliation deadline under the task lock TTL

2 participants