Skip to content

Commit

Permalink
fix(solc): make scoped reporter work in parallel (gakonst#1214)
Browse files Browse the repository at this point in the history
* fix(solc): make scoped reporter work in parallel

* typo
  • Loading branch information
mattsse authored May 3, 2022
1 parent c8c81b4 commit 2304ed5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 10 additions & 1 deletion ethers-solc/src/compile/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,20 @@ fn compile_parallel(
}
}

// need to get the currently installed reporter before installing the pool, otherwise each new
// thread in the pool will get initialized with the default value of the `thread_local!`'s
// localkey. This way we keep access to the reporter in the rayon pool
let scoped_report = report::get_default(|reporter| reporter.clone());

// start a rayon threadpool that will execute all `Solc::compile()` processes
let pool = rayon::ThreadPoolBuilder::new().num_threads(num_jobs).build().unwrap();

let outputs = pool.install(move || {
jobs.into_par_iter()
.map(|(solc, version, input, actually_dirty)| {
.map(move |(solc, version, input, actually_dirty)| {
// set the reporter on this thread
let _guard = report::set_scoped(&scoped_report);

tracing::trace!(
"calling solc `{}` {:?} with {} sources: {:?}",
version,
Expand Down
4 changes: 3 additions & 1 deletion ethers-solc/src/report/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ where
/// print custom messages to `stdout`.
///
/// A `Reporter` is entirely passive and only listens to incoming "events".
pub trait Reporter: 'static {
pub trait Reporter: 'static + std::fmt::Debug {
/// Callback invoked right before [`Solc::compile()`] is called
///
/// This contains the [Solc] its [Version] the complete [CompilerInput] and all files that
Expand Down Expand Up @@ -453,6 +453,7 @@ mod tests {

#[test]
fn scoped_reporter_works() {
#[derive(Debug)]
struct TestReporter;
impl Reporter for TestReporter {}

Expand All @@ -468,6 +469,7 @@ mod tests {
});

set_global_reporter(Report::new(BasicStdoutReporter::default())).unwrap();
#[derive(Debug)]
struct TestReporter;
impl Reporter for TestReporter {}

Expand Down

0 comments on commit 2304ed5

Please sign in to comment.