@@ -40,6 +40,7 @@ impl AgentConnection {
4040 handler : H ,
4141 outgoing_bytes : impl Unpin + AsyncWrite ,
4242 incoming_bytes : impl Unpin + AsyncRead ,
43+ spawn : impl Fn ( LocalBoxFuture < ' static , ( ) > ) ,
4344 ) -> (
4445 Self ,
4546 impl Future < Output = ( ) > ,
@@ -53,6 +54,7 @@ impl AgentConnection {
5354 } ) ,
5455 outgoing_bytes,
5556 incoming_bytes,
57+ spawn,
5658 ) ;
5759 ( Self ( connection) , handler_task, io_task)
5860 }
@@ -81,6 +83,7 @@ impl ClientConnection {
8183 handler : H ,
8284 outgoing_bytes : impl Unpin + AsyncWrite ,
8385 incoming_bytes : impl Unpin + AsyncRead ,
86+ spawn : impl Fn ( LocalBoxFuture < ' static , ( ) > ) ,
8487 ) -> (
8588 Self ,
8689 impl Future < Output = ( ) > ,
@@ -94,6 +97,7 @@ impl ClientConnection {
9497 } ) ,
9598 outgoing_bytes,
9699 incoming_bytes,
100+ spawn,
97101 ) ;
98102 ( Self ( connection) , handler_task, io_task)
99103 }
@@ -189,6 +193,7 @@ where
189193 request_handler : Box < dyn ' static + Fn ( In ) -> LocalBoxFuture < ' static , Result < In :: Response > > > ,
190194 outgoing_bytes : impl Unpin + AsyncWrite ,
191195 incoming_bytes : impl Unpin + AsyncRead ,
196+ spawn : impl Fn ( LocalBoxFuture < ' static , ( ) > ) ,
192197 ) -> (
193198 Self ,
194199 impl Future < Output = ( ) > ,
@@ -201,7 +206,7 @@ where
201206 outgoing_tx : outgoing_tx. clone ( ) ,
202207 next_id : AtomicI32 :: new ( 0 ) ,
203208 } ;
204- let handler_task = Self :: handle_incoming ( outgoing_tx, incoming_rx, request_handler) ;
209+ let handler_task = Self :: handle_incoming ( outgoing_tx, incoming_rx, request_handler, spawn ) ;
205210 let io_task = Self :: handle_io (
206211 outgoing_rx,
207212 incoming_tx,
@@ -309,27 +314,35 @@ where
309314 incoming_handler : Box <
310315 dyn ' static + Fn ( In ) -> LocalBoxFuture < ' static , Result < In :: Response > > ,
311316 > ,
317+ spawn : impl Fn ( LocalBoxFuture < ' static , ( ) > ) ,
312318 ) {
313319 while let Some ( ( id, params) ) = incoming_rx. next ( ) . await {
314- let result = incoming_handler ( params) . await ;
315- match result {
316- Ok ( result) => {
317- outgoing_tx
318- . unbounded_send ( OutgoingMessage :: OkResponse { id, result } )
319- . ok ( ) ;
320- }
321- Err ( error) => {
322- outgoing_tx
323- . unbounded_send ( OutgoingMessage :: ErrorResponse {
324- id,
325- error : Error {
326- code : -32603 ,
327- message : error. to_string ( ) ,
328- } ,
329- } )
330- . ok ( ) ;
320+ let result = incoming_handler ( params) ;
321+ let outgoing_tx = outgoing_tx. clone ( ) ;
322+ spawn (
323+ async move {
324+ let result = result. await ;
325+ match result {
326+ Ok ( result) => {
327+ outgoing_tx
328+ . unbounded_send ( OutgoingMessage :: OkResponse { id, result } )
329+ . ok ( ) ;
330+ }
331+ Err ( error) => {
332+ outgoing_tx
333+ . unbounded_send ( OutgoingMessage :: ErrorResponse {
334+ id,
335+ error : Error {
336+ code : -32603 ,
337+ message : error. to_string ( ) ,
338+ } ,
339+ } )
340+ . ok ( ) ;
341+ }
342+ }
331343 }
332- }
344+ . boxed_local ( ) ,
345+ )
333346 }
334347 }
335348}
0 commit comments