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: Prevent panicking in VirtualTaskManagerExt::spawn_and_block_on #4322

Merged
merged 3 commits into from
Jan 30, 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
8 changes: 4 additions & 4 deletions lib/cli/src/commands/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl Run {
.spawn_and_block_on(async move {
BinaryPackage::from_registry(&specifier, inner_runtime.as_ref()).await
})
.with_context(|| format!("Unable to load \"{name}\""))?
.with_context(|| format!("Unable to load \"{name}\""))??
};
dependencies.push(pkg);
}
Expand Down Expand Up @@ -574,7 +574,7 @@ impl PackageSource {
let inner_rt = rt.clone();
let pkg = rt.task_manager().spawn_and_block_on(async move {
BinaryPackage::from_registry(&inner_pck, inner_rt.as_ref()).await
})?;
})??;
Ok(ExecutableTarget::Package(pkg))
}
}
Expand Down Expand Up @@ -667,7 +667,7 @@ impl ExecutableTarget {
let inner_runtime = runtime.clone();
let pkg = runtime.task_manager().spawn_and_block_on(async move {
BinaryPackage::from_webc(&container, inner_runtime.as_ref()).await
})?;
})??;

Ok(ExecutableTarget::Package(pkg))
}
Expand Down Expand Up @@ -718,7 +718,7 @@ impl ExecutableTarget {
let inner_runtime = runtime.clone();
let pkg = runtime.task_manager().spawn_and_block_on(async move {
BinaryPackage::from_webc(&container, inner_runtime.as_ref()).await
})?;
})??;
Ok(ExecutableTarget::Package(pkg))
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/src/commands/run/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl Wasi {
.spawn_and_block_on(async move {
BinaryPackage::from_registry(&specifier, &*inner_rt).await
})
.with_context(|| format!("Unable to load \"{name}\""))?
.with_context(|| format!("Unable to load \"{name}\""))??
};
uses.push(pkg);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/wasix/src/runners/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ impl crate::runners::Runner for WasiRunner {
.context("Unable to wait for the process to exit")
}
.in_current_span(),
)?;
)??;

if exit_code.raw() == 0 {
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion lib/wasix/src/runners/wcgi/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl WcgiRunner {
})
.await
})
.context("Unable to start the server")?;
.context("Unable to start the server")??;

Ok(())
}
Expand Down
17 changes: 12 additions & 5 deletions lib/wasix/src/runtime/task_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,10 @@ impl dyn VirtualTaskManager {
pub trait VirtualTaskManagerExt {
/// Runs the work in the background via the task managers shared background
/// threads while blocking the current execution until it finishs
fn spawn_and_block_on<A>(&self, task: impl Future<Output = A> + Send + 'static) -> A
fn spawn_and_block_on<A>(
&self,
task: impl Future<Output = A> + Send + 'static,
) -> Result<A, anyhow::Error>
where
A: Send + 'static;

Expand All @@ -422,17 +425,21 @@ where
{
/// Runs the work in the background via the task managers shared background
/// threads while blocking the current execution until it finishs
fn spawn_and_block_on<A>(&self, task: impl Future<Output = A> + Send + 'static) -> A
fn spawn_and_block_on<A>(
&self,
task: impl Future<Output = A> + Send + 'static,
) -> Result<A, anyhow::Error>
where
A: Send + 'static,
{
let (work_tx, mut work_rx) = ::tokio::sync::mpsc::unbounded_channel();
let (tx, rx) = ::tokio::sync::oneshot::channel();
let work = Box::pin(async move {
let ret = task.await;
work_tx.send(ret).ok();
tx.send(ret).ok();
});
self.task_shared(Box::new(move || work)).unwrap();
InlineWaker::block_on(work_rx.recv()).unwrap()
rx.blocking_recv()
.map_err(|_| anyhow::anyhow!("task execution failed - result channel dropped"))
}

fn spawn_await<O, F>(
Expand Down
Loading