Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Spawn the network worker with spawn_blocking (#5909)
Browse files Browse the repository at this point in the history
* Spawn the network worker with spawn_blocking

* Some comment adjustments

* Fix shutdown not working
  • Loading branch information
tomaka authored May 12, 2020
1 parent 4e8e1b7 commit f3315cd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
14 changes: 9 additions & 5 deletions client/cli/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,16 @@ impl<C: SubstrateCli> Runner<C> {
// and drop the runtime first.
let _telemetry = service.telemetry();

let f = service.fuse();
pin_mut!(f);
{
let f = service.fuse();
self.tokio_runtime
.block_on(main(f))
.map_err(|e| e.to_string())?;
}

self.tokio_runtime
.block_on(main(f))
.map_err(|e| e.to_string())?;
// The `service` **must** have been destroyed here for the shutdown signal to propagate
// to all the tasks. Dropping `tokio_runtime` will block the thread until all tasks have
// shut down.
drop(self.tokio_runtime);

Ok(())
Expand Down
9 changes: 8 additions & 1 deletion client/service/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,14 @@ ServiceBuilder<
// This is used internally, so don't restrict access to unsafe RPC
let rpc_handlers = gen_handler(sc_rpc::DenyUnsafe::No);

spawn_handle.spawn(
// The network worker is responsible for gathering all network messages and processing
// them. This is quite a heavy task, and at the time of the writing of this comment it
// frequently happens that this future takes several seconds or in some situations
// even more than a minute until it has processed its entire queue. This is clearly an
// issue, and ideally we would like to fix the network future to take as little time as
// possible, but we also take the extra harm-prevention measure to execute the networking
// future using `spawn_blocking`.
spawn_handle.spawn_blocking(
"network-worker",
build_network_future(
config.role.clone(),
Expand Down

0 comments on commit f3315cd

Please sign in to comment.