-
Notifications
You must be signed in to change notification settings - Fork 237
feat: enforce max queue deliveries in handlers with graceful failure #1344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| "@workflow/errors": patch | ||
| "@workflow/core": patch | ||
| "@workflow/world-local": patch | ||
| "@workflow/builders": patch | ||
| "@workflow/sveltekit": patch | ||
| --- | ||
|
|
||
| Remove VQS maxDeliveries cap and enforce max delivery limit in workflow/step handlers with graceful failure |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Maximum number of queue delivery attempts before the handler gives up and | ||
| // gracefully fails the run/step. This must be bounded under the VQS message | ||
| // max visibility window (24 hours) so that our handler-side failure path | ||
| // reliably executes before VQS expires the message. | ||
| // | ||
| // VQS retry schedule (with retryAfterSeconds: 5): | ||
| // Attempts 1–32: linear backoff at 5s each → 32 × 5s = 160s (~2.7 min) | ||
| // Attempts 33+: exponential backoff: 60s × 2^(attempt-32), | ||
| // capped at 7,200s (2h), floored at retryAfterSeconds | ||
| // | ||
| // At 48 attempts the total elapsed time is approximately 20 hours, which is | ||
| // safely under the 24-hour message visibility limit. | ||
| export const MAX_QUEUE_DELIVERIES = 48; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -41,6 +41,7 @@ import { | |||||||||||||
| queueMessage, | ||||||||||||||
| withHealthCheck, | ||||||||||||||
| } from './helpers.js'; | ||||||||||||||
| import { MAX_QUEUE_DELIVERIES } from './constants.js'; | ||||||||||||||
| import { getWorld, getWorldHandlers } from './world.js'; | ||||||||||||||
|
|
||||||||||||||
| const DEFAULT_STEP_MAX_RETRIES = 3; | ||||||||||||||
|
|
@@ -67,6 +68,67 @@ const stepHandler = getWorldHandlers().createQueueHandler( | |||||||||||||
| requestedAt, | ||||||||||||||
| } = StepInvokePayloadSchema.parse(message_); | ||||||||||||||
| const { requestId } = metadata; | ||||||||||||||
|
|
||||||||||||||
| // --- Max delivery check --- | ||||||||||||||
| // Enforce max delivery limit before any infrastructure calls. | ||||||||||||||
| // This prevents runaway steps from consuming infinite queue deliveries. | ||||||||||||||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| // At this point, we want to do the minimal amount of work (no fetching | ||||||||||||||
| // of the step details, etc. We simply attempt to mark the step as failed | ||||||||||||||
| // and enqueue the workflow once, and if either of those fails, the message | ||||||||||||||
| // is still consumed but with adequate logging that an error occurred. | ||||||||||||||
| if (metadata.attempt > MAX_QUEUE_DELIVERIES) { | ||||||||||||||
| runtimeLogger.error( | ||||||||||||||
| `Step handler exceeded max deliveries (${metadata.attempt}/${MAX_QUEUE_DELIVERIES})`, | ||||||||||||||
| { | ||||||||||||||
| workflowRunId, | ||||||||||||||
| stepId, | ||||||||||||||
| stepName: metadata.queueName.slice('__wkf_step_'.length), | ||||||||||||||
| attempt: metadata.attempt, | ||||||||||||||
| } | ||||||||||||||
| ); | ||||||||||||||
| try { | ||||||||||||||
| const world = getWorld(); | ||||||||||||||
| await world.events.create( | ||||||||||||||
| workflowRunId, | ||||||||||||||
| { | ||||||||||||||
| eventType: 'step_failed', | ||||||||||||||
| specVersion: SPEC_VERSION_CURRENT, | ||||||||||||||
| correlationId: stepId, | ||||||||||||||
| eventData: { | ||||||||||||||
| error: `Step exceeded maximum queue deliveries (${metadata.attempt}/${MAX_QUEUE_DELIVERIES})`, | ||||||||||||||
| }, | ||||||||||||||
| }, | ||||||||||||||
| { requestId } | ||||||||||||||
| ); | ||||||||||||||
| // Re-queue the workflow to handle the failed step | ||||||||||||||
| await queueMessage(world, getWorkflowQueueName(workflowName), { | ||||||||||||||
| runId: workflowRunId, | ||||||||||||||
| traceCarrier: await serializeTraceCarrier(), | ||||||||||||||
| requestedAt: new Date(), | ||||||||||||||
| }); | ||||||||||||||
| } catch (err) { | ||||||||||||||
| if (EntityConflictError.is(err) || RunExpiredError.is(err)) { | ||||||||||||||
| return; | ||||||||||||||
| } | ||||||||||||||
| // Can't even mark the step as failed. Consume the message to stop | ||||||||||||||
| // further retries. The run will remain in its current state. | ||||||||||||||
| runtimeLogger.error( | ||||||||||||||
| `Failed to mark step as failed after ${metadata.attempt} delivery attempts. ` + | ||||||||||||||
| `A persistent error is preventing the step from being terminated. ` + | ||||||||||||||
| `The run will remain in its current state until manually resolved. ` + | ||||||||||||||
| `This is most likely due to a persistent outage of the workflow backend ` + | ||||||||||||||
| `or a bug in the workflow runtime and should be reported to the Workflow team.`, | ||||||||||||||
| { | ||||||||||||||
| workflowRunId, | ||||||||||||||
| stepId, | ||||||||||||||
| attempt: metadata.attempt, | ||||||||||||||
| error: err instanceof Error ? err.message : String(err), | ||||||||||||||
| } | ||||||||||||||
| ); | ||||||||||||||
| } | ||||||||||||||
| return; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| const spanLinks = await linkToCurrentContext(); | ||||||||||||||
| // Execute step within the propagated trace context | ||||||||||||||
| return await withTraceContext(traceContext, async () => { | ||||||||||||||
|
|
||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,6 +90,14 @@ export function createQueue(config: Partial<Config>): LocalQueue { | |
| const { pathname, prefix } = getQueueRoute(queueName); | ||
| const messageId = MessageId.parse(`msg_${generateId()}`); | ||
|
|
||
| // Extract identifiers from the message for structured logging. | ||
| // Workflow messages have `runId`, step messages have `workflowRunId` + `stepId`. | ||
| const msg = message as Record<string, unknown>; | ||
| const runId = (msg.runId ?? msg.workflowRunId ?? undefined) as | ||
| | string | ||
| | undefined; | ||
| const stepId = (msg.stepId ?? undefined) as string | undefined; | ||
|
|
||
| if (opts?.idempotencyKey) { | ||
| const key = opts.idempotencyKey; | ||
| inflightMessages.set(key, messageId); | ||
|
|
@@ -106,12 +114,12 @@ export function createQueue(config: Partial<Config>): LocalQueue { | |
| ); | ||
| await semaphore.acquire(); | ||
| } | ||
| // Safety limit to prevent infinite loops in the local queue. | ||
| // The actual max delivery enforcement happens in the workflow/step handlers | ||
| // (at MAX_QUEUE_DELIVERIES = 48), so this just needs to be comfortably higher. | ||
| const MAX_LOCAL_SAFETY_LIMIT = 256; | ||
| try { | ||
| const maxAttempts = 3; | ||
| let defaultRetriesLeft = maxAttempts; | ||
| for (let attempt = 0; defaultRetriesLeft > 0; attempt++) { | ||
| defaultRetriesLeft--; | ||
|
|
||
| for (let attempt = 0; attempt < MAX_LOCAL_SAFETY_LIMIT; attempt++) { | ||
|
Comment on lines
+117
to
+122
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah there's no reason this has to be 1000. just needs to be higher than the max queue attempts. let's go with 256 |
||
| const headers: Record<string, string> = { | ||
| ...opts?.headers, | ||
| 'content-type': 'application/json', | ||
|
|
@@ -163,23 +171,39 @@ export function createQueue(config: Partial<Config>): LocalQueue { | |
| ); | ||
| await setTimeout(timeoutMs); | ||
| } | ||
| defaultRetriesLeft++; | ||
| continue; | ||
| } | ||
| } catch {} | ||
| return; | ||
| } | ||
|
|
||
| console.error( | ||
| `[world-local] Queue message failed (attempt ${attempt + 1}/${maxAttempts}, status ${response.status}): ${text}`, | ||
| { queueName, messageId } | ||
| `[world-local] Queue message failed (attempt ${attempt + 1}, HTTP ${response.status})`, | ||
| { | ||
| queueName, | ||
| messageId, | ||
| ...(runId && { runId }), | ||
| ...(stepId && { stepId }), | ||
| handlerError: text, | ||
| } | ||
| ); | ||
|
|
||
| // 5s linear backoff to approximate VQS retry timing in local dev. | ||
| // VQS uses 5s linear for attempts 1–32, then exponential, but for | ||
| // local dev linear 5s is sufficient — the handler enforces the real | ||
| // cap at MAX_QUEUE_DELIVERIES (48) which keeps total time under ~4min. | ||
| await setTimeout(5000); | ||
| } | ||
|
|
||
| console.error(`[world-local] Queue message exhausted all retries`, { | ||
| queueName, | ||
| messageId, | ||
| }); | ||
| console.error( | ||
| `[world-local] Queue message exhausted safety limit (${MAX_LOCAL_SAFETY_LIMIT} attempts)`, | ||
| { | ||
| queueName, | ||
| messageId, | ||
| ...(runId && { runId }), | ||
| ...(stepId && { stepId }), | ||
| } | ||
| ); | ||
| } finally { | ||
| semaphore.release(); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.