@@ -5,6 +5,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
55import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js" ;
66import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js" ;
77import type { WorkerConnectionEndpoint } from "../worker/worker-connection-endpoint.js" ;
8+ import { NodeWorkerContainerLifecycle } from "./node-worker-container-lifecycle.js" ;
89import { NodeWorkerLaunchStore } from "./node-worker-launch-store.js" ;
910import { requireNodeWorkerProcessIdentity } from "./node-worker-process-identity.js" ;
1011import { createNodeWorkerSupervisor } from "./node-worker-supervisor.js" ;
@@ -840,6 +841,73 @@ describe("node worker supervisor container isolation", () => {
840841 }
841842 } ) ;
842843
844+ it ( "waits for healthy container shutdown before reporting a sibling removal failure" , async ( ) => {
845+ const fixture = containerFixture ( { capacity : 2 } ) ;
846+ const first = testWorkerLaunchInput ( fixture . workspaceDir , "container-close-failed" , "wait" ) ;
847+ const sibling = testWorkerLaunchInput ( fixture . workspaceDir , "container-close-sibling" , "wait" ) ;
848+ const removalMarker = path . join ( fixture . engineRoot , "hold-removal" ) ;
849+ const store = new NodeWorkerLaunchStore ( { env : fixture . env } ) ;
850+ const removalFailure = new Error ( "injected first container removal failure" ) ;
851+ const originalRemove = Reflect . get (
852+ NodeWorkerContainerLifecycle . prototype ,
853+ "remove" ,
854+ ) as NodeWorkerContainerLifecycle [ "remove" ] ;
855+ const remove = vi
856+ . spyOn ( NodeWorkerContainerLifecycle . prototype , "remove" )
857+ . mockImplementation ( async function ( this : NodeWorkerContainerLifecycle , container , owner ) {
858+ if ( owner . launchId === first . launchId ) {
859+ throw removalFailure ;
860+ }
861+ await originalRemove . call ( this , container , owner ) ;
862+ } ) ;
863+
864+ try {
865+ const failedWorker = await fixture . supervisor . launch ( first , endpoint ) ;
866+ const siblingWorker = await fixture . supervisor . launch ( sibling , endpoint ) ;
867+ await vi . waitFor (
868+ ( ) => expect ( fixture . events ( ) . filter ( ( event ) => event . argv [ 0 ] === "start" ) ) . toHaveLength ( 2 ) ,
869+ { timeout : 5_000 } ,
870+ ) ;
871+ fs . writeFileSync ( removalMarker , "hold" ) ;
872+
873+ const closing = fixture . supervisor . close ( ) ;
874+ const settled = vi . fn ( ) ;
875+ void closing . then ( settled , settled ) ;
876+ await vi . waitFor (
877+ ( ) =>
878+ expect (
879+ fixture
880+ . events ( )
881+ . some (
882+ ( event ) =>
883+ event . argv [ 0 ] === "rm" &&
884+ event . argv . at ( - 1 ) === siblingWorker . container ! . containerId ,
885+ ) ,
886+ ) . toBe ( true ) ,
887+ { timeout : 5_000 } ,
888+ ) ;
889+
890+ expect ( settled ) . not . toHaveBeenCalled ( ) ;
891+ expect ( fixture . exists ( siblingWorker . container ! . containerId ) ) . toBe ( true ) ;
892+
893+ fs . unlinkSync ( removalMarker ) ;
894+ await expect ( closing ) . rejects . toBe ( removalFailure ) ;
895+ expect ( fixture . exists ( siblingWorker . container ! . containerId ) ) . toBe ( false ) ;
896+ expect ( store . get ( sibling . launchId ) ) . toMatchObject ( { state : "interrupted" } ) ;
897+ expect ( fixture . exists ( failedWorker . container ! . containerId ) ) . toBe ( true ) ;
898+ expect ( store . get ( first . launchId ) ) . toMatchObject ( {
899+ state : "running" ,
900+ container : failedWorker . container ,
901+ } ) ;
902+ } finally {
903+ if ( fs . existsSync ( removalMarker ) ) {
904+ fs . unlinkSync ( removalMarker ) ;
905+ }
906+ remove . mockRestore ( ) ;
907+ await fixture . supervisor . close ( ) ;
908+ }
909+ } ) ;
910+
843911 it ( "never executes a container worker when its durable identity cannot be recorded" , async ( ) => {
844912 const fixture = containerFixture ( ) ;
845913 const input = testWorkerLaunchInput ( fixture . workspaceDir , "container-journal-failure" , "wait" ) ;
0 commit comments