@@ -24,7 +24,7 @@ import {
2424 filterSparseMissingOxlintTargets ,
2525 shouldPrepareExtensionPackageBoundaryArtifacts ,
2626} from "../../scripts/run-oxlint.mts" ;
27- import { waitForDead , waitForPidFile } from "../helpers/process-wait.js" ;
27+ import { waitForDead , waitForFile , waitForPidFile } from "../helpers/process-wait.js" ;
2828import { startProcessWatchdogFixture } from "../helpers/process-watchdog.js" ;
2929import { createScriptTestHarness } from "./test-helpers.js" ;
3030
@@ -35,6 +35,7 @@ const RUN_OXLINT_SHARDS_URL = pathToFileURL(
3535 join ( process . cwd ( ) , "scripts/run-oxlint-shards.mts" ) ,
3636) . href ;
3737type SignalScenario = "forward" | "group" | "ignore" ;
38+ type SuccessfulLeaderDescendantMode = "drain" | "persist" ;
3839
3940async function captureFailedTrailer (
4041 run : ( ) => Promise < void > | void ,
@@ -92,6 +93,78 @@ function createSignalRunner(mode: SignalScenario, target: string): void {
9293 ] ) ;
9394}
9495
96+ function createSuccessfulLeaderRunner ( mode : SuccessfulLeaderDescendantMode , target : string ) : void {
97+ const childScript = [
98+ "const { existsSync, renameSync, writeFileSync } = require('node:fs');" ,
99+ "const publish = (target, value) => { writeFileSync(target + '.tmp', value); renameSync(target + '.tmp', target); };" ,
100+ "publish(process.env.CHILD_PID_PATH, String(process.pid));" ,
101+ ...( mode === "drain"
102+ ? [
103+ "process.on('disconnect', () => publish(process.env.DRAINING_FILE, 'ready'));" ,
104+ "setInterval(() => { if (existsSync(process.env.RELEASE_FILE)) process.exit(0); }, 5);" ,
105+ ]
106+ : [ "process.on('disconnect', () => {});" , "setInterval(() => {}, 1000);" ] ) ,
107+ "publish(process.env.READY_FILE, 'ready');" ,
108+ "process.send?.('ready');" ,
109+ ] . join ( "\n" ) ;
110+ writeModule ( target , [
111+ "import { spawn } from 'node:child_process';" ,
112+ `const child = spawn(process.execPath, ['-e', ${ JSON . stringify ( childScript ) } ], { env: process.env, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] });` ,
113+ "child.once('message', () => process.exit(0));" ,
114+ "child.once('error', () => process.exit(2));" ,
115+ ] ) ;
116+ }
117+
118+ async function runSuccessfulLeaderDescendantScenario (
119+ mode : SuccessfulLeaderDescendantMode ,
120+ ) : Promise < number > {
121+ const tempDir = createTempDir ( `openclaw-oxlint-success-${ mode } -` ) ;
122+ const runner = join ( tempDir , "success-runner.mjs" ) ;
123+ const childPidPath = join ( tempDir , "child.pid" ) ;
124+ const readyFile = join ( tempDir , "ready" ) ;
125+ const drainingFile = join ( tempDir , "draining" ) ;
126+ const releaseFile = join ( tempDir , "release" ) ;
127+ let childPid = 0 ;
128+ createSuccessfulLeaderRunner ( mode , runner ) ;
129+
130+ const completion = runShard ( {
131+ env : {
132+ ...process . env ,
133+ CHILD_PID_PATH : childPidPath ,
134+ DRAINING_FILE : drainingFile ,
135+ READY_FILE : readyFile ,
136+ RELEASE_FILE : releaseFile ,
137+ OPENCLAW_OXLINT_SHARD_HEARTBEAT_MS : "0" ,
138+ OPENCLAW_OXLINT_SHARD_KILL_GRACE_MS : "1000" ,
139+ OPENCLAW_OXLINT_SHARD_TIMEOUT_MS : "0" ,
140+ } ,
141+ extraArgs : [ ] ,
142+ runner,
143+ shard : { name : `success-${ mode } -test` , args : [ ] } ,
144+ } ) ;
145+ try {
146+ childPid = await waitForPidFile ( childPidPath , 15_000 ) ;
147+ await waitForFile ( readyFile , 15_000 ) ;
148+ expect ( isProcessAlive ( childPid ) ) . toBe ( true ) ;
149+ if ( mode === "drain" ) {
150+ await waitForFile ( drainingFile , 15_000 ) ;
151+ writeFileSync ( releaseFile , "release" , "utf8" ) ;
152+ }
153+ const status = await completion ;
154+ await waitForDead ( childPid , 2_000 ) ;
155+ return status ;
156+ } finally {
157+ await completion . catch ( ( ) => undefined ) ;
158+ if ( ! childPid && existsSync ( childPidPath ) ) {
159+ childPid = Number ( readFileSync ( childPidPath , "utf8" ) ) ;
160+ }
161+ if ( childPid && isProcessAlive ( childPid ) ) {
162+ process . kill ( childPid , "SIGKILL" ) ;
163+ await waitForDead ( childPid , 2_000 ) ;
164+ }
165+ }
166+ }
167+
95168function runParentTerminationScenario ( mode : SignalScenario ) {
96169 const groupScenario = mode === "group" ;
97170 const tempDir = createTempDir (
@@ -487,6 +560,20 @@ describe("run-oxlint", () => {
487560 } ,
488561 ) ;
489562
563+ it . runIf ( process . platform !== "win32" ) (
564+ "preserves a successful shard status when its process group drains during grace" ,
565+ async ( ) => {
566+ await expect ( runSuccessfulLeaderDescendantScenario ( "drain" ) ) . resolves . toBe ( 0 ) ;
567+ } ,
568+ ) ;
569+
570+ it . runIf ( process . platform !== "win32" ) (
571+ "fails a successful shard when its process group requires SIGKILL" ,
572+ async ( ) => {
573+ await expect ( runSuccessfulLeaderDescendantScenario ( "persist" ) ) . resolves . toBe ( 1 ) ;
574+ } ,
575+ ) ;
576+
490577 it . runIf ( process . platform !== "win32" ) (
491578 "forwards parent termination to detached oxlint shard processes" ,
492579 ( ) => {
0 commit comments