@@ -67,11 +67,6 @@ describe("handleDirList — happy path", () => {
6767 await fs . mkdir ( replacement ) ;
6868 await fs . writeFile ( path . join ( approved , "approved.txt" ) , "approved" ) ;
6969 await fs . writeFile ( path . join ( replacement , "secret.txt" ) , "secret" ) ;
70- await Promise . all (
71- Array . from ( { length : 5000 } , ( _ , index ) =>
72- fs . writeFile ( path . join ( approved , `f-${ String ( index ) . padStart ( 4 , "0" ) } ` ) , "" ) ,
73- ) ,
74- ) ;
7570 await fs . symlink ( approved , current ) ;
7671 const approvedStats = await fs . stat ( approved , { bigint : true } ) ;
7772
@@ -80,40 +75,50 @@ describe("handleDirList — happy path", () => {
8075 expectedCanonicalPath : approved ,
8176 expectedDevice : String ( approvedStats . dev ) ,
8277 expectedInode : String ( approvedStats . ino ) ,
83- maxEntries : 5001 ,
78+ maxEntries : 10 ,
8479 offset : 0 ,
8580 } ) ;
86- const child = spawn ( command [ 0 ] ! , command . slice ( 1 ) , {
87- stdio : [ "ignore" , "pipe" , "pipe" ] ,
88- } ) ;
81+ // Retarget after the real chdir, before the unchanged worker checks and
82+ // enumerates its bound directory. Polling /proc can miss the entire child.
83+ const retargetAfterBinding = `(() => {
84+ const fs = require("node:fs");
85+ const chdir = process.chdir;
86+ process.chdir = (directory) => {
87+ chdir(directory);
88+ process.chdir = chdir;
89+ fs.unlinkSync(${ JSON . stringify ( current ) } );
90+ fs.symlinkSync(${ JSON . stringify ( replacement ) } , ${ JSON . stringify ( current ) } );
91+ };
92+ })();` ;
93+ const child = spawn (
94+ command [ 0 ] ! ,
95+ [ command [ 1 ] ! , retargetAfterBinding + command [ 2 ] ! , ...command . slice ( 3 ) ] ,
96+ {
97+ stdio : [ "ignore" , "pipe" , "pipe" ] ,
98+ } ,
99+ ) ;
89100 const stdout : Buffer [ ] = [ ] ;
101+ const stderr : Buffer [ ] = [ ] ;
90102 child . stdout . on ( "data" , ( chunk : Buffer ) => stdout . push ( chunk ) ) ;
103+ child . stderr . on ( "data" , ( chunk : Buffer ) => stderr . push ( chunk ) ) ;
91104 const exit = new Promise < number | null > ( ( resolve , reject ) => {
92105 child . once ( "error" , reject ) ;
93- child . once ( "exit " , resolve ) ;
106+ child . once ( "close " , resolve ) ;
94107 } ) ;
95-
96- let observedBoundCwd = false ;
97- for ( let attempt = 0 ; attempt < 200 && child . exitCode === null ; attempt += 1 ) {
98- const cwd = await fs . readlink ( `/proc/${ child . pid } /cwd` ) . catch ( ( ) => "" ) ;
99- if ( cwd === approved ) {
100- observedBoundCwd = true ;
101- break ;
108+ try {
109+ expect ( await exit , Buffer . concat ( stderr ) . toString ( "utf8" ) ) . toBe ( 0 ) ;
110+ expect ( await fs . readlink ( current ) ) . toBe ( replacement ) ;
111+ const result = JSON . parse ( Buffer . concat ( stdout ) . toString ( "utf8" ) ) as {
112+ entries : Array < { name : string } > ;
113+ } ;
114+ expect ( result . entries . some ( ( entry ) => entry . name === "approved.txt" ) ) . toBe ( true ) ;
115+ expect ( result . entries . some ( ( entry ) => entry . name === "secret.txt" ) ) . toBe ( false ) ;
116+ } finally {
117+ if ( child . exitCode === null && child . signalCode === null ) {
118+ child . kill ( "SIGKILL" ) ;
102119 }
103- await new Promise < void > ( ( resolve ) => {
104- setTimeout ( resolve , 1 ) ;
105- } ) ;
120+ await exit . catch ( ( ) => undefined ) ;
106121 }
107- expect ( observedBoundCwd ) . toBe ( true ) ;
108- await fs . unlink ( current ) ;
109- await fs . symlink ( replacement , current ) ;
110-
111- expect ( await exit ) . toBe ( 0 ) ;
112- const result = JSON . parse ( Buffer . concat ( stdout ) . toString ( "utf8" ) ) as {
113- entries : Array < { name : string } > ;
114- } ;
115- expect ( result . entries . some ( ( entry ) => entry . name === "approved.txt" ) ) . toBe ( true ) ;
116- expect ( result . entries . some ( ( entry ) => entry . name === "secret.txt" ) ) . toBe ( false ) ;
117122 } ,
118123 ) ;
119124
0 commit comments