@@ -1139,6 +1139,7 @@ pub struct AcpAgent {
11391139 prompt_sender : Option < mpsc:: Sender < String > > ,
11401140 config_sender : Option < mpsc:: Sender < PendingConfigUpdate > > ,
11411141 cancel_sender : Arc < tokio:: sync:: Mutex < Option < mpsc:: Sender < ( ) > > > > ,
1142+ shutdown_sender : Option < mpsc:: Sender < ( ) > > ,
11421143 process_handle : Option < tokio:: task:: JoinHandle < ( ) > > ,
11431144 io_handle : Option < tokio:: task:: JoinHandle < ( ) > > ,
11441145 history : Arc < tokio:: sync:: Mutex < Vec < AcpMessage > > > ,
@@ -1174,6 +1175,7 @@ impl AcpAgent {
11741175 prompt_sender : None ,
11751176 config_sender : None ,
11761177 cancel_sender : Arc :: new ( tokio:: sync:: Mutex :: new ( None ) ) ,
1178+ shutdown_sender : None ,
11771179 process_handle : None ,
11781180 io_handle : None ,
11791181 history : Arc :: new ( tokio:: sync:: Mutex :: new ( Vec :: new ( ) ) ) ,
@@ -1203,6 +1205,8 @@ impl AcpAgent {
12031205 let mut cancel_sender_guard = self . cancel_sender . lock ( ) . await ;
12041206 * cancel_sender_guard = Some ( cancel_tx. clone ( ) ) ;
12051207 }
1208+ let ( shutdown_tx, mut shutdown_rx) = mpsc:: channel :: < ( ) > ( 1 ) ;
1209+ self . shutdown_sender = Some ( shutdown_tx) ;
12061210
12071211 let ( bootstrap_tx, bootstrap_rx) = oneshot:: channel :: < Result < SessionBootstrap > > ( ) ;
12081212
@@ -1250,8 +1254,21 @@ impl AcpAgent {
12501254 self . connection = None ;
12511255
12521256 let process_handle = tokio:: spawn ( async move {
1253- let _ = child. wait ( ) . await ;
1254- debug ! ( "ACP agent process ended" ) ;
1257+ tokio:: select! {
1258+ result = child. wait( ) => {
1259+ if let Err ( err) = result {
1260+ debug!( "ACP agent process wait failed: {}" , err) ;
1261+ }
1262+ debug!( "ACP agent process ended" ) ;
1263+ }
1264+ _ = shutdown_rx. recv( ) => {
1265+ if let Err ( err) = child. kill( ) . await {
1266+ debug!( "ACP agent process kill failed: {}" , err) ;
1267+ }
1268+ let _ = child. wait( ) . await ;
1269+ debug!( "ACP agent process stopped manually" ) ;
1270+ }
1271+ }
12551272 } ) ;
12561273 self . process_handle = Some ( process_handle) ;
12571274
@@ -1967,8 +1984,11 @@ impl AcpAgent {
19671984
19681985 pub async fn stop ( & mut self ) {
19691986 self . ready . store ( false , Ordering :: SeqCst ) ;
1987+ if let Some ( shutdown_tx) = self . shutdown_sender . take ( ) {
1988+ let _ = shutdown_tx. send ( ( ) ) . await ;
1989+ }
19701990 if let Some ( handle) = self . process_handle . take ( ) {
1971- handle. abort ( ) ;
1991+ let _ = handle. await ;
19721992 }
19731993 if let Some ( handle) = self . io_handle . take ( ) {
19741994 handle. abort ( ) ;
@@ -2182,6 +2202,13 @@ impl AcpManager {
21822202 }
21832203 }
21842204
2205+ pub async fn stop_all ( & mut self ) {
2206+ let agent_ids: Vec < String > = self . agents . keys ( ) . cloned ( ) . collect ( ) ;
2207+ for agent_id in agent_ids {
2208+ self . stop_agent ( & agent_id) . await ;
2209+ }
2210+ }
2211+
21852212 /// Cancel current prompt for agent by agent_id.
21862213 pub async fn cancel_prompt ( & self , agent_id : & str ) -> Result < ( ) > {
21872214 if let Some ( agent) = self . agents . get ( agent_id) {
0 commit comments