1+ import type { ChildProcess } from "node:child_process" ;
12import { createHash } from "node:crypto" ;
3+ import { channel } from "node:diagnostics_channel" ;
24import fs from "node:fs/promises" ;
35import http from "node:http" ;
46import os from "node:os" ;
57import path from "node:path" ;
68import * as tar from "tar" ;
79import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
810import * as openclawRoot from "../infra/openclaw-root.js" ;
11+ import { createDeferredCore } from "../shared/deferred.js" ;
912import {
1013 DEFAULT_WORKER_BUNDLE_ARCHIVE_LIMITS ,
1114 readWorkerBundleDirectoryManifest ,
@@ -17,12 +20,16 @@ import { NodeWorkerBundleInstaller } from "./node-worker-bundle-installer.js";
1720describe ( "node worker bundle installer" , ( ) => {
1821 let root : string ;
1922 let server : http . Server | undefined ;
23+ let cleanupPrewarming : ( ( ) => Promise < void > ) | undefined ;
2024
2125 beforeEach ( async ( ) => {
2226 root = await fs . mkdtemp ( path . join ( await fs . realpath ( os . tmpdir ( ) ) , "openclaw-node-bundle-" ) ) ;
2327 } ) ;
2428
2529 afterEach ( async ( ) => {
30+ const cleanup = cleanupPrewarming ;
31+ cleanupPrewarming = undefined ;
32+ await cleanup ?.( ) ;
2633 vi . restoreAllMocks ( ) ;
2734 await new Promise < void > ( ( resolve ) => {
2835 if ( ! server ) {
@@ -703,20 +710,25 @@ describe("node worker bundle installer", () => {
703710 ) . rejects . toThrow ( "gateway returned an unexpected worker bundle length" ) ;
704711 } ) ;
705712
706- it ( "cancels prewarming and releases the namespace queue for the next install" , async ( ) => {
707- const slowStarted = path . join ( root , "slow-prewarm-started" ) ;
713+ it ( "cancels prewarming and releases the namespace queue for the next install" , async ( {
714+ signal,
715+ } ) => {
708716 const slow = await bundleFixture ( {
709717 fixtureName : "slow" ,
710718 bundlePrewarm : 1 ,
711- workerSource : `import fs from "node:fs";\nfs.writeFileSync( ${ JSON . stringify ( slowStarted ) } , "started");\nawait new Promise((resolve) => setTimeout(resolve, 2_000)) ;\n` ,
719+ workerSource : 'process.stdout.write( "started");\nprocess.stdin.resume() ;\n' ,
712720 } ) ;
713721 const fastMarker = path . join ( root , "fast-prewarm-finished" ) ;
714722 const fast = await bundleFixture ( {
715723 fixtureName : "fast" ,
716724 bundlePrewarm : 1 ,
717725 prewarmMarker : fastMarker ,
718726 } ) ;
727+ const fastRequested = createDeferredCore ( ) ;
719728 server = http . createServer ( ( req , res ) => {
729+ if ( req . url ?. endsWith ( fast . input . build . bundleHash ) ) {
730+ fastRequested . resolve ( ) ;
731+ }
720732 const archive = req . url ?. endsWith ( slow . input . build . bundleHash ) ? slow . archive : fast . archive ;
721733 res . writeHead ( 200 , {
722734 "content-type" : "application/octet-stream" ,
@@ -734,25 +746,73 @@ describe("node worker bundle installer", () => {
734746 const gatewayUrl = `ws://127.0.0.1:${ address . port } ` ;
735747 const installer = new NodeWorkerBundleInstaller ( { root } ) ;
736748 const controller = new AbortController ( ) ;
749+ const cleanupController = new AbortController ( ) ;
750+ const testSignal = AbortSignal . any ( [ signal , cleanupController . signal ] ) ;
751+ const started = createDeferredCore < ChildProcess > ( ) ;
752+ const children = new Map < ChildProcess , Promise < void > > ( ) ;
753+ const entries = [ slow , fast ] . map ( ( fixture ) =>
754+ path . join (
755+ root ,
756+ fixture . input . gatewayNamespace ,
757+ "bundles" ,
758+ fixture . input . build . bundleHash ,
759+ "worker.mjs" ,
760+ ) ,
761+ ) ;
762+ const childProcesses = channel ( "child_process" ) ;
763+ const trackPrewarm = ( message : unknown ) => {
764+ const child = ( message as { process : ChildProcess } ) . process ;
765+ child . once ( "spawn" , ( ) => {
766+ if ( ! entries . includes ( child . spawnargs [ 1 ] ?? "" ) ) {
767+ return ;
768+ }
769+ const closed = createDeferredCore ( ) ;
770+ child . once ( "close" , ( ) => closed . resolve ( ) ) ;
771+ children . set ( child , closed . promise ) ;
772+ if ( child . spawnargs [ 1 ] === entries [ 0 ] ) {
773+ child . stdout ! . once ( "data" , ( ) => started . resolve ( child ) ) ;
774+ }
775+ } ) ;
776+ } ;
777+ childProcesses . subscribe ( trackPrewarm ) ;
737778 const first = installer . ensure ( {
738779 input : slow . input ,
739780 gatewayUrl,
740- signal : controller . signal ,
781+ signal : AbortSignal . any ( [ controller . signal , testSignal ] ) ,
741782 } ) ;
742- await vi . waitFor ( async ( ) => await expect ( fs . access ( slowStarted ) ) . resolves . toBeUndefined ( ) ) ;
743- const second = installer . ensure ( { input : fast . input , gatewayUrl } ) ;
783+ const installs = [ first ] ;
784+ cleanupPrewarming = async ( ) => {
785+ cleanupController . abort ( ) ;
786+ for ( const child of children . keys ( ) ) {
787+ if ( child . exitCode === null && child . signalCode === null ) {
788+ child . kill ( "SIGKILL" ) ;
789+ }
790+ }
791+ await Promise . allSettled ( [ ...installs , ...children . values ( ) ] ) ;
792+ childProcesses . unsubscribe ( trackPrewarm ) ;
793+ } ;
794+ // Startup time is not the cancellation contract: hold the real child until
795+ // abort, and join its close event even when readiness or assertions fail.
796+ const slowChild = await Promise . race ( [
797+ started . promise ,
798+ first . then ( ( ) => {
799+ throw new Error ( "prewarm finished before cancellation" ) ;
800+ } ) ,
801+ ] ) ;
802+ testSignal . throwIfAborted ( ) ;
803+ const second = installer . ensure ( { input : fast . input , gatewayUrl, signal : testSignal } ) ;
804+ installs . push ( second ) ;
744805
745806 controller . abort ( new Error ( "launch fenced" ) ) ;
746807
747- await expect ( first ) . rejects . toThrow ( "launch fenced" ) ;
748- await expect (
749- Promise . race ( [
750- second ,
751- new Promise < never > ( ( _ , reject ) => {
752- setTimeout ( ( ) => reject ( new Error ( "namespace queue stayed occupied" ) ) , 750 ) ;
753- } ) ,
754- ] ) ,
755- ) . resolves . toEqual ( fast . input . build ) ;
808+ // Bound handoff from cancellation; acquisition precedes cold extraction and prewarm.
809+ await Promise . all ( [
810+ vi . waitFor ( ( ) => expect ( fastRequested . promise ) . resolves . toBeUndefined ( ) , { timeout : 750 } ) ,
811+ expect ( first ) . rejects . toThrow ( "launch fenced" ) ,
812+ ] ) ;
813+ await expect ( second ) . resolves . toEqual ( fast . input . build ) ;
814+ await children . get ( slowChild ) ;
815+ expect ( slowChild . killed ) . toBe ( true ) ;
756816 await expect ( fs . readFile ( fastMarker , "utf8" ) ) . resolves . toBe ( "ready" ) ;
757817 } ) ;
758818} ) ;
0 commit comments