@@ -6,9 +6,11 @@ mod transport;
66use serde_json:: { json, Value } ;
77use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
88use std:: sync:: Arc ;
9+ use std:: time:: Duration ;
910
1011use tauri:: AppHandle ;
1112use tokio:: sync:: Mutex ;
13+ use tokio:: time:: timeout;
1214
1315use crate :: state:: AppState ;
1416use crate :: types:: { BackendMode , RemoteBackendProvider } ;
@@ -18,6 +20,9 @@ use self::protocol::{build_request_line, DEFAULT_REMOTE_HOST, DISCONNECTED_MESSA
1820use self :: tcp_transport:: TcpTransport ;
1921use self :: transport:: { PendingMap , RemoteTransport , RemoteTransportConfig , RemoteTransportKind } ;
2022
23+ const REMOTE_REQUEST_TIMEOUT : Duration = Duration :: from_secs ( 300 ) ;
24+ const REMOTE_SEND_TIMEOUT : Duration = Duration :: from_secs ( 15 ) ;
25+
2126pub ( crate ) fn normalize_path_for_remote ( path : String ) -> String {
2227 let trimmed = path. trim ( ) ;
2328 if trimmed. is_empty ( ) {
@@ -58,7 +63,7 @@ pub(crate) struct RemoteBackend {
5863}
5964
6065struct RemoteBackendInner {
61- out_tx : tokio:: sync:: mpsc:: UnboundedSender < String > ,
66+ out_tx : tokio:: sync:: mpsc:: Sender < String > ,
6267 pending : Arc < Mutex < PendingMap > > ,
6368 next_id : AtomicU64 ,
6469 connected : Arc < std:: sync:: atomic:: AtomicBool > ,
@@ -75,12 +80,32 @@ impl RemoteBackend {
7580 self . inner . pending . lock ( ) . await . insert ( id, tx) ;
7681
7782 let message = build_request_line ( id, method, params) ?;
78- if self . inner . out_tx . send ( message) . is_err ( ) {
79- self . inner . pending . lock ( ) . await . remove ( & id) ;
80- return Err ( DISCONNECTED_MESSAGE . to_string ( ) ) ;
83+ match timeout ( REMOTE_SEND_TIMEOUT , self . inner . out_tx . send ( message) ) . await {
84+ Ok ( Ok ( ( ) ) ) => { }
85+ Ok ( Err ( _) ) => {
86+ self . inner . pending . lock ( ) . await . remove ( & id) ;
87+ return Err ( DISCONNECTED_MESSAGE . to_string ( ) ) ;
88+ }
89+ Err ( _) => {
90+ self . inner . pending . lock ( ) . await . remove ( & id) ;
91+ return Err ( format ! (
92+ "remote backend request dispatch timed out after {} seconds" ,
93+ REMOTE_SEND_TIMEOUT . as_secs( )
94+ ) ) ;
95+ }
8196 }
8297
83- rx. await . map_err ( |_| DISCONNECTED_MESSAGE . to_string ( ) ) ?
98+ match timeout ( REMOTE_REQUEST_TIMEOUT , rx) . await {
99+ Ok ( Ok ( result) ) => result,
100+ Ok ( Err ( _) ) => Err ( DISCONNECTED_MESSAGE . to_string ( ) ) ,
101+ Err ( _) => {
102+ self . inner . pending . lock ( ) . await . remove ( & id) ;
103+ Err ( format ! (
104+ "remote backend request timed out after {} seconds" ,
105+ REMOTE_REQUEST_TIMEOUT . as_secs( )
106+ ) )
107+ }
108+ }
84109 }
85110}
86111
0 commit comments