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

wasi: Thread Lifecycle Fix #3639

Merged
merged 2 commits into from
Mar 2, 2023
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
4 changes: 2 additions & 2 deletions lib/wasi/src/bin_factory/exec.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{pin::Pin, sync::Arc};

use crate::{
os::task::{thread::WasiThreadGuard, TaskJoinHandle},
os::task::{thread::WasiThreadRunGuard, TaskJoinHandle},
VirtualBusError, WasiRuntimeError,
};
use futures::Future;
Expand Down Expand Up @@ -120,7 +120,7 @@ pub fn spawn_exec_module(
}
};

let thread = WasiThreadGuard::new(wasi_env.thread.clone());
let thread = WasiThreadRunGuard::new(wasi_env.thread.clone());

let mut wasi_env = WasiFunctionEnv::new(&mut store, wasi_env);

Expand Down
14 changes: 13 additions & 1 deletion lib/wasi/src/os/task/task_join_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ impl TaskStatus {
_ => None,
}
}

/// Returns `true` if the task status is [`Finished`].
///
/// [`Finished`]: TaskStatus::Finished
#[must_use]
pub fn is_finished(&self) -> bool {
matches!(self, Self::Finished(..))
}
}

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -88,6 +96,11 @@ impl OwnedTaskStatus {

/// Marks the task as finished.
pub(super) fn set_finished(&self, res: Result<ExitCode, Arc<WasiRuntimeError>>) {
// Don't overwrite a previous finished state.
if self.status().is_finished() {
return;
}

let inner = match res {
Ok(code) => Ok(code),
Err(err) => {
Expand All @@ -98,7 +111,6 @@ impl OwnedTaskStatus {
}
}
};

self.watch.send(TaskStatus::Finished(inner)).ok();
}

Expand Down
6 changes: 3 additions & 3 deletions lib/wasi/src/os/task/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,17 @@ pub struct WasiThread {
/// [`Thread::set_status_running`] or [`Thread::set_status_finished`], but
/// this guard can ensure that the thread is marked as terminated even if this
/// is forgotten or a panic occurs.
pub struct WasiThreadGuard {
pub struct WasiThreadRunGuard {
pub thread: WasiThread,
}

impl WasiThreadGuard {
impl WasiThreadRunGuard {
pub fn new(thread: WasiThread) -> Self {
Self { thread }
}
}

impl Drop for WasiThreadGuard {
impl Drop for WasiThreadRunGuard {
fn drop(&mut self) {
self.thread
.set_status_finished(Err(
Expand Down