Skip to content

Commit 8e72e13

Browse files
waleedlatif1claude
andcommitted
fix(processing): enforce waitForCompletion inside markAsFailed to prevent completion races
Move waitForCompletion() into markAsFailed() so every call site is automatically safe against in-flight fire-and-forget completions. Remove the now-redundant external waitForCompletion() calls in route.ts. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent f093193 commit 8e72e13

File tree

2 files changed

+3
-6
lines changed

2 files changed

+3
-6
lines changed

apps/sim/app/api/workflows/[id]/execute/route.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,6 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
641641
logger.info(`[${requestId}] Non-SSE execution timed out`, {
642642
timeoutMs: timeoutController.timeoutMs,
643643
})
644-
await loggingSession.waitForCompletion()
645644
await loggingSession.markAsFailed(timeoutErrorMessage)
646645

647646
return NextResponse.json(
@@ -1027,7 +1026,6 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
10271026
logger.error(`[${requestId}] Missing snapshot seed for paused execution`, {
10281027
executionId,
10291028
})
1030-
await loggingSession.waitForCompletion()
10311029
await loggingSession.markAsFailed('Missing snapshot seed for paused execution')
10321030
} else {
10331031
try {
@@ -1043,7 +1041,6 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
10431041
executionId,
10441042
error: pauseError instanceof Error ? pauseError.message : String(pauseError),
10451043
})
1046-
await loggingSession.waitForCompletion()
10471044
await loggingSession.markAsFailed(
10481045
`Failed to persist pause state: ${pauseError instanceof Error ? pauseError.message : String(pauseError)}`
10491046
)
@@ -1060,7 +1057,6 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
10601057
timeoutMs: timeoutController.timeoutMs,
10611058
})
10621059

1063-
await loggingSession.waitForCompletion()
10641060
await loggingSession.markAsFailed(timeoutErrorMessage)
10651061

10661062
sendEvent({

apps/sim/lib/logs/execution/logging-session.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,8 @@ export class LoggingSession {
675675

676676
/**
677677
* Wait for any in-flight fire-and-forget completion to finish.
678-
* Used by callers (e.g. markAsFailed) that need to ensure completion
679-
* has settled before overwriting execution status.
678+
* Called internally by markAsFailed to ensure completion has settled
679+
* before overwriting execution status.
680680
*/
681681
async waitForCompletion(): Promise<void> {
682682
if (this.completionPromise) {
@@ -793,6 +793,7 @@ export class LoggingSession {
793793
}
794794

795795
async markAsFailed(errorMessage?: string): Promise<void> {
796+
await this.waitForCompletion()
796797
await LoggingSession.markExecutionAsFailed(this.executionId, errorMessage, this.requestId)
797798
}
798799

0 commit comments

Comments
 (0)