@@ -1527,6 +1527,38 @@ test("an unreachable registered broker is never terminated while an owner is liv
15271527 assert . equal ( loadBrokerSession ( repo ) ?. pid , first . pid ) ;
15281528} ) ;
15291529
1530+ test ( "a detached test broker self-expires when its test runner cannot clean it" , async ( t ) => {
1531+ if ( process . platform === "win32" ) {
1532+ t . skip ( "Unix broker sockets are required for this contract." ) ;
1533+ return ;
1534+ }
1535+
1536+ const repo = makeTempDir ( ) ;
1537+ const socketPath = path . join ( "/private/tmp" , `cxc-test-ttl-${ process . pid } -${ Date . now ( ) } .sock` ) ;
1538+ const endpoint = `unix:${ socketPath } ` ;
1539+ const broker = spawn ( process . execPath , [ BROKER_SCRIPT , "serve" , "--endpoint" , endpoint , "--cwd" , repo ] , {
1540+ cwd : repo ,
1541+ env : {
1542+ ...process . env ,
1543+ CODEX_COMPANION_TEST_BROKER_TTL_MS : "250"
1544+ } ,
1545+ detached : true ,
1546+ stdio : "ignore"
1547+ } ) ;
1548+ broker . unref ( ) ;
1549+ const ownership = captureProcessOwnership ( broker . pid , { cwd : repo } ) ;
1550+ t . after ( async ( ) => {
1551+ await terminateProcessTree ( broker . pid , {
1552+ expectedRootIdentity : ownership . rootIdentity ,
1553+ ownershipSnapshot : ownership
1554+ } ) . catch ( ( ) => { } ) ;
1555+ } ) ;
1556+
1557+ assert . equal ( await waitForBrokerEndpoint ( endpoint , 2000 ) , true ) ;
1558+ await waitFor ( ( ) => ! hasLiveProcessIdentity ( broker . pid , ownership . rootIdentity ) , { timeoutMs : 2000 } ) ;
1559+ assert . equal ( fs . existsSync ( socketPath ) , false ) ;
1560+ } ) ;
1561+
15301562test ( "pre-activation broker exits when its launcher pipe closes" , async ( t ) => {
15311563 if ( process . platform === "win32" ) {
15321564 t . skip ( "Unix process groups are required for this contract." ) ;
@@ -1541,7 +1573,14 @@ test("pre-activation broker exits when its launcher pipe closes", async (t) => {
15411573 const child = spawn (
15421574 process . execPath ,
15431575 [ BROKER_SCRIPT , "serve" , "--endpoint" , endpoint , "--cwd" , repo , "--pid-file" , pidFile , "--require-activation-stdin" ] ,
1544- { cwd : repo , detached : true , stdio : [ "pipe" , "ignore" , "ignore" ] }
1576+ {
1577+ cwd : repo ,
1578+ env : { ...process . env , CODEX_COMPANION_TEST_BROKER_TTL_MS : "30000" } ,
1579+ detached : true ,
1580+ stdio : [ "pipe" , "ignore" , "ignore" ] ,
1581+ timeout : 30000 ,
1582+ killSignal : "SIGKILL"
1583+ }
15451584 ) ;
15461585 const ownershipSnapshot = captureProcessOwnership ( child . pid , { cwd : repo } ) ;
15471586 t . after ( async ( ) => {
@@ -1595,8 +1634,10 @@ test("an unregistered broker refuses to activate a detached app-server child", a
15951634 const broker = spawn ( process . execPath , [ BROKER_SCRIPT , "serve" , "--endpoint" , endpoint , "--cwd" , repo ] , {
15961635 cwd : repo ,
15971636 env,
1598- detached : true ,
1599- stdio : [ "ignore" , "ignore" , "ignore" ]
1637+ detached : false ,
1638+ stdio : [ "ignore" , "ignore" , "ignore" ] ,
1639+ timeout : 30000 ,
1640+ killSignal : "SIGKILL"
16001641 } ) ;
16011642 const brokerOwnership = captureProcessOwnership ( broker . pid , { cwd : repo , env } ) ;
16021643 t . after ( async ( ) => {
@@ -1605,6 +1646,7 @@ test("an unregistered broker refuses to activate a detached app-server child", a
16051646 expectedRootIdentity : brokerOwnership . rootIdentity ,
16061647 ownershipSnapshot : brokerOwnership
16071648 } ) . catch ( ( ) => { } ) ;
1649+ await waitFor ( ( ) => ! hasLiveProcessIdentity ( broker . pid , brokerOwnership . rootIdentity ) ) ;
16081650 } ) ;
16091651 assert . equal ( await waitForBrokerEndpoint ( endpoint , 2000 ) , true ) ;
16101652
0 commit comments