-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix: reap ghost jobs whose worker died without recording a result #425
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
Open
Greenbauer
wants to merge
4
commits into
openai:main
Choose a base branch
from
Greenbauer:fix/reap-dead-tracked-jobs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7580728
fix: reap ghost jobs whose worker died without recording a result
Greenbauer 954af53
fix: refresh updatedAt when reaping a dead job so it sorts newest-first
Greenbauer 0cfbff8
fix: do not catch teardown signals in the worker crash guard
Greenbauer f9dff6a
fix: reap dead jobs before selecting task resume candidates
Greenbauer 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
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
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
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,150 @@ | ||
| import fs from "node:fs"; | ||
| import path from "node:path"; | ||
| import test from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { spawn } from "node:child_process"; | ||
| import { fileURLToPath, pathToFileURL } from "node:url"; | ||
|
|
||
| import { makeTempDir, run } from "./helpers.mjs"; | ||
| import { reapDeadJobs } from "../plugins/codex/scripts/lib/tracked-jobs.mjs"; | ||
| import { listJobs, readJobFile, resolveJobFile, upsertJob, writeJobFile } from "../plugins/codex/scripts/lib/state.mjs"; | ||
|
|
||
| const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); | ||
| const TRACKED_JOBS_URL = pathToFileURL(path.join(ROOT, "plugins", "codex", "scripts", "lib", "tracked-jobs.mjs")).href; | ||
|
|
||
| function seedJob(workspace, job) { | ||
| writeJobFile(workspace, job.id, job); | ||
| upsertJob(workspace, job); | ||
| } | ||
|
|
||
| function spawnDeadPid() { | ||
| const result = run(process.execPath, ["-e", ""]); | ||
| assert.equal(result.status, 0); | ||
| return result.pid; | ||
| } | ||
|
|
||
| test("reapDeadJobs marks a running job with a dead pid as failed", () => { | ||
| const workspace = makeTempDir(); | ||
| seedJob(workspace, { id: "job-dead", status: "running", phase: "delegating", pid: spawnDeadPid(), logFile: null }); | ||
|
|
||
| const reaped = reapDeadJobs(workspace, listJobs(workspace)); | ||
|
|
||
| assert.equal(reaped.length, 1); | ||
| assert.equal(reaped[0].status, "failed"); | ||
| assert.equal(reaped[0].pid, null); | ||
| assert.match(reaped[0].errorMessage, /died without recording a result/); | ||
|
|
||
| const stored = readJobFile(resolveJobFile(workspace, "job-dead")); | ||
| assert.equal(stored.status, "failed"); | ||
| assert.equal(stored.pid, null); | ||
| assert.equal(listJobs(workspace).find((job) => job.id === "job-dead").status, "failed"); | ||
| }); | ||
|
|
||
| test("reapDeadJobs refreshes updatedAt so the reaped job sorts newest-first", () => { | ||
| const workspace = makeTempDir(); | ||
| const stale = "2000-01-01T00:00:00.000Z"; | ||
| seedJob(workspace, { id: "job-stale", status: "running", phase: "delegating", pid: spawnDeadPid(), updatedAt: stale, logFile: null }); | ||
|
|
||
| const reaped = reapDeadJobs(workspace, listJobs(workspace)); | ||
|
|
||
| assert.notEqual(reaped[0].updatedAt, stale); | ||
| assert.equal(reaped[0].updatedAt, reaped[0].completedAt); | ||
| assert.equal(readJobFile(resolveJobFile(workspace, "job-stale")).updatedAt, reaped[0].updatedAt); | ||
| }); | ||
|
|
||
| test("reapDeadJobs leaves a running job with a live pid untouched", () => { | ||
| const workspace = makeTempDir(); | ||
| seedJob(workspace, { id: "job-live", status: "running", phase: "delegating", pid: process.pid, logFile: null }); | ||
|
|
||
| const reaped = reapDeadJobs(workspace, listJobs(workspace)); | ||
|
|
||
| assert.equal(reaped[0].status, "running"); | ||
| assert.equal(readJobFile(resolveJobFile(workspace, "job-live")).status, "running"); | ||
| }); | ||
|
|
||
| test("reapDeadJobs leaves jobs without a recorded pid untouched", () => { | ||
| const workspace = makeTempDir(); | ||
| seedJob(workspace, { id: "job-no-pid", status: "queued", phase: "queued", pid: null, logFile: null }); | ||
|
|
||
| const reaped = reapDeadJobs(workspace, listJobs(workspace)); | ||
|
|
||
| assert.equal(reaped[0].status, "queued"); | ||
| }); | ||
|
|
||
| test("reapDeadJobs keeps the stored result when the job finished between read and probe", () => { | ||
| const workspace = makeTempDir(); | ||
| const deadPid = spawnDeadPid(); | ||
| writeJobFile(workspace, "job-raced", { id: "job-raced", status: "completed", phase: "completed", result: "done" }); | ||
| upsertJob(workspace, { id: "job-raced", status: "running", phase: "delegating", pid: deadPid, logFile: null }); | ||
|
|
||
| const reaped = reapDeadJobs(workspace, [{ id: "job-raced", status: "running", pid: deadPid, logFile: null }]); | ||
|
|
||
| assert.equal(reaped[0].status, "completed"); | ||
| assert.equal(reaped[0].result, "done"); | ||
| assert.equal(readJobFile(resolveJobFile(workspace, "job-raced")).status, "completed"); | ||
| }); | ||
|
|
||
| test("registerWorkerCrashGuard marks the job failed when the worker dies on an unhandled rejection", () => { | ||
| const workspace = makeTempDir(); | ||
| seedJob(workspace, { id: "job-crash", status: "running", phase: "delegating", pid: null, logFile: null }); | ||
|
|
||
| const workerFile = path.join(makeTempDir(), "crashing-worker.mjs"); | ||
| fs.writeFileSync( | ||
| workerFile, | ||
| [ | ||
| `import { registerWorkerCrashGuard } from ${JSON.stringify(TRACKED_JOBS_URL)};`, | ||
| "registerWorkerCrashGuard(process.argv[2], process.argv[3], null);", | ||
| 'Promise.reject(new Error("boom"));', | ||
| "setTimeout(() => {}, 5000);", | ||
| "" | ||
| ].join("\n"), | ||
| "utf8" | ||
| ); | ||
|
|
||
| const result = run(process.execPath, [workerFile, workspace, "job-crash"]); | ||
|
|
||
| assert.equal(result.status, 1); | ||
| const stored = readJobFile(resolveJobFile(workspace, "job-crash")); | ||
| assert.equal(stored.status, "failed"); | ||
| assert.match(stored.errorMessage, /unhandledRejection/); | ||
| assert.match(stored.errorMessage, /boom/); | ||
| }); | ||
|
|
||
| test("registerWorkerCrashGuard does not rewrite a cancelled job when the worker is SIGTERMed", async () => { | ||
| const workspace = makeTempDir(); | ||
| // Simulate handleCancel having already written the terminal state before the | ||
| // worker processes the teardown SIGTERM it delivered. | ||
| seedJob(workspace, { id: "job-cancelled", status: "cancelled", phase: "cancelled", pid: null, errorMessage: "Cancelled by user." }); | ||
|
|
||
| const workerFile = path.join(makeTempDir(), "long-worker.mjs"); | ||
| fs.writeFileSync( | ||
| workerFile, | ||
| [ | ||
| `import { registerWorkerCrashGuard } from ${JSON.stringify(TRACKED_JOBS_URL)};`, | ||
| "registerWorkerCrashGuard(process.argv[2], process.argv[3], null);", | ||
| 'process.stdout.write("ready\\n");', | ||
| "setInterval(() => {}, 1000);", | ||
| "" | ||
| ].join("\n"), | ||
| "utf8" | ||
| ); | ||
|
|
||
| const child = spawn(process.execPath, [workerFile, workspace, "job-cancelled"], { stdio: ["ignore", "pipe", "ignore"] }); | ||
| await new Promise((resolve, reject) => { | ||
| child.stdout.on("data", (chunk) => { | ||
| if (chunk.toString().includes("ready")) { | ||
| resolve(); | ||
| } | ||
| }); | ||
| child.on("error", reject); | ||
| }); | ||
|
|
||
| const exited = new Promise((resolve) => child.on("exit", (code, signal) => resolve({ code, signal }))); | ||
| child.kill("SIGTERM"); | ||
| const { signal } = await exited; | ||
|
|
||
| assert.equal(signal, "SIGTERM"); | ||
| const stored = readJobFile(resolveJobFile(workspace, "job-cancelled")); | ||
| assert.equal(stored.status, "cancelled"); | ||
| assert.equal(stored.errorMessage, "Cancelled by user."); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Linux/container sessions where the detached worker is reparented to a PID 1 that does not reap children, a crashed worker can remain as a zombie: it has exited, but its PID still exists, so
process.kill(pid, 0)succeeds here andreapDeadJobsleaves the jobrunningforever. That preserves the exact stale-status problem this reaper is meant to fix for SIGKILL/OOM/native-crash cases; check for zombie state as dead on platforms that expose it instead of relying only on kill(0).Useful? React with 👍 / 👎.