Skip to content

Commit f0c427b

Browse files
committed
Correct exitcode when running through subprocess.run
1 parent acbc517 commit f0c427b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

vm/src/vm/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ use crate::{
3636
AsObject, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
3737
};
3838
use crossbeam_utils::atomic::AtomicCell;
39+
#[cfg(unix)]
40+
use nix::{
41+
sys::signal::{kill, sigaction, SaFlags, SigAction, SigSet, Signal::SIGINT},
42+
unistd::getpid,
43+
};
3944
use std::sync::atomic::AtomicBool;
4045
use std::{
4146
borrow::Cow,
@@ -763,6 +768,37 @@ impl VirtualMachine {
763768
self.print_exception(exc);
764769
#[cfg(unix)]
765770
{
771+
unsafe {
772+
if sigaction(
773+
SIGINT,
774+
&SigAction::new(
775+
nix::sys::signal::SigHandler::SigDfl,
776+
SaFlags::SA_ONSTACK,
777+
SigSet::empty(),
778+
),
779+
)
780+
.is_ok()
781+
{
782+
// FIXME: flush sys.stdout and sys.stderr before killing process.
783+
if let Ok(stdout) = stdlib::sys::get_stdout(self) {
784+
let _ = self.call_method(
785+
&stdout,
786+
identifier!(self, flush).as_str(),
787+
(),
788+
);
789+
}
790+
if let Ok(stderr) = stdlib::sys::get_stderr(self) {
791+
let _ = self.call_method(
792+
&stderr,
793+
identifier!(self, flush).as_str(),
794+
(),
795+
);
796+
}
797+
798+
kill(getpid(), SIGINT).expect("Expect to be killed.");
799+
}
800+
}
801+
766802
(libc::SIGINT as u8) + 128u8
767803
}
768804
#[cfg(not(unix))]

0 commit comments

Comments
 (0)