Skip to content

Re-run failed completions from their scheduled args - #280

Draft
ianmacartney wants to merge 2 commits into
mainfrom
ian/recover-lost-completion
Draft

ianmacartney wants to merge 2 commits into
mainfrom
ian/recover-lost-completion

Conversation

@ianmacartney

@ianmacartney ianmacartney commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Fixes #276 for mutation jobs and canceled jobs.

A mutation job whose scheduled complete failed stayed running and held its pool slot forever: recovery only checked the wrapper, which had succeeded.

  • The mutation wrapper records the scheduled complete on the work (completionId) in the same transaction as the work. Scheduling now happens outside the wrapper's try, so a scheduling error fails the wrapper and rolls back the work.
  • Recovery checks that completion instead of the wrapper. It waits while the completion is pending or in progress, and re-runs a failed one from its stored scheduler args; complete is guarded by the attempt number, so committed work is never re-run. After 5 re-runs it frees the slot and leaves the work doc in place.
  • complete clears completionId when it bumps attempts, and points jobs it forwards to a split batch at the new completion.
  • Completions for canceled jobs are tracked as completing entries in running, which don't count toward maxParallelism. complete now writes a pendingCompletion for canceled jobs so the loop can release them.
  • The loop reads up to 64 completions per iteration regardless of maxParallelism, and stops taking cancelations while 256 canceled jobs are waiting on their completions, so a mass cancel can't grow running without bound.
  • Reports, diagnostics and stats.running count only entries that hold a slot.
  • Adds lostCompletion.test.ts, which injects failures into complete.

Not covered: action jobs whose inline and fallback completions both fail, and mutations stuck pending in the scheduler (#240).

A mutation job whose scheduled `complete` failed held its pool slot
forever (#276): recovery only checked the wrapper, which had succeeded.

The wrapper now records the scheduled completion on the work in the
same transaction as the work. Recovery checks that completion instead
of the wrapper: it waits while it is pending, and re-runs a failed or
canceled one from its stored args, since `complete` is guarded by the
attempt number. Committed work is never re-run. After five re-runs it
frees the slot and leaves the work in place.

Completions for work canceled before it started are tracked the same
way, as `completing` entries in running that hold no slot.

Scheduling the completion now happens outside the wrapper's try, so a
scheduling error fails the wrapper and rolls back the work instead of
recording a failure for committed work.
@ianmacartney

Copy link
Copy Markdown
Member Author

@cubic-dev-ai review

@cubic-dev-ai

cubic-dev-ai Bot commented Nov 25, 2026

Copy link
Copy Markdown

@cubic-dev-ai review

@ianmacartney I have started the AI code review. It will take a few minutes to complete.

@pkg-pr-new

pkg-pr-new Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@convex-dev/workpool@280

commit: 14327ab

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 9 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread src/component/loop.ts Resolved
Comment thread src/component/stateMachine.test.ts Resolved
Each canceled-before-start job now waits in the running list as a
`completing` entry until the loop sees its completion. Completions were
read at most maxParallelism per iteration while cancelations arrive 64
at a time, so a mass cancel grew the list by thousands of entries.

Read up to 64 completions regardless of maxParallelism, and stop taking
cancelations while 256 canceled jobs are waiting on their completions.

Reports, diagnostics and the running query count only entries that hold
a slot.
@ianmacartney

Copy link
Copy Markdown
Member Author

@cubic-dev-ai review

@cubic-dev-ai

cubic-dev-ai Bot commented Nov 25, 2026

Copy link
Copy Markdown

@cubic-dev-ai review

@ianmacartney I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

2 issues found across 11 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/component/lostCompletion.test.ts">

<violation number="1" location="src/component/lostCompletion.test.ts:137">
P2: `drive` performs roughly 24,500 scheduler drains and database queries across this file, which makes the new suite unnecessarily slow and likely to hit the default test timeout. Advance in coarser intervals or target the recovery boundaries instead of polling every two seconds.</violation>
</file>

<file name="src/component/loop.ts">

<violation number="1" location="src/component/loop.ts:389">
P2: The `MAX_COMPLETING` bound is bypassed when it is already full: the start lane can still select a due `pendingStart` whose cancellation was not in the limited cancellation batch, and `handleStart` then adds another `completing` entry. Under a stuck completion and a mass cancel, the running list can exceed the advertised bound and continue growing; enforce the cap in every path that calls `scheduleCanceledCompletion` or exclude these canceled starts before selecting the start lane.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

* that no more than maxParallelism entries hold a slot.
*/
async function drive(t: T, ms: number) {
for (let elapsed = 0; elapsed < ms; elapsed += 2 * SECOND) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: drive performs roughly 24,500 scheduler drains and database queries across this file, which makes the new suite unnecessarily slow and likely to hit the default test timeout. Advance in coarser intervals or target the recovery boundaries instead of polling every two seconds.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/component/lostCompletion.test.ts, line 137:

<comment>`drive` performs roughly 24,500 scheduler drains and database queries across this file, which makes the new suite unnecessarily slow and likely to hit the default test timeout. Advance in coarser intervals or target the recovery boundaries instead of polling every two seconds.</comment>

<file context>
@@ -0,0 +1,346 @@
+ * that no more than maxParallelism entries hold a slot.
+ */
+async function drive(t: T, ms: number) {
+  for (let elapsed = 0; elapsed < ms; elapsed += 2 * SECOND) {
+    vi.advanceTimersByTime(2 * SECOND);
+    await t.finishInProgressScheduledFunctions();
</file context>

Comment thread src/component/loop.ts
.take(CANCELLATION_BATCH_SIZE);
.take(MAIN_BATCH_SIZE);
const completing = running.length - countRunning(running);
const cancelLimit = Math.min(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: The MAX_COMPLETING bound is bypassed when it is already full: the start lane can still select a due pendingStart whose cancellation was not in the limited cancellation batch, and handleStart then adds another completing entry. Under a stuck completion and a mass cancel, the running list can exceed the advertised bound and continue growing; enforce the cap in every path that calls scheduleCanceledCompletion or exclude these canceled starts before selecting the start lane.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/component/loop.ts, line 389:

<comment>The `MAX_COMPLETING` bound is bypassed when it is already full: the start lane can still select a due `pendingStart` whose cancellation was not in the limited cancellation batch, and `handleStart` then adds another `completing` entry. Under a stuck completion and a mass cancel, the running list can exceed the advertised bound and continue growing; enforce the cap in every path that calls `scheduleCanceledCompletion` or exclude these canceled starts before selecting the start lane.</comment>

<file context>
@@ -371,19 +380,28 @@ async function queryPending(
-    .take(CANCELLATION_BATCH_SIZE);
+    .take(MAIN_BATCH_SIZE);
+  const completing = running.length - countRunning(running);
+  const cancelLimit = Math.min(
+    CANCELLATION_BATCH_SIZE,
+    Math.max(0, MAX_COMPLETING - completing),
</file context>

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.

Mutation job stays running forever when its scheduled complete fails

1 participant