Skip to content

Commit 537a781

Browse files
committed
fix: run HTTP requests in dedicated thread to avoid runtime panic
reqwest::blocking internally calls block_on for DNS resolution which panics when called from within a tokio runtime. Spawn a dedicated thread for the HTTP request to avoid the nested runtime issue.
1 parent 25d8e41 commit 537a781

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

crates/wasm-runtime-interface/src/network.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,19 @@ impl NetworkState {
275275
builder = builder.body(request.body.clone());
276276
}
277277

278-
let response = builder.send().map_err(map_reqwest_error)?;
279-
let status = response.status().as_u16();
280-
let headers = collect_headers(response.headers())?;
281-
282-
let body = read_response_body(response, self.policy.limits.max_response_bytes)?;
278+
// Execute HTTP request in a dedicated thread to avoid
279+
// "Cannot start a runtime from within a runtime" panic when
280+
// reqwest::blocking internally calls block_on for DNS resolution.
281+
let max_response_bytes = self.policy.limits.max_response_bytes;
282+
let (status, headers, body) = std::thread::spawn(move || {
283+
let response = builder.send().map_err(map_reqwest_error)?;
284+
let status = response.status().as_u16();
285+
let headers = collect_headers(response.headers())?;
286+
let body = read_response_body(response, max_response_bytes)?;
287+
Ok::<_, NetworkError>((status, headers, body))
288+
})
289+
.join()
290+
.map_err(|_| NetworkError::HttpFailure("HTTP thread panicked".to_string()))??;
283291

284292
self.ensure_header_limits(&headers)?;
285293

0 commit comments

Comments
 (0)