Skip to content

Keep tasks pending until Substrate worker capacity becomes available - #465

Closed
erain wants to merge 3 commits into
google:mainfrom
erain:fix/367-wait-for-worker
Closed

erain wants to merge 3 commits into
google:mainfrom
erain:fix/367-wait-for-worker

Conversation

@erain

@erain erain commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #367.

A task that cannot find a free Substrate worker now waits and retries instead of becoming permanently Failed.

When a user resumes a task:

  1. AX tries to start its actor on a worker.
  2. If Substrate reports no capacity, AX saves the task as Pending with Ready=False, reason=WaitingForWorker. The CLI reports that it is waiting for a worker.
  3. The server retries after 1 second, then 2 and 4 seconds, with later delays capped at 5 seconds. Once capacity is available, the task moves to Running.

The shorter default cap helps waiting tasks use newly freed workers promptly. Operators can tune both delays with ax-server flags:

ax-server --schedule-retry-interval=1s --schedule-retry-max-delay=5s

The interval must be positive and the maximum must be at least the interval. Invalid settings fail at startup. These settings control retry pacing, not how long a task may wait for capacity.

Waiting tasks remain in the existing store, so they survive a server restart. Each retry checks the latest task state under its resource lock: suspending or deleting a waiting task cancels its pending resume. Other errors still follow the existing failure behavior.

The change adds the retry loop, regression tests, and a live test runnable with make test-e2e-scheduling. Setup instructions are in test/e2e/README.md.

End-to-end test

The test uses a real AX server, Redis, and Substrate. It creates an isolated pool with one worker and four tasks whose actors each need the worker's full CPU capacity.

  • Start all four: one should run and three should stay Pending across retries.
  • Suspend one waiting task and delete another: neither should start later. Restart AX: the remaining task should still be waiting.
  • Delete the running task: the remaining task should start automatically and successfully write and read a file inside its sandbox.
  • Resume the suspended task and scale the pool to two workers: that task should also start.

All scenarios passed. The fixture pre-creates actors to pin them to the test pool; generated-template compatibility is outside this test's coverage.

Worker exhaustion and recovery

make test, targeted race tests, go vet ./..., the build, and ShellCheck passed. Tests cover the default and custom retry timings, invalid configuration, and the actual retry loop. The live scenarios above also passed with the new defaults. Module files are unchanged.

Implemented with AI coding assistance.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@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 @erain, keeping the store as the source of truth and re-checking under the lock is solid design, and the live e2e test is great. #468 builds on your loop to cover #463's case too.

}
}

func waitingForWorker(task *v1alpha1.Task) bool {

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.

#463 will need a SubstrateUnavailable reason picked up here too, so it'd help to make this a small set of waiting reasons rather than one hard-coded reason.

}

func (s *Server) retryPendingTasks(ctx context.Context, now time.Time, retries map[string]schedulingRetry, opts SchedulingOptions) {
tasks, err := s.store.ListTasks(ctx, "", math.MaxInt64, 0)

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.

Fine for now. Worth a TODO to index Pending tasks, since this lists every task each interval.

@rakyll

rakyll commented Oct 14, 2026

Copy link
Copy Markdown
Member

We shouldn't merge this. We don't want AX to deal with a standalone queuing system. Substrate itself will eventually introduce some queueing itself and we can expand AX to use it.

@rakyll

rakyll commented Oct 14, 2026

Copy link
Copy Markdown
Member

Closing this PR, see my comment on #367 (comment).

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.

Task is marked Failed permanently when Substrate returns ResourceExhausted (no free workers) instead of staying Pending and retrying

3 participants