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 for interrupts #4747

Merged
merged 1 commit into from
Jun 3, 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
3 changes: 3 additions & 0 deletions lib/wasix/src/os/task/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ impl WasiThread {

/// Adds a signal for this thread to process
pub fn signal(&self, signal: Signal) {
let tid = self.tid();
tracing::trace!(%tid, "signal-thread({:?})", signal);

let mut guard = self.state.signals.lock().unwrap();
if !guard.0.contains(&signal) {
guard.0.push(signal);
Expand Down
6 changes: 5 additions & 1 deletion lib/wasix/src/state/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ impl WasiEnv {
tracing::trace!(
pid=%ctx.data().pid(),
?signal,
"Processing signal",
"processing signal via handler",
);
if let Err(err) = handler.call(ctx, signal as i32) {
match err.downcast::<WasiError>() {
Expand All @@ -832,6 +832,10 @@ impl WasiEnv {
}
}
}
tracing::trace!(
pid=%ctx.data().pid(),
"signal processed",
);
}
Ok(true)
} else {
Expand Down
50 changes: 30 additions & 20 deletions lib/wasix/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ struct AsyncifyPoller<'a, 'b, 'c, T, Fut>
where
Fut: Future<Output = T> + Send + Sync + 'static,
{
process_signals: bool,
ctx: &'b mut FunctionEnvMut<'c, WasiEnv>,
work: &'a mut Pin<Box<Fut>>,
}
Expand All @@ -380,18 +379,37 @@ where
Errno::Child.into()
}))));
}
if self.process_signals && env.thread.has_signals_or_subscribe(cx.waker()) {
theduke marked this conversation as resolved.
Show resolved Hide resolved
let signals = env.thread.signals().lock().unwrap();
for sig in signals.0.iter() {
if *sig == Signal::Sigint
|| *sig == Signal::Sigquit
|| *sig == Signal::Sigkill
|| *sig == Signal::Sigabrt
{
let exit_code = env.thread.set_or_get_exit_code_for_signal(*sig);
return Poll::Ready(Err(WasiError::Exit(exit_code)));
if env.thread.has_signals_or_subscribe(cx.waker()) {
let has_exit = {
let signals = env.thread.signals().lock().unwrap();
signals
.0
.iter()
.filter_map(|sig| {
if *sig == Signal::Sigint
|| *sig == Signal::Sigquit
|| *sig == Signal::Sigkill
|| *sig == Signal::Sigabrt
{
Some(env.thread.set_or_get_exit_code_for_signal(*sig))
} else {
None
}
})
.next()
};

return match WasiEnv::process_signals_and_exit(self.ctx) {
Ok(Ok(_)) => {
if let Some(exit_code) = has_exit {
Poll::Ready(Err(WasiError::Exit(exit_code)))
} else {
Poll::Pending
}
}
}
Ok(Err(err)) => Poll::Ready(Err(WasiError::Exit(ExitCode::Errno(err)))),
Err(err) => Poll::Ready(Err(err)),
};
}
Poll::Pending
}
Expand Down Expand Up @@ -465,13 +483,6 @@ where
false => Duration::from_millis(50),
};

// Determine if we should process signals or now
let process_signals = ctx
.data()
.try_inner()
.map(|i| !i.signal_set)
theduke marked this conversation as resolved.
Show resolved Hide resolved
.unwrap_or(true);

// Box up the trigger
let mut trigger = Box::pin(work);

Expand All @@ -498,7 +509,6 @@ where
Ok(tokio::select! {
// Inner wait with finializer
res = AsyncifyPoller {
process_signals,
ctx: &mut ctx,
work: &mut trigger,
} => {
Expand Down
Loading