@@ -116,13 +116,59 @@ async function readPid(filePath: string, timeoutMs: number): Promise<number> {
116116 throw new Error ( `timeout waiting for a positive pid in ${ filePath } ` ) ;
117117}
118118
119- async function joinCommandObservations ( observations : Promise < unknown > [ ] ) : Promise < void > {
120- // Join PID discovery and the timeout assertion before cleanup, including on failure.
121- const results = await Promise . allSettled ( observations ) ;
122- for ( const result of results ) {
123- if ( result . status === "rejected" ) {
124- throw result . reason ;
119+ async function expectCommandTimeoutAfterReady (
120+ start : ( ) => Promise < string > ,
121+ ready : ( ) => Promise < void > ,
122+ timeoutMs = 500 ,
123+ ) : Promise < void > {
124+ const realSetTimeout = globalThis . setTimeout ;
125+ const deadlines : Array < ( ) => void > = [ ] ;
126+ const expire = ( ) => {
127+ for ( const deadline of deadlines . splice ( 0 ) ) {
128+ deadline ( ) ;
125129 }
130+ } ;
131+ // Hold only the command deadline until the real child has installed its handlers.
132+ // Restore scheduling before readiness polling and all real termination/grace work.
133+ const timerSpy = vi
134+ . spyOn ( globalThis , "setTimeout" )
135+ . mockImplementation ( ( callback , milliseconds , ...args ) => {
136+ if ( milliseconds !== timeoutMs ) {
137+ return realSetTimeout ( callback , milliseconds , ...args ) ;
138+ }
139+ const timer = realSetTimeout ( ( ) => { } , milliseconds ) ;
140+ deadlines . push ( ( ) => {
141+ clearTimeout ( timer ) ;
142+ callback ( ...args ) ;
143+ } ) ;
144+ return timer ;
145+ } ) ;
146+ try {
147+ let runPromise : Promise < string > ;
148+ try {
149+ runPromise = start ( ) ;
150+ } finally {
151+ timerSpy . mockRestore ( ) ;
152+ }
153+ // Observe rejection before the first await, and join both outcomes before cleanup.
154+ const results = await Promise . allSettled ( [
155+ expect ( runPromise ) . rejects . toThrow ( `timed out after ${ timeoutMs } ms` ) ,
156+ ( async ( ) => {
157+ try {
158+ expect ( deadlines ) . toHaveLength ( 1 ) ;
159+ await ready ( ) ;
160+ } finally {
161+ expire ( ) ;
162+ }
163+ } ) ( ) ,
164+ ] ) ;
165+ for ( const result of results ) {
166+ if ( result . status === "rejected" ) {
167+ throw result . reason ;
168+ }
169+ }
170+ } finally {
171+ expire ( ) ;
126172 }
127173}
128174
@@ -302,17 +348,17 @@ describe("package-openclaw-for-docker", () => {
302348
303349 let childPid = 0 ;
304350 try {
305- const runPromise = runCommandForTest ( "pnpm" , [ "probe" ] , tempDir , {
306- env ,
307- killAfterMs : 25 ,
308- timeoutMs : 500 ,
309- } ) ;
310- await joinCommandObservations ( [
311- expect ( runPromise ) . rejects . toThrow ( / t i m e d o u t a f t e r 5 0 0 m s / u ) ,
312- readPid ( childPidPath , 2_000 ) . then ( ( pid ) => {
313- childPid = pid ;
314- } ) ,
315- ] ) ;
351+ await expectCommandTimeoutAfterReady (
352+ ( ) =>
353+ runCommandForTest ( "pnpm" , [ "probe" ] , tempDir , {
354+ env ,
355+ killAfterMs : 25 ,
356+ timeoutMs : 500 ,
357+ } ) ,
358+ async ( ) => {
359+ childPid = await readPid ( childPidPath , 2_000 ) ;
360+ } ,
361+ ) ;
316362 await waitForDead ( childPid , 2_000 ) ;
317363 } finally {
318364 if ( childPid && isProcessAlive ( childPid ) ) {
@@ -1611,29 +1657,30 @@ describe("package-openclaw-for-docker", () => {
16111657 const childPidPath = path . join ( tempDir , "child.pid" ) ;
16121658 let childPid = 0 ;
16131659 try {
1614- const childScript = [ "process.on('SIGTERM', () => {});" , "setInterval(() => {}, 1000);" ] . join (
1615- "" ,
1616- ) ;
1660+ const childScript = [
1661+ "const fs = require('node:fs');" ,
1662+ "process.on('SIGTERM', () => {});" ,
1663+ "fs.writeFileSync(process.env.OPENCLAW_TEST_CHILD_PID, String(process.pid));" ,
1664+ "setInterval(() => {}, 1000);" ,
1665+ ] . join ( "" ) ;
16171666 const parentScript = [
16181667 "const { spawn } = require('node:child_process');" ,
1619- "const fs = require('node:fs');" ,
1620- `const child = spawn(process.execPath, ['-e', ${ JSON . stringify ( childScript ) } ], { stdio: 'ignore' });` ,
1621- "fs.writeFileSync(process.env.OPENCLAW_TEST_CHILD_PID, String(child.pid));" ,
16221668 "process.on('SIGTERM', () => {});" ,
1669+ `spawn(process.execPath, ['-e', ${ JSON . stringify ( childScript ) } ], { stdio: 'ignore' });` ,
16231670 "setInterval(() => {}, 1000);" ,
16241671 ] . join ( "" ) ;
16251672
1626- const runPromise = runCommandForTest ( process . execPath , [ "-e" , parentScript ] , process . cwd ( ) , {
1627- env : { ... process . env , OPENCLAW_TEST_CHILD_PID : childPidPath } ,
1628- killAfterMs : 25 ,
1629- timeoutMs : 500 ,
1630- } ) ;
1631- await joinCommandObservations ( [
1632- expect ( runPromise ) . rejects . toThrow ( / t i m e d o u t a f t e r 5 0 0 m s / u ) ,
1633- readPid ( childPidPath , 2000 ) . then ( ( pid ) => {
1634- childPid = pid ;
1635- } ) ,
1636- ] ) ;
1673+ await expectCommandTimeoutAfterReady (
1674+ ( ) =>
1675+ runCommandForTest ( process . execPath , [ "-e" , parentScript ] , process . cwd ( ) , {
1676+ env : { ... process . env , OPENCLAW_TEST_CHILD_PID : childPidPath } ,
1677+ killAfterMs : 25 ,
1678+ timeoutMs : 500 ,
1679+ } ) ,
1680+ async ( ) => {
1681+ childPid = await readPid ( childPidPath , 2000 ) ;
1682+ } ,
1683+ ) ;
16371684 await waitForDead ( childPid , 2000 ) ;
16381685 } finally {
16391686 if ( childPid && isProcessAlive ( childPid ) ) {
@@ -1655,23 +1702,23 @@ describe("package-openclaw-for-docker", () => {
16551702 try {
16561703 const script = [
16571704 "const fs = require('node:fs');" ,
1658- `fs.writeFileSync(${ JSON . stringify ( childPidPath ) } , String(process.pid));` ,
16591705 "process.on('SIGTERM', () => {" ,
16601706 ` setTimeout(() => { fs.writeFileSync(${ JSON . stringify ( donePath ) } , 'done'); process.exit(0); }, 75);` ,
16611707 "});" ,
1708+ `fs.writeFileSync(${ JSON . stringify ( childPidPath ) } , String(process.pid));` ,
16621709 "setInterval(() => {}, 1000);" ,
16631710 ] . join ( "\n" ) ;
16641711
1665- const runPromise = runCommandForTest ( process . execPath , [ "-e" , script ] , process . cwd ( ) , {
1666- killAfterMs : MAX_TIMER_TIMEOUT_MS + 1 ,
1667- timeoutMs : 500 ,
1668- } ) ;
1669- await joinCommandObservations ( [
1670- expect ( runPromise ) . rejects . toThrow ( / t i m e d o u t a f t e r 5 0 0 m s / u ) ,
1671- readPid ( childPidPath , 2000 ) . then ( ( pid ) => {
1672- childPid = pid ;
1673- } ) ,
1674- ] ) ;
1712+ await expectCommandTimeoutAfterReady (
1713+ ( ) =>
1714+ runCommandForTest ( process . execPath , [ "-e" , script ] , process . cwd ( ) , {
1715+ killAfterMs : MAX_TIMER_TIMEOUT_MS + 1 ,
1716+ timeoutMs : 500 ,
1717+ } ) ,
1718+ async ( ) => {
1719+ childPid = await readPid ( childPidPath , 2000 ) ;
1720+ } ,
1721+ ) ;
16751722 expect ( fs . readFileSync ( donePath , "utf8" ) ) . toBe ( "done" ) ;
16761723 } finally {
16771724 if ( childPid && isProcessAlive ( childPid ) ) {
@@ -1688,28 +1735,31 @@ describe("package-openclaw-for-docker", () => {
16881735
16891736 const tempDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-package-descendant-" ) ) ;
16901737 const childPidPath = path . join ( tempDir , "child.pid" ) ;
1691- let childPid ;
1738+ let childPid = 0 ;
16921739 try {
1693- const childScript = [ "process.on('SIGTERM', () => {});" , "setInterval(() => {}, 1000);" ] . join (
1694- "" ,
1695- ) ;
1740+ const childScript = [
1741+ "const fs = require('node:fs');" ,
1742+ "process.on('SIGTERM', () => {});" ,
1743+ "fs.writeFileSync(process.env.OPENCLAW_TEST_CHILD_PID, String(process.pid));" ,
1744+ "setInterval(() => {}, 1000);" ,
1745+ ] . join ( "" ) ;
16961746 const parentScript = [
16971747 "const { spawn } = require('node:child_process');" ,
1698- "const fs = require('node:fs');" ,
1699- `const child = spawn(process.execPath, ['-e', ${ JSON . stringify ( childScript ) } ], { stdio: 'ignore' });` ,
1700- "fs.writeFileSync(process.env.OPENCLAW_TEST_CHILD_PID, String(child.pid));" ,
1748+ `spawn(process.execPath, ['-e', ${ JSON . stringify ( childScript ) } ], { stdio: 'ignore' });` ,
17011749 "setInterval(() => {}, 1000);" ,
17021750 ] . join ( "" ) ;
17031751
1704- await expect (
1705- runCommandForTest ( process . execPath , [ "-e" , parentScript ] , process . cwd ( ) , {
1706- env : { ...process . env , OPENCLAW_TEST_CHILD_PID : childPidPath } ,
1707- killAfterMs : 25 ,
1708- timeoutMs : 500 ,
1709- } ) ,
1710- ) . rejects . toThrow ( / t i m e d o u t a f t e r 5 0 0 m s / u) ;
1711-
1712- childPid = await readPid ( childPidPath , 2000 ) ;
1752+ await expectCommandTimeoutAfterReady (
1753+ ( ) =>
1754+ runCommandForTest ( process . execPath , [ "-e" , parentScript ] , process . cwd ( ) , {
1755+ env : { ...process . env , OPENCLAW_TEST_CHILD_PID : childPidPath } ,
1756+ killAfterMs : 25 ,
1757+ timeoutMs : 500 ,
1758+ } ) ,
1759+ async ( ) => {
1760+ childPid = await readPid ( childPidPath , 2000 ) ;
1761+ } ,
1762+ ) ;
17131763 await waitForDead ( childPid , 2000 ) ;
17141764 } finally {
17151765 if ( childPid && isProcessAlive ( childPid ) ) {
@@ -1724,19 +1774,29 @@ describe("package-openclaw-for-docker", () => {
17241774 return ;
17251775 }
17261776
1777+ const tempDir = tempDirs . make ( "openclaw-package-grace-exit-" ) ;
1778+ const childPidPath = path . join ( tempDir , "child.pid" ) ;
1779+ let childPid = 0 ;
17271780 const killSpy = vi . spyOn ( process , "kill" ) ;
17281781 try {
17291782 const script = [
1783+ "const fs = require('node:fs');" ,
17301784 "process.on('SIGTERM', () => process.exit(0));" ,
1785+ `fs.writeFileSync(${ JSON . stringify ( childPidPath ) } , String(process.pid));` ,
17311786 "setInterval(() => {}, 1000);" ,
17321787 ] . join ( "" ) ;
17331788
1734- await expect (
1735- runCommandForTest ( process . execPath , [ "-e" , script ] , process . cwd ( ) , {
1736- killAfterMs : 100 ,
1737- timeoutMs : 25 ,
1738- } ) ,
1739- ) . rejects . toThrow ( / t i m e d o u t a f t e r 2 5 m s / u) ;
1789+ await expectCommandTimeoutAfterReady (
1790+ ( ) =>
1791+ runCommandForTest ( process . execPath , [ "-e" , script ] , process . cwd ( ) , {
1792+ killAfterMs : 100 ,
1793+ timeoutMs : 25 ,
1794+ } ) ,
1795+ async ( ) => {
1796+ childPid = await readPid ( childPidPath , 2000 ) ;
1797+ } ,
1798+ 25 ,
1799+ ) ;
17401800
17411801 const sigkillCallsAfterExit = killSpy . mock . calls . filter (
17421802 ( [ , signal ] ) => signal === "SIGKILL" ,
@@ -1747,6 +1807,9 @@ describe("package-openclaw-for-docker", () => {
17471807 ) ;
17481808 } finally {
17491809 killSpy . mockRestore ( ) ;
1810+ if ( childPid && isProcessAlive ( childPid ) ) {
1811+ process . kill ( childPid , "SIGKILL" ) ;
1812+ }
17501813 }
17511814 } ) ;
17521815
0 commit comments