Skip to content

Commit

Permalink
Fix for interrupts
Browse files Browse the repository at this point in the history
  • Loading branch information
john-sharratt committed May 25, 2024
1 parent 1480210 commit 5b1e6ad
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
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()) {
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)
.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

0 comments on commit 5b1e6ad

Please sign in to comment.