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

fix: avoid blocking worker threads #4857

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
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
14 changes: 8 additions & 6 deletions crates/edr_napi/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ impl Provider {
if let Some((method_name, provider_error)) = reason.provider_error() {
// Ignore potential failure of logging, as returning the original error is more
// important
if let Err(error) = provider
.log_failed_deserialization(&method_name, &provider_error)
let _result = runtime::Handle::current()
.spawn_blocking(move || {
provider.log_failed_deserialization(&method_name, &provider_error)
})
.await
{
log::error!("Failed to log deserialization error: {error}");
}
.map_err(|error| {
napi::Error::new(Status::GenericFailure, error.to_string())
})?;
}

let data = serde_json::from_str(&json_request).ok();
Expand All @@ -105,7 +107,7 @@ impl Provider {
};

let response = runtime::Handle::current()
.spawn(async move { provider.handle_request(request).await })
.spawn_blocking(move || provider.handle_request(request))
.await
.map_err(|e| napi::Error::new(Status::GenericFailure, e.to_string()))?;

Expand Down
12 changes: 7 additions & 5 deletions crates/edr_provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use lazy_static::lazy_static;
use logger::SyncLogger;
use parking_lot::Mutex;
use requests::{eth::handle_set_interval_mining, hardhat::rpc_types::ResetProviderConfig};
use tokio::{runtime, sync::Mutex as AsyncMutex};
use tokio::{runtime, sync::Mutex as AsyncMutex, task};

pub use self::{
config::*,
Expand Down Expand Up @@ -118,24 +118,26 @@ impl<LoggerErrorT: Debug + Send + Sync + 'static> Provider<LoggerErrorT> {
})
}

pub async fn handle_request(
/// Blocking method to handle a request.
pub fn handle_request(
&self,
request: ProviderRequest,
) -> Result<serde_json::Value, ProviderError<LoggerErrorT>> {
let mut data = self.data.lock().await;
let mut data = task::block_in_place(|| self.runtime.block_on(self.data.lock()));

match request {
ProviderRequest::Single(request) => self.handle_single_request(&mut data, request),
ProviderRequest::Batch(requests) => self.handle_batch_request(&mut data, requests),
}
}

pub async fn log_failed_deserialization(
/// Blocking method to log a failed deserialization.
pub fn log_failed_deserialization(
&self,
method_name: &str,
error: &ProviderError<LoggerErrorT>,
) -> Result<(), ProviderError<LoggerErrorT>> {
let mut data = self.data.lock().await;
let mut data = task::block_in_place(|| self.runtime.block_on(self.data.lock()));
data.logger_mut()
.print_method_logs(method_name, Some(error))
.map_err(ProviderError::Logger)
Expand Down
Loading