Skip to content

Commit 761ac43

Browse files
committed
Align winapi with CPython behavior
1 parent c045593 commit 761ac43

File tree

3 files changed

+203
-65
lines changed

3 files changed

+203
-65
lines changed

crates/vm/src/signal.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
use crate::{PyResult, VirtualMachine};
33
use alloc::fmt;
44
use core::sync::atomic::{AtomicBool, Ordering};
5+
#[cfg(windows)]
6+
use core::sync::atomic::AtomicIsize;
57
use std::cell::Cell;
68
use std::sync::mpsc;
79

@@ -12,6 +14,9 @@ static ANY_TRIGGERED: AtomicBool = AtomicBool::new(false);
1214
const ATOMIC_FALSE: AtomicBool = AtomicBool::new(false);
1315
pub(crate) static TRIGGERS: [AtomicBool; NSIG] = [ATOMIC_FALSE; NSIG];
1416

17+
#[cfg(windows)]
18+
static SIGINT_EVENT: AtomicIsize = AtomicIsize::new(0);
19+
1520
thread_local! {
1621
/// Prevent recursive signal handler invocation. When a Python signal
1722
/// handler is running, new signals are deferred until it completes.
@@ -150,3 +155,14 @@ pub fn user_signal_channel() -> (UserSignalSender, UserSignalReceiver) {
150155
let (tx, rx) = mpsc::channel();
151156
(UserSignalSender { tx }, UserSignalReceiver { rx })
152157
}
158+
159+
#[cfg(windows)]
160+
pub fn set_sigint_event(handle: isize) {
161+
SIGINT_EVENT.store(handle, Ordering::Release);
162+
}
163+
164+
#[cfg(windows)]
165+
pub fn get_sigint_event() -> Option<isize> {
166+
let handle = SIGINT_EVENT.load(Ordering::Acquire);
167+
if handle == 0 { None } else { Some(handle) }
168+
}

crates/vm/src/stdlib/signal.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,14 @@ pub(crate) mod _signal {
682682
pub extern "C" fn run_signal(signum: i32) {
683683
signal::TRIGGERS[signum as usize].store(true, Ordering::Relaxed);
684684
signal::set_triggered();
685+
#[cfg(windows)]
686+
if signum == libc::SIGINT
687+
&& let Some(handle) = signal::get_sigint_event()
688+
{
689+
unsafe {
690+
windows_sys::Win32::System::Threading::SetEvent(handle as _);
691+
}
692+
}
685693
let wakeup_fd = WAKEUP.load(Ordering::Relaxed);
686694
if wakeup_fd != INVALID_WAKEUP {
687695
let sigbyte = signum as u8;

0 commit comments

Comments
 (0)