Skip to content

Commit fadec77

Browse files
committed
improve reqwest error handling (#923)
Previously it was possible for reqwest to fail and cause a panic. Now the actual error will be returned to the original caller.
1 parent daf3389 commit fadec77

File tree

1 file changed

+21
-11
lines changed
  • gix-transport/src/client/blocking_io/http/reqwest

1 file changed

+21
-11
lines changed

gix-transport/src/client/blocking_io/http/reqwest/remote.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,20 @@ impl Default for Remote {
155155

156156
/// utilities
157157
impl Remote {
158+
fn restore_thread_after_failure(&mut self) -> http::Error {
159+
let err_that_brought_thread_down = self
160+
.handle
161+
.take()
162+
.expect("thread handle present")
163+
.join()
164+
.expect("handler thread should never panic")
165+
.expect_err("something should have gone wrong with curl (we join on error only)");
166+
*self = Remote::default();
167+
http::Error::InitHttpClient {
168+
source: Box::new(err_that_brought_thread_down),
169+
}
170+
}
171+
158172
fn make_request(
159173
&mut self,
160174
url: &str,
@@ -179,14 +193,18 @@ impl Remote {
179193
None => continue,
180194
};
181195
}
182-
self.request
196+
if self
197+
.request
183198
.send(Request {
184199
url: url.to_owned(),
185200
headers: header_map,
186201
upload_body_kind,
187202
config: self.config.clone(),
188203
})
189-
.expect("the remote cannot be down at this point");
204+
.is_err()
205+
{
206+
return Err(self.restore_thread_after_failure());
207+
}
190208

191209
let Response {
192210
headers,
@@ -195,15 +213,7 @@ impl Remote {
195213
} = match self.response.recv() {
196214
Ok(res) => res,
197215
Err(_) => {
198-
let err = self
199-
.handle
200-
.take()
201-
.expect("always present")
202-
.join()
203-
.expect("no panic")
204-
.expect_err("no receiver means thread is down with init error");
205-
*self = Self::default();
206-
return Err(http::Error::InitHttpClient { source: Box::new(err) });
216+
return Err(self.restore_thread_after_failure());
207217
}
208218
};
209219

0 commit comments

Comments
 (0)