11use std:: fs;
22use std:: process:: { Command , Stdio } ;
33use std:: thread;
4- use std:: time:: Duration ;
4+ use std:: time:: { Duration , Instant } ;
55
66fn main ( ) {
77 let test_dir = std:: env:: temp_dir ( ) . join ( "filelocksmith-ci-test" ) ;
@@ -22,23 +22,36 @@ fn main() {
2222 . args ( [ "-NoProfile" , "-Command" , & ps_command] )
2323 . stdin ( Stdio :: null ( ) )
2424 . stdout ( Stdio :: null ( ) )
25- . stderr ( Stdio :: null ( ) )
25+ . stderr ( Stdio :: piped ( ) )
2626 . spawn ( )
2727 . expect ( "failed to spawn powershell" ) ;
2828
2929 let child_pid = child. id ( ) as usize ;
3030 println ! ( "spawned locking process pid={child_pid}" ) ;
3131
32- // Give PowerShell time to open the file
33- thread:: sleep ( Duration :: from_secs ( 3 ) ) ;
34-
35- // Detect which processes are locking the file
36- let pids = filelocksmith:: find_processes_locking_path ( & test_file) ;
37- println ! ( "locking pids: {pids:?}" ) ;
38- assert ! (
39- pids. contains( & child_pid) ,
40- "expected pid {child_pid} in locking pids {pids:?}"
41- ) ;
32+ // Poll until the lock is detected (PowerShell can be slow to start on CI)
33+ let timeout = Duration :: from_secs ( 30 ) ;
34+ let start = Instant :: now ( ) ;
35+ let mut pids;
36+ loop {
37+ thread:: sleep ( Duration :: from_secs ( 1 ) ) ;
38+ pids = filelocksmith:: find_processes_locking_path ( & test_file) ;
39+ if pids. contains ( & child_pid) {
40+ break ;
41+ }
42+ if start. elapsed ( ) > timeout {
43+ // Check if PowerShell crashed
44+ if let Some ( status) = child. try_wait ( ) . expect ( "failed to check child" ) {
45+ let mut stderr_buf = String :: new ( ) ;
46+ if let Some ( mut stderr) = child. stderr . take ( ) {
47+ std:: io:: Read :: read_to_string ( & mut stderr, & mut stderr_buf) . ok ( ) ;
48+ }
49+ panic ! ( "powershell exited early with {status}, locking pids: {pids:?}, stderr: {stderr_buf}" ) ;
50+ }
51+ panic ! ( "timed out waiting for pid {child_pid} in locking pids {pids:?}" ) ;
52+ }
53+ }
54+ println ! ( "locking pids: {pids:?} (detected after {:?})" , start. elapsed( ) ) ;
4255
4356 // Verify we can resolve the process path
4457 let proc_path =
0 commit comments