@@ -3,7 +3,7 @@ import { execFile } from "node:child_process";
33import { EventEmitter , once } from "node:events" ;
44import fs from "node:fs" ;
55import fsp from "node:fs/promises" ;
6- import { createServer } from "node:http" ;
6+ import { Agent , createServer } from "node:http" ;
77import type { AddressInfo } from "node:net" ;
88import os from "node:os" ;
99import path from "node:path" ;
@@ -13,11 +13,18 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
1313import { WebSocketServer } from "ws" ;
1414
1515const spawnMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
16+ const execFileSyncMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
1617
1718vi . mock ( "node:child_process" , async ( ) => {
1819 const actual = await vi . importActual < typeof import ( "node:child_process" ) > ( "node:child_process" ) ;
1920 return {
2021 ...actual ,
22+ execFileSync : ( ...args : unknown [ ] ) => {
23+ const mock = execFileSyncMock . getMockImplementation ( ) ;
24+ return mock
25+ ? mock ( ...args )
26+ : ( actual . execFileSync as unknown as ( ...actualArgs : unknown [ ] ) => unknown ) ( ...args ) ;
27+ } ,
2128 spawn : ( ...args : unknown [ ] ) => spawnMock ( ...args ) ,
2229 } ;
2330} ) ;
@@ -67,6 +74,7 @@ import {
6774 launchOpenClawChrome ,
6875 ManagedChromeCleanupError ,
6976 resolveOpenClawUserDataDir ,
77+ stopOwnedOpenClawChrome ,
7078} from "./chrome.js" ;
7179import type { ResolvedBrowserConfig , ResolvedBrowserProfile } from "./config.js" ;
7280import { BROWSER_ERROR_REASONS , BrowserProfileUnavailableError } from "./errors.js" ;
@@ -401,6 +409,7 @@ describe("chrome.ts internal", () => {
401409 vi . unstubAllGlobals ( ) ;
402410 vi . restoreAllMocks ( ) ;
403411 spawnMock . mockReset ( ) ;
412+ execFileSyncMock . mockReset ( ) ;
404413 ensurePortAvailableMock . mockReset ( ) ;
405414 ensurePortAvailableMock . mockImplementation ( async ( ) => { } ) ;
406415 registerManagedProxyBrowserCdpBypassMock . mockReset ( ) ;
@@ -1261,6 +1270,116 @@ describe("chrome.ts internal", () => {
12611270 }
12621271 } ) ;
12631272
1273+ it ( "stops a managed Chrome from another runtime through a pinned CDP connection" , async ( ) => {
1274+ const originalPlatform = process . platform ;
1275+ const executablePath = path . join ( tmpDir , "chrome" ) ;
1276+ await fsp . writeFile ( executablePath , "" ) ;
1277+ const existsSync = fs . existsSync . bind ( fs ) ;
1278+ vi . spyOn ( fs , "existsSync" ) . mockImplementation ( ( candidate ) => {
1279+ const value = String ( candidate ) ;
1280+ return (
1281+ value . endsWith ( "Local State" ) || value . endsWith ( "Preferences" ) || existsSync ( candidate )
1282+ ) ;
1283+ } ) ;
1284+
1285+ const managedPid = 43213 ;
1286+ let managedProcessAlive = true ;
1287+ let processStartTime = "Fri Jul 17 12:00:00 2026" ;
1288+ let rotateProcessIdentity = true ;
1289+ const killSpy = vi . spyOn ( process , "kill" ) . mockImplementation ( ( ( pid , signal ) => {
1290+ if ( pid === managedPid && signal === 0 && ! managedProcessAlive ) {
1291+ const error = new Error ( "no such process" ) as NodeJS . ErrnoException ;
1292+ error . code = "ESRCH" ;
1293+ throw error ;
1294+ }
1295+ return true ;
1296+ } ) as typeof process . kill ) ;
1297+ const connectionSpy = vi . spyOn ( Agent . prototype , "createConnection" ) ;
1298+
1299+ Object . defineProperty ( process , "platform" , { value : "darwin" } ) ;
1300+ try {
1301+ await withMockChromeCdpServer ( {
1302+ wsPath : "/devtools/browser/CROSS_PROCESS_OWNER" ,
1303+ onConnection : ( server ) => {
1304+ server . on ( "connection" , ( socket ) => {
1305+ socket . on ( "message" , ( raw ) => {
1306+ const message = JSON . parse ( rawDataToString ( raw ) ) as {
1307+ id : number ;
1308+ method : string ;
1309+ } ;
1310+ if ( message . method === "SystemInfo.getProcessInfo" ) {
1311+ if ( rotateProcessIdentity ) {
1312+ processStartTime = "Fri Jul 17 12:01:00 2026" ;
1313+ rotateProcessIdentity = false ;
1314+ }
1315+ socket . send (
1316+ JSON . stringify ( {
1317+ id : message . id ,
1318+ result : { processInfo : [ { type : "browser" , id : managedPid } ] } ,
1319+ } ) ,
1320+ ) ;
1321+ return ;
1322+ }
1323+ expect ( message . method ) . toBe ( "Browser.close" ) ;
1324+ managedProcessAlive = false ;
1325+ socket . send ( JSON . stringify ( { id : message . id , result : { } } ) ) ;
1326+ } ) ;
1327+ } ) ;
1328+ } ,
1329+ run : async ( baseUrl ) => {
1330+ const port = Number ( new URL ( baseUrl ) . port ) ;
1331+ const profile = {
1332+ ...makeProfile ( port ) ,
1333+ cdpUrl : baseUrl ,
1334+ driver : "openclaw" ,
1335+ executablePath,
1336+ } as ResolvedBrowserProfile ;
1337+ const userDataDir = resolveOpenClawUserDataDir ( profile . name ) ;
1338+ execFileSyncMock . mockImplementation ( ( command : string , args : string [ ] ) => {
1339+ if ( command === "ps" && args . includes ( "command=" ) ) {
1340+ return `${ executablePath } --remote-debugging-port=${ port } --user-data-dir=${ userDataDir } \n` ;
1341+ }
1342+ if ( command === "ps" && args . includes ( "lstart=" ) ) {
1343+ return `${ processStartTime } \n` ;
1344+ }
1345+ if ( command === "lsof" ) {
1346+ return `p${ managedPid } \n` ;
1347+ }
1348+ throw new Error ( `unexpected command: ${ command } ` ) ;
1349+ } ) ;
1350+ await fsp . mkdir ( userDataDir , { recursive : true } ) ;
1351+ await fsp . symlink (
1352+ `${ os . hostname ( ) } -${ managedPid } ` ,
1353+ path . join ( userDataDir , "SingletonLock" ) ,
1354+ ) ;
1355+
1356+ try {
1357+ const resolved = makeResolved ( {
1358+ ssrfPolicy : { dangerouslyAllowPrivateNetwork : true } ,
1359+ } ) ;
1360+ await expect ( stopOwnedOpenClawChrome ( resolved , profile ) ) . resolves . toBe ( false ) ;
1361+ expect ( managedProcessAlive ) . toBe ( true ) ;
1362+ expect ( killSpy ) . not . toHaveBeenCalledWith ( managedPid , "SIGTERM" ) ;
1363+ await expect (
1364+ fsp . lstat ( path . join ( userDataDir , "SingletonLock" ) ) ,
1365+ ) . resolves . toBeTruthy ( ) ;
1366+
1367+ await expect ( stopOwnedOpenClawChrome ( resolved , profile ) ) . resolves . toBe ( true ) ;
1368+ expect (
1369+ connectionSpy . mock . calls . some ( ( [ options ] ) => typeof options . lookup === "function" ) ,
1370+ ) . toBe ( true ) ;
1371+ expect ( killSpy ) . not . toHaveBeenCalledWith ( managedPid , "SIGTERM" ) ;
1372+ expect ( fs . existsSync ( path . join ( userDataDir , "SingletonLock" ) ) ) . toBe ( false ) ;
1373+ } finally {
1374+ await fsp . rm ( userDataDir , { recursive : true , force : true } ) ;
1375+ }
1376+ } ,
1377+ } ) ;
1378+ } finally {
1379+ Object . defineProperty ( process , "platform" , { value : originalPlatform } ) ;
1380+ }
1381+ } ) ;
1382+
12641383 it ( "does not stop a current-host lock pid without managed Chrome ownership proof" , async ( ) => {
12651384 const originalPlatform = process . platform ;
12661385 const executablePath = path . join ( tmpDir , "chrome" ) ;
0 commit comments