-
-
Notifications
You must be signed in to change notification settings - Fork 985
fix(cli): reject execute() immediately when child process is dead #2978
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
+139
−0
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "trigger.dev": patch | ||
| --- | ||
|
|
||
| Fix runner getting stuck indefinitely when `execute()` is called on a dead child process. |
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,121 @@ | ||
| import { TaskRunProcess, type TaskRunProcessOptions } from "./taskRunProcess.js"; | ||
| import { describe, it, expect, vi } from "vitest"; | ||
| import { UnexpectedExitError } from "@trigger.dev/core/v3/errors"; | ||
| import type { | ||
| TaskRunExecution, | ||
| TaskRunExecutionPayload, | ||
| WorkerManifest, | ||
| ServerBackgroundWorker, | ||
| MachinePresetResources, | ||
| } from "@trigger.dev/core/v3"; | ||
|
|
||
| function createTaskRunProcessOptions( | ||
| overrides: Partial<TaskRunProcessOptions> = {} | ||
| ): TaskRunProcessOptions { | ||
| return { | ||
| workerManifest: { | ||
| runtime: "node", | ||
| workerEntryPoint: "/dev/null", | ||
| configEntryPoint: "/dev/null", | ||
| otelImportHook: {}, | ||
| } as unknown as WorkerManifest, | ||
| serverWorker: {} as unknown as ServerBackgroundWorker, | ||
| env: {}, | ||
| machineResources: { cpu: 1, memory: 1 } as MachinePresetResources, | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| function createExecution(runId: string, attemptNumber: number): TaskRunExecution { | ||
| return { | ||
| run: { | ||
| id: runId, | ||
| payload: "{}", | ||
| payloadType: "application/json", | ||
| tags: [], | ||
| isTest: false, | ||
| createdAt: new Date(), | ||
| startedAt: new Date(), | ||
| maxAttempts: 3, | ||
| version: "1", | ||
| durationMs: 0, | ||
| costInCents: 0, | ||
| baseCostInCents: 0, | ||
| }, | ||
| attempt: { | ||
| number: attemptNumber, | ||
| startedAt: new Date(), | ||
| id: "deprecated", | ||
| backgroundWorkerId: "deprecated", | ||
| backgroundWorkerTaskId: "deprecated", | ||
| status: "deprecated" as any, | ||
| }, | ||
| task: { id: "test-task", filePath: "test.ts" }, | ||
| queue: { id: "queue-1", name: "test-queue" }, | ||
| environment: { id: "env-1", slug: "test", type: "DEVELOPMENT" }, | ||
| organization: { id: "org-1", slug: "test-org", name: "Test Org" }, | ||
| project: { id: "proj-1", ref: "proj_test", slug: "test", name: "Test" }, | ||
| machine: { name: "small-1x", cpu: 0.5, memory: 0.5, centsPerMs: 0 }, | ||
| } as unknown as TaskRunExecution; | ||
| } | ||
|
|
||
| describe("TaskRunProcess", () => { | ||
| describe("execute() on a dead child process", () => { | ||
| it("should reject when child process has already exited and IPC send is skipped", async () => { | ||
| const proc = new TaskRunProcess(createTaskRunProcessOptions()); | ||
|
|
||
| // Simulate a child process that has exited: _child exists but is not connected | ||
| const fakeChild = { | ||
| connected: false, | ||
| killed: false, | ||
| pid: 12345, | ||
| kill: vi.fn(), | ||
| on: vi.fn(), | ||
| stdout: { on: vi.fn() }, | ||
| stderr: { on: vi.fn() }, | ||
| }; | ||
|
|
||
| // Set internal state to mimic a process whose child has crashed | ||
| (proc as any)._child = fakeChild; | ||
| (proc as any)._childPid = 12345; | ||
| (proc as any)._isBeingKilled = false; | ||
|
|
||
| const execution = createExecution("run-1", 2); | ||
|
|
||
| // This should NOT hang forever - it should reject promptly. | ||
| // | ||
| // BUG: Currently execute() creates a promise, skips the IPC send because | ||
| // _child.connected is false, then awaits the promise which will never | ||
| // resolve because the child is dead and #handleExit already ran. | ||
| // | ||
| // The Promise.race with a timeout detects the hang. | ||
| const result = await Promise.race([ | ||
| proc | ||
| .execute( | ||
| { | ||
| payload: { execution, traceContext: {}, metrics: [] }, | ||
| messageId: "run_run-1", | ||
| env: {}, | ||
| }, | ||
| true | ||
| ) | ||
| .then( | ||
| (v) => ({ type: "resolved" as const, value: v }), | ||
| (e) => ({ type: "rejected" as const, error: e }) | ||
| ), | ||
| new Promise<{ type: "hung" }>((resolve) => | ||
| setTimeout(() => resolve({ type: "hung" as const }), 2000) | ||
| ), | ||
| ]); | ||
|
|
||
| // The test fails (proving the bug) if execute() hangs | ||
| expect(result.type).not.toBe("hung"); | ||
| expect(result.type).toBe("rejected"); | ||
|
|
||
| if (result.type === "rejected") { | ||
| expect(result.error).toBeInstanceOf(UnexpectedExitError); | ||
| expect(result.error.stderr).toContain("not connected"); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
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.