mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-27 00:00:12 +02:00
Make task::Builder::spawn* methods fallible (#4823)
This commit is contained in:
@@ -11,6 +11,7 @@ use crate::runtime::{Builder, Callback, ToHandle};
|
||||
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
use std::time::Duration;
|
||||
|
||||
pub(crate) struct BlockingPool {
|
||||
@@ -82,6 +83,25 @@ pub(crate) enum Mandatory {
|
||||
NonMandatory,
|
||||
}
|
||||
|
||||
pub(crate) enum SpawnError {
|
||||
/// Pool is shutting down and the task was not scheduled
|
||||
ShuttingDown,
|
||||
/// There are no worker threads available to take the task
|
||||
/// and the OS failed to spawn a new one
|
||||
NoThreads(io::Error),
|
||||
}
|
||||
|
||||
impl From<SpawnError> for io::Error {
|
||||
fn from(e: SpawnError) -> Self {
|
||||
match e {
|
||||
SpawnError::ShuttingDown => {
|
||||
io::Error::new(io::ErrorKind::Other, "blocking pool shutting down")
|
||||
}
|
||||
SpawnError::NoThreads(e) => e,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Task {
|
||||
pub(crate) fn new(task: task::UnownedTask<NoopSchedule>, mandatory: Mandatory) -> Task {
|
||||
Task { task, mandatory }
|
||||
@@ -221,7 +241,7 @@ impl fmt::Debug for BlockingPool {
|
||||
// ===== impl Spawner =====
|
||||
|
||||
impl Spawner {
|
||||
pub(crate) fn spawn(&self, task: Task, rt: &dyn ToHandle) -> Result<(), ()> {
|
||||
pub(crate) fn spawn(&self, task: Task, rt: &dyn ToHandle) -> Result<(), SpawnError> {
|
||||
let mut shared = self.inner.shared.lock();
|
||||
|
||||
if shared.shutdown {
|
||||
@@ -231,7 +251,7 @@ impl Spawner {
|
||||
task.task.shutdown();
|
||||
|
||||
// no need to even push this task; it would never get picked up
|
||||
return Err(());
|
||||
return Err(SpawnError::ShuttingDown);
|
||||
}
|
||||
|
||||
shared.queue.push_back(task);
|
||||
@@ -262,7 +282,7 @@ impl Spawner {
|
||||
Err(e) => {
|
||||
// The OS refused to spawn the thread and there is no thread
|
||||
// to pick up the task that has just been pushed to the queue.
|
||||
panic!("OS can't spawn worker thread: {}", e)
|
||||
return Err(SpawnError::NoThreads(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user