@@ -41,7 +41,12 @@ import { runStatusFromError, ServiceValidationError } from "../errors.js";
4141import { sendNotificationToWorker } from "../eventBus.js" ;
4242import { getMachinePreset , machinePresetFromName } from "../machinePresets.js" ;
4343import { retryOutcomeFromCompletion } from "../retrying.js" ;
44- import { isExecuting , isInitialState } from "../statuses.js" ;
44+ import {
45+ isExecuting ,
46+ isFinishedOrPendingFinished ,
47+ isInitialState ,
48+ isPendingExecuting ,
49+ } from "../statuses.js" ;
4550import { RunEngineOptions } from "../types.js" ;
4651import { BatchSystem } from "./batchSystem.js" ;
4752import { DelayedRunSystem } from "./delayedRunSystem.js" ;
@@ -345,8 +350,8 @@ export class RunAttemptSystem {
345350 span . setAttribute ( "taskRunId" , taskRun . id ) ;
346351 span . setAttribute ( "taskRunFriendlyId" , taskRun . friendlyId ) ;
347352
348- if ( taskRun . status === "CANCELED" ) {
349- throw new ServiceValidationError ( "Task run is cancelled " , 400 ) ;
353+ if ( isFinishedOrPendingFinished ( latestSnapshot . executionStatus ) ) {
354+ throw new ServiceValidationError ( "Task run is already finished " , 400 ) ;
350355 }
351356
352357 if ( ! taskRun . lockedById ) {
@@ -1321,7 +1326,10 @@ export class RunAttemptSystem {
13211326 } ) ;
13221327
13231328 //if executing, we need to message the worker to cancel the run and put it into `PENDING_CANCEL` status
1324- if ( isExecuting ( latestSnapshot . executionStatus ) ) {
1329+ if (
1330+ isExecuting ( latestSnapshot . executionStatus ) ||
1331+ isPendingExecuting ( latestSnapshot . executionStatus )
1332+ ) {
13251333 const newSnapshot = await this . executionSnapshotSystem . createExecutionSnapshot ( prisma , {
13261334 run,
13271335 snapshot : {
0 commit comments