-
-
Notifications
You must be signed in to change notification settings - Fork 742
v4: fix race condition when continuing run when blocked at the same time #2073
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
33 changes: 33 additions & 0 deletions
33
internal-packages/run-engine/src/engine/systems/raceSimulationSystem.ts
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,33 @@ | ||
import { promiseWithResolvers } from "@trigger.dev/core"; | ||
|
||
export class RaceSimulationSystem { | ||
private racepoints: Record<string, Promise<void> | undefined> = {}; | ||
|
||
constructor() {} | ||
|
||
async waitForRacepoint({ runId }: { runId: string }): Promise<void> { | ||
if (this.racepoints[runId]) { | ||
return this.racepoints[runId]; | ||
} | ||
|
||
return Promise.resolve(); | ||
} | ||
|
||
registerRacepointForRun({ runId, waitInterval }: { runId: string; waitInterval: number }) { | ||
if (this.racepoints[runId]) { | ||
return; | ||
} | ||
|
||
const { promise, resolve } = promiseWithResolvers<void>(); | ||
|
||
this.racepoints[runId] = promise; | ||
|
||
setTimeout(() => { | ||
resolve(); | ||
}, waitInterval); | ||
|
||
promise.then(() => { | ||
delete this.racepoints[runId]; | ||
}); | ||
} | ||
ericallam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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
111 changes: 111 additions & 0 deletions
111
internal-packages/run-engine/src/engine/tests/waitpointRace.test.ts
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,111 @@ | ||
import { containerTest } from "@internal/testcontainers"; | ||
import { trace } from "@internal/tracing"; | ||
import { expect } from "vitest"; | ||
import { RunEngine } from "../index.js"; | ||
import { setupAuthenticatedEnvironment, setupBackgroundWorker } from "./setup.js"; | ||
import { setTimeout } from "timers/promises"; | ||
|
||
vi.setConfig({ testTimeout: 60_000 }); | ||
|
||
describe("RunEngine Waitpoints – race condition", () => { | ||
containerTest( | ||
"join-row removed before run continues (failing race)", | ||
async ({ prisma, redisOptions }) => { | ||
const env = await setupAuthenticatedEnvironment(prisma, "PRODUCTION"); | ||
const engine = new RunEngine({ | ||
prisma, | ||
worker: { redis: redisOptions, workers: 1, tasksPerWorker: 10, pollIntervalMs: 100 }, | ||
queue: { redis: redisOptions }, | ||
runLock: { redis: redisOptions }, | ||
machines: { | ||
defaultMachine: "small-1x", | ||
machines: { | ||
"small-1x": { name: "small-1x" as const, cpu: 0.5, memory: 0.5, centsPerMs: 0.0001 }, | ||
}, | ||
baseCostInCents: 0.0001, | ||
}, | ||
tracer: trace.getTracer("test", "0.0.0"), | ||
}); | ||
|
||
try { | ||
const taskIdentifier = "test-task"; | ||
await setupBackgroundWorker(engine, env, taskIdentifier); | ||
|
||
const run = await engine.trigger( | ||
{ | ||
number: 1, | ||
friendlyId: "run_race", | ||
environment: env, | ||
taskIdentifier, | ||
payload: "{}", | ||
payloadType: "application/json", | ||
context: {}, | ||
traceContext: {}, | ||
traceId: "race-trace", | ||
spanId: "race-span", | ||
masterQueue: "main", | ||
queue: "task/test-task", | ||
isTest: false, | ||
tags: [], | ||
}, | ||
prisma | ||
); | ||
|
||
const dequeued = await engine.dequeueFromMasterQueue({ | ||
consumerId: "test", | ||
masterQueue: run.masterQueue, | ||
maxRunCount: 10, | ||
}); | ||
await engine.startRunAttempt({ | ||
runId: dequeued[0].run.id, | ||
snapshotId: dequeued[0].snapshot.id, | ||
}); | ||
|
||
// create manual waitpoint | ||
const { waitpoint } = await engine.createManualWaitpoint({ | ||
environmentId: env.id, | ||
projectId: env.projectId, | ||
}); | ||
|
||
// block the run | ||
await engine.blockRunWithWaitpoint({ | ||
runId: run.id, | ||
waitpoints: waitpoint.id, | ||
projectId: env.projectId, | ||
organizationId: env.organizationId, | ||
}); | ||
|
||
// Now we need to block the run again right after the continueRunIfUnblocked function | ||
// is called as a result of the above completeWaitpoint call | ||
const { waitpoint: waitpoint2 } = await engine.createManualWaitpoint({ | ||
environmentId: env.id, | ||
projectId: env.projectId, | ||
}); | ||
|
||
engine.registerRacepointForRun({ runId: run.id, waitInterval: 500 }); | ||
|
||
// complete the waitpoint (this will schedule a continueRunIfUnblocked job normally) | ||
await engine.completeWaitpoint({ id: waitpoint.id }); | ||
|
||
await engine.waitpointSystem.blockRunWithWaitpoint({ | ||
runId: run.id, | ||
waitpoints: waitpoint2.id, | ||
projectId: env.projectId, | ||
organizationId: env.organizationId, | ||
}); | ||
|
||
await setTimeout(1000); | ||
|
||
// The join row SHOULD still exist until the run progresses naturally. | ||
const joinRow = await prisma.taskRunWaitpoint.findFirst({ | ||
where: { taskRunId: run.id, waitpointId: waitpoint2.id }, | ||
}); | ||
|
||
// Intentionally expect it to still be there – current implementation erroneously deletes it so test fails. | ||
expect(joinRow).not.toBeNull(); | ||
} finally { | ||
await engine.quit(); | ||
} | ||
} | ||
); | ||
}); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.