Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.0.x] Fix panic when calling certain API endpoints #552

Merged
merged 2 commits into from
Dec 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions impls/src/client_utils/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use std::fmt::{self, Display};
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::runtime::{Builder, Runtime};
use tokio::runtime::{Builder, Handle, Runtime};

// Global Tokio runtime.
// Needs a `Mutex` because `Runtime::block_on` requires mutable access.
Expand Down Expand Up @@ -101,6 +101,7 @@ impl From<Context<ErrorKind>> for Error {
}
}

#[derive(Clone)]
pub struct Client {
/// Whether to use socks proxy
pub use_socks: bool,
Expand Down Expand Up @@ -339,9 +340,20 @@ impl Client {
}

pub fn send_request(&self, req: Request<Body>) -> Result<String, Error> {
RUNTIME
.lock()
.unwrap()
.block_on(self.send_request_async(req))
// This client is currently used both outside and inside of a tokio runtime
// context. In the latter case we are not allowed to do a blocking call to
// our global runtime, which unfortunately means we have to spawn a new thread
if Handle::try_current().is_ok() {
let rt = RUNTIME.clone();
let client = self.clone();
std::thread::spawn(move || rt.lock().unwrap().block_on(client.send_request_async(req)))
.join()
.unwrap()
} else {
RUNTIME
.lock()
.unwrap()
.block_on(self.send_request_async(req))
}
}
}