Skip to content

Commit 2d19333

Browse files
committed
Fixed reschedule race condition
1 parent 1293d43 commit 2d19333

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

internal-packages/run-engine/src/engine/locking.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -418,15 +418,18 @@ export class RunLocker {
418418
context.lock.expiration - Date.now() - this.automaticExtensionThreshold;
419419

420420
if (timeUntilExtension > 0) {
421-
context.timeout = setTimeout(() => {
422-
context.extension = this.#extendLock(
423-
context,
424-
duration,
425-
signal,
426-
controller,
427-
scheduleExtension
428-
);
429-
}, timeUntilExtension);
421+
// Check for cleanup immediately before scheduling to prevent race condition
422+
if (context.timeout !== null) {
423+
context.timeout = setTimeout(() => {
424+
context.extension = this.#extendLock(
425+
context,
426+
duration,
427+
signal,
428+
controller,
429+
scheduleExtension
430+
);
431+
}, timeUntilExtension);
432+
}
430433
}
431434
};
432435

@@ -452,10 +455,8 @@ export class RunLocker {
452455

453456
if (!error && newLock) {
454457
context.lock = newLock;
455-
// Only schedule next extension if we haven't been cleaned up
456-
if (context.timeout !== null) {
457-
scheduleNext();
458-
}
458+
// Schedule next extension (cleanup check is now inside scheduleNext)
459+
scheduleNext();
459460
} else {
460461
if (context.lock.expiration > Date.now()) {
461462
// If lock hasn't expired yet, schedule a retry instead of recursing

0 commit comments

Comments
 (0)