Skip to content

Commit 116f378

Browse files
anaslimembenbrandt
andauthored
fix: Clearer error message when connection is broken before messages are sent (#89)
* fix avoid hanging forever when send fails * Fix formatting * removed the unnecessary wait * Return earlier --------- Co-authored-by: Ben Brandt <benjamin.j.brandt@gmail.com>
1 parent 04597c9 commit 116f378

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

src/agent-client-protocol/src/lib.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl Agent for ClientSideConnection {
8181
.request(
8282
AGENT_METHOD_NAMES.initialize,
8383
Some(ClientRequest::InitializeRequest(args)),
84-
)
84+
)?
8585
.await
8686
}
8787

@@ -90,7 +90,7 @@ impl Agent for ClientSideConnection {
9090
.request::<Option<_>>(
9191
AGENT_METHOD_NAMES.authenticate,
9292
Some(ClientRequest::AuthenticateRequest(args)),
93-
)
93+
)?
9494
.await
9595
.map(Option::unwrap_or_default)
9696
}
@@ -101,7 +101,7 @@ impl Agent for ClientSideConnection {
101101
.request::<Option<_>>(
102102
AGENT_METHOD_NAMES.logout,
103103
Some(ClientRequest::LogoutRequest(args)),
104-
)
104+
)?
105105
.await
106106
.map(Option::unwrap_or_default)
107107
}
@@ -111,7 +111,7 @@ impl Agent for ClientSideConnection {
111111
.request(
112112
AGENT_METHOD_NAMES.session_new,
113113
Some(ClientRequest::NewSessionRequest(args)),
114-
)
114+
)?
115115
.await
116116
}
117117

@@ -120,7 +120,7 @@ impl Agent for ClientSideConnection {
120120
.request::<Option<_>>(
121121
AGENT_METHOD_NAMES.session_load,
122122
Some(ClientRequest::LoadSessionRequest(args)),
123-
)
123+
)?
124124
.await
125125
.map(Option::unwrap_or_default)
126126
}
@@ -133,7 +133,7 @@ impl Agent for ClientSideConnection {
133133
.request(
134134
AGENT_METHOD_NAMES.session_set_mode,
135135
Some(ClientRequest::SetSessionModeRequest(args)),
136-
)
136+
)?
137137
.await
138138
}
139139

@@ -142,7 +142,7 @@ impl Agent for ClientSideConnection {
142142
.request(
143143
AGENT_METHOD_NAMES.session_prompt,
144144
Some(ClientRequest::PromptRequest(args)),
145-
)
145+
)?
146146
.await
147147
}
148148

@@ -162,7 +162,7 @@ impl Agent for ClientSideConnection {
162162
.request(
163163
AGENT_METHOD_NAMES.session_set_model,
164164
Some(ClientRequest::SetSessionModelRequest(args)),
165-
)
165+
)?
166166
.await
167167
}
168168

@@ -171,7 +171,7 @@ impl Agent for ClientSideConnection {
171171
.request(
172172
AGENT_METHOD_NAMES.session_list,
173173
Some(ClientRequest::ListSessionsRequest(args)),
174-
)
174+
)?
175175
.await
176176
}
177177

@@ -181,7 +181,7 @@ impl Agent for ClientSideConnection {
181181
.request(
182182
AGENT_METHOD_NAMES.session_fork,
183183
Some(ClientRequest::ForkSessionRequest(args)),
184-
)
184+
)?
185185
.await
186186
}
187187

@@ -191,7 +191,7 @@ impl Agent for ClientSideConnection {
191191
.request(
192192
AGENT_METHOD_NAMES.session_resume,
193193
Some(ClientRequest::ResumeSessionRequest(args)),
194-
)
194+
)?
195195
.await
196196
}
197197

@@ -201,7 +201,7 @@ impl Agent for ClientSideConnection {
201201
.request::<Option<_>>(
202202
AGENT_METHOD_NAMES.session_close,
203203
Some(ClientRequest::CloseSessionRequest(args)),
204-
)
204+
)?
205205
.await
206206
.map(Option::unwrap_or_default)
207207
}
@@ -214,7 +214,7 @@ impl Agent for ClientSideConnection {
214214
.request(
215215
AGENT_METHOD_NAMES.session_set_config_option,
216216
Some(ClientRequest::SetSessionConfigOptionRequest(args)),
217-
)
217+
)?
218218
.await
219219
}
220220

@@ -223,7 +223,7 @@ impl Agent for ClientSideConnection {
223223
.request(
224224
format!("_{}", args.method),
225225
Some(ClientRequest::ExtMethodRequest(args)),
226-
)
226+
)?
227227
.await
228228
}
229229

@@ -441,7 +441,7 @@ impl Client for AgentSideConnection {
441441
.request(
442442
CLIENT_METHOD_NAMES.session_request_permission,
443443
Some(AgentRequest::RequestPermissionRequest(args)),
444-
)
444+
)?
445445
.await
446446
}
447447

@@ -450,7 +450,7 @@ impl Client for AgentSideConnection {
450450
.request::<Option<_>>(
451451
CLIENT_METHOD_NAMES.fs_write_text_file,
452452
Some(AgentRequest::WriteTextFileRequest(args)),
453-
)
453+
)?
454454
.await
455455
.map(Option::unwrap_or_default)
456456
}
@@ -460,7 +460,7 @@ impl Client for AgentSideConnection {
460460
.request(
461461
CLIENT_METHOD_NAMES.fs_read_text_file,
462462
Some(AgentRequest::ReadTextFileRequest(args)),
463-
)
463+
)?
464464
.await
465465
}
466466

@@ -469,7 +469,7 @@ impl Client for AgentSideConnection {
469469
.request(
470470
CLIENT_METHOD_NAMES.terminal_create,
471471
Some(AgentRequest::CreateTerminalRequest(args)),
472-
)
472+
)?
473473
.await
474474
}
475475

@@ -478,7 +478,7 @@ impl Client for AgentSideConnection {
478478
.request(
479479
CLIENT_METHOD_NAMES.terminal_output,
480480
Some(AgentRequest::TerminalOutputRequest(args)),
481-
)
481+
)?
482482
.await
483483
}
484484

@@ -490,7 +490,7 @@ impl Client for AgentSideConnection {
490490
.request::<Option<_>>(
491491
CLIENT_METHOD_NAMES.terminal_release,
492492
Some(AgentRequest::ReleaseTerminalRequest(args)),
493-
)
493+
)?
494494
.await
495495
.map(Option::unwrap_or_default)
496496
}
@@ -503,7 +503,7 @@ impl Client for AgentSideConnection {
503503
.request(
504504
CLIENT_METHOD_NAMES.terminal_wait_for_exit,
505505
Some(AgentRequest::WaitForTerminalExitRequest(args)),
506-
)
506+
)?
507507
.await
508508
}
509509

@@ -512,7 +512,7 @@ impl Client for AgentSideConnection {
512512
.request::<Option<_>>(
513513
CLIENT_METHOD_NAMES.terminal_kill,
514514
Some(AgentRequest::KillTerminalRequest(args)),
515-
)
515+
)?
516516
.await
517517
.map(Option::unwrap_or_default)
518518
}
@@ -529,7 +529,7 @@ impl Client for AgentSideConnection {
529529
.request(
530530
format!("_{}", args.method),
531531
Some(AgentRequest::ExtMethodRequest(args)),
532-
)
532+
)?
533533
.await
534534
}
535535

src/agent-client-protocol/src/rpc.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ where
113113
&self,
114114
method: impl Into<Arc<str>>,
115115
params: Option<Remote::InRequest>,
116-
) -> impl Future<Output = Result<Out>> {
116+
) -> Result<impl Future<Output = Result<Out>>> {
117117
let (tx, rx) = oneshot::channel();
118118
let id = self.next_id.fetch_add(1, Ordering::SeqCst);
119119
let id = RequestId::Number(id);
@@ -139,16 +139,19 @@ where
139139
.is_err()
140140
{
141141
self.pending_responses.lock().unwrap().remove(&id);
142+
return Err(
143+
Error::internal_error().data("connection closed before request could be sent")
144+
);
142145
}
143-
async move {
146+
Ok(async move {
144147
let result = rx
145148
.await
146149
.map_err(|_| Error::internal_error().data("server shut down unexpectedly"))??
147150
.downcast::<Out>()
148151
.map_err(|_| Error::internal_error().data("failed to deserialize response"))?;
149152

150153
Ok(*result)
151-
}
154+
})
152155
}
153156

154157
async fn handle_io(

0 commit comments

Comments
 (0)