mirror of
https://github.com/servo/servo.git
synced 2025-09-27 23:30:08 +01:00
net: clean shutdown of the async runtime (#38425)
The previous use of a static variable for the runtime prevented it from shutting down cleanly, because shutdown requires dropping or taking ownership of it. This PR switches the static variable to a handle only, and introduces a new trait to pass a handle to the async runtime to the constellation, where it can be shut-down along with other components and help reduce our count of still running threads after shutdown. Testing: manual testing, and covered by unit-test in net, and wpt tests. Fixes: part of - https://github.com/servo/servo/issues/30849 --------- Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com>
This commit is contained in:
parent
7ad32f944f
commit
77ff351cde
11 changed files with 132 additions and 49 deletions
|
@ -1,15 +1,45 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
use std::cmp::Ord;
|
||||
use std::sync::LazyLock;
|
||||
use std::sync::OnceLock;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use tokio::runtime::{Builder, Runtime};
|
||||
use futures::Future;
|
||||
use net_traits::AsyncRuntime;
|
||||
use tokio::runtime::{Builder, Handle, Runtime};
|
||||
|
||||
pub static HANDLE: LazyLock<Runtime> = LazyLock::new(|| {
|
||||
Builder::new_multi_thread()
|
||||
/// The actual runtime,
|
||||
/// to be used as part of shut-down.
|
||||
pub struct AsyncRuntimeHolder {
|
||||
runtime: Option<Runtime>,
|
||||
}
|
||||
|
||||
impl AsyncRuntimeHolder {
|
||||
pub(crate) fn new(runtime: Runtime) -> Self {
|
||||
Self {
|
||||
runtime: Some(runtime),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncRuntime for AsyncRuntimeHolder {
|
||||
fn shutdown(&mut self) {
|
||||
self.runtime
|
||||
.take()
|
||||
.expect("Runtime should have been initialized on start-up.")
|
||||
.shutdown_timeout(Duration::from_millis(100))
|
||||
}
|
||||
}
|
||||
|
||||
/// A shared handle to the runtime,
|
||||
/// to be initialized on start-up.
|
||||
static ASYNC_RUNTIME_HANDLE: OnceLock<Handle> = OnceLock::new();
|
||||
|
||||
pub fn init_async_runtime() -> Box<dyn AsyncRuntime> {
|
||||
// Initialize a tokio runtime.
|
||||
let runtime = Builder::new_multi_thread()
|
||||
.thread_name_fn(|| {
|
||||
static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0);
|
||||
let id = ATOMIC_ID.fetch_add(1, Ordering::Relaxed);
|
||||
|
@ -24,5 +54,36 @@ pub static HANDLE: LazyLock<Runtime> = LazyLock::new(|| {
|
|||
.enable_io()
|
||||
.enable_time()
|
||||
.build()
|
||||
.expect("Unable to build tokio-runtime runtime")
|
||||
});
|
||||
.expect("Unable to build tokio-runtime runtime");
|
||||
|
||||
// Make the runtime available to users inside this crate.
|
||||
ASYNC_RUNTIME_HANDLE
|
||||
.set(runtime.handle().clone())
|
||||
.expect("Runtime handle should be initialized once on start-up");
|
||||
|
||||
// Return an async runtime for use in shutdown.
|
||||
Box::new(AsyncRuntimeHolder::new(runtime))
|
||||
}
|
||||
|
||||
/// Spawn a task using the handle to the runtime.
|
||||
pub fn spawn_task<F>(task: F)
|
||||
where
|
||||
F: Future + 'static + std::marker::Send,
|
||||
F::Output: Send + 'static,
|
||||
{
|
||||
ASYNC_RUNTIME_HANDLE
|
||||
.get()
|
||||
.expect("Runtime handle should be initialized on start-up")
|
||||
.spawn(task);
|
||||
}
|
||||
|
||||
/// Spawn a blocking task using the handle to the runtime.
|
||||
pub fn spawn_blocking_task<F, R>(task: F) -> F::Output
|
||||
where
|
||||
F: Future,
|
||||
{
|
||||
ASYNC_RUNTIME_HANDLE
|
||||
.get()
|
||||
.expect("Runtime handle should be initialized on start-up")
|
||||
.block_on(task)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue