@@ -21,7 +21,7 @@ use winsafe::co::{CP, WC};
2121use xxhash_rust:: const_xxh3:: xxh3_128;
2222
2323use crate :: {
24- TrackedChild ,
24+ ChildTermination , TrackedChild ,
2525 command:: Command ,
2626 error:: SpawnError ,
2727 fixture:: Fixture ,
@@ -81,17 +81,9 @@ pub(crate) async fn spawn_impl(command: Command) -> Result<TrackedChild, SpawnEr
8181 let ( channel_conf, receiver) =
8282 channel ( SHM_CAPACITY ) . map_err ( SpawnError :: ChannelCreationError ) ?;
8383
84- let accesses_future = async move {
85- let ipc_receiver_lock_guard = OwnedReceiverLockGuard :: lock_async ( receiver) . await ?;
86- io:: Result :: Ok ( PathAccessIterable { ipc_receiver_lock_guard } )
87- }
88- . boxed ( ) ;
89-
90- // let path_access_stream = PathAccessIterable { pipe_receiver };
91-
9284 let mut spawn_success = false ;
9385 let spawn_success = & mut spawn_success;
94- let child = command
86+ let mut child = command
9587 . spawn_with ( |std_command| {
9688 let std_child = std_command. spawn ( ) ?;
9789 * spawn_success = true ;
@@ -138,5 +130,22 @@ pub(crate) async fn spawn_impl(command: Command) -> Result<TrackedChild, SpawnEr
138130 }
139131 } ) ?;
140132
141- Ok ( TrackedChild { tokio_child : child, accesses_future } )
133+ Ok ( TrackedChild {
134+ stdin : child. stdin . take ( ) ,
135+ stdout : child. stdout . take ( ) ,
136+ stderr : child. stderr . take ( ) ,
137+ // Keep polling for the child to exit in the background even if `wait_handle` is not awaited,
138+ // because we need to stop the supervisor and lock the channel as soon as the child exits.
139+ wait_handle : tokio:: spawn ( async move {
140+ let status = child. wait ( ) . await ?;
141+ // Lock the ipc channel after the child has exited.
142+ // We are not interested in path accesses from decendants after the main child has exited.
143+ let ipc_receiver_lock_guard = OwnedReceiverLockGuard :: lock_async ( receiver) . await ?;
144+ let path_accesses = PathAccessIterable { ipc_receiver_lock_guard } ;
145+
146+ io:: Result :: Ok ( ChildTermination { status, path_accesses } )
147+ } )
148+ . map ( |f| io:: Result :: Ok ( f??) ) // flatten JoinError and io::Result
149+ . boxed ( ) ,
150+ } )
142151}
0 commit comments