Skip to content

Commit 0d0d363

Browse files
authored
fix(client): don't panic in DNS resolution when task cancelled (#2229)
If the runtime is dropped while a DNS request is being made, it can lead to a panic. This patch checks if the task was cancelled, and returns a generic IO error instead of panicking in that case. The following code reproduces the problem on my macOS machine: ``` use hyper::Client; use tokio::runtime; type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>; fn main() { let rt = runtime::Builder::new() .threaded_scheduler() .core_threads(1) .enable_all() .build() .unwrap(); // spawn a request and then drop the runtime immediately rt.spawn(fetch_url()); } async fn fetch_url() -> Result<()> { let url: hyper::Uri = "http://example.com".parse()?; let client = Client::builder().build_http::<hyper::Body>(); let res = client.get(url).await?; println!("Response: {}", res.status()); Ok(()) } ```
1 parent 9998f0f commit 0d0d363

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/client/connect/dns.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,13 @@ impl Future for GaiFuture {
141141
Pin::new(&mut self.inner).poll(cx).map(|res| match res {
142142
Ok(Ok(addrs)) => Ok(GaiAddrs { inner: addrs }),
143143
Ok(Err(err)) => Err(err),
144-
Err(join_err) => panic!("gai background task failed: {:?}", join_err),
144+
Err(join_err) => {
145+
if join_err.is_cancelled() {
146+
Err(io::Error::new(io::ErrorKind::Interrupted, join_err))
147+
} else {
148+
panic!("gai background task failed: {:?}", join_err)
149+
}
150+
}
145151
})
146152
}
147153
}

0 commit comments

Comments
 (0)