Skip to content

Prevent large outputs from overwriting each other #1971

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 3 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/spotty-pants-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"trigger.dev": patch
"@trigger.dev/core": patch
---

Prevent large outputs from overwriting each other
2 changes: 1 addition & 1 deletion packages/cli-v3/src/entryPoints/dev-run-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ export class DevRunController {
}).initialize();

logger.debug("executing task run process", {
attemptId: execution.attempt.id,
attemptNumber: execution.attempt.number,
runId: execution.run.id,
});

Expand Down
3 changes: 2 additions & 1 deletion packages/cli-v3/src/entryPoints/dev-run-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
AnyOnStartHookFunction,
AnyOnSuccessHookFunction,
apiClientManager,
attemptKey,
clock,
ExecutorToWorkerMessageCatalog,
type HandleErrorFunction,
Expand Down Expand Up @@ -546,7 +547,7 @@ const heartbeatInterval = parseInt(heartbeatIntervalMs ?? "30000", 10);
for await (const _ of setInterval(heartbeatInterval)) {
if (_isRunning && _execution) {
try {
await zodIpc.send("TASK_HEARTBEAT", { id: _execution.attempt.id });
await zodIpc.send("TASK_HEARTBEAT", { id: attemptKey(_execution) });
} catch (err) {
logError("Failed to send HEARTBEAT message", err);
}
Expand Down
15 changes: 10 additions & 5 deletions packages/cli-v3/src/executions/taskRunProcess.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
attemptKey,
CompletedWaitpoint,
ExecutorToWorkerMessageCatalog,
MachinePresetResources,
Expand Down Expand Up @@ -164,15 +165,17 @@ export class TaskRunProcess {
TASK_RUN_COMPLETED: async (message) => {
const { result, execution } = message;

const promiseStatus = this._attemptStatuses.get(execution.attempt.id);
const key = attemptKey(execution);

const promiseStatus = this._attemptStatuses.get(key);

if (promiseStatus !== "PENDING") {
return;
}

this._attemptStatuses.set(execution.attempt.id, "RESOLVED");
this._attemptStatuses.set(key, "RESOLVED");

const attemptPromise = this._attemptPromises.get(execution.attempt.id);
const attemptPromise = this._attemptPromises.get(key);

if (!attemptPromise) {
return;
Expand Down Expand Up @@ -229,10 +232,12 @@ export class TaskRunProcess {
rejecter = reject;
});

this._attemptStatuses.set(params.payload.execution.attempt.id, "PENDING");
const key = attemptKey(params.payload.execution);

this._attemptStatuses.set(key, "PENDING");

// @ts-expect-error - We know that the resolver and rejecter are defined
this._attemptPromises.set(params.payload.execution.attempt.id, { resolver, rejecter });
this._attemptPromises.set(key, { resolver, rejecter });

const { execution, traceContext, metrics } = params.payload;

Expand Down
16 changes: 15 additions & 1 deletion packages/core/src/v3/idempotencyKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function injectScope(scope: "run" | "attempt" | "global"): string[] {
}
case "attempt": {
if (taskContext?.ctx) {
return [taskContext.ctx.attempt.id];
return [taskContext.ctx.run.id, taskContext.ctx.attempt.number.toString()];
}
break;
}
Expand All @@ -125,3 +125,17 @@ async function generateIdempotencyKey(keyMaterial: string[]) {
.map((byte) => byte.toString(16).padStart(2, "0"))
.join("");
}

type AttemptKeyMaterial = {
run: {
id: string;
};
attempt: {
number: number;
};
};

/** Creates a unique key for each attempt. */
export function attemptKey(ctx: AttemptKeyMaterial): string {
return `${ctx.run.id}-${ctx.attempt.number}`;
}
3 changes: 2 additions & 1 deletion packages/core/src/v3/workers/taskExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from "../errors.js";
import {
accessoryAttributes,
attemptKey,
flattenAttributes,
lifecycleHooks,
runMetadata,
Expand Down Expand Up @@ -235,7 +236,7 @@ export class TaskExecutor {
const [exportError, finalOutput] = await tryCatch(
conditionallyExportPacket(
stringifiedOutput,
`${execution.attempt.id}/output`,
`${attemptKey(ctx)}/output`,
this._tracer
)
);
Expand Down