Skip to content

Commit

Permalink
check for KeyboardInterrupt exception
Browse files Browse the repository at this point in the history
[ci skip-build-wheels]
  • Loading branch information
gshuflin committed Sep 14, 2020
1 parent 0145f05 commit 42d03b9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/rust/engine/src/externs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,17 @@ pub fn call_method(value: &Value, method: &str, args: &[Value]) -> Result<Value,
.map_err(|py_err| Failure::from_py_err(gil.python(), py_err))
}

pub fn call(func: &Value, args: &[Value]) -> Result<Value, Failure> {
pub fn call_function(func: &Value, args: &[Value]) -> Result<PyObject, PyErr> {
let arg_handles: Vec<PyObject> = args.iter().map(|v| v.clone().into()).collect();
let gil = Python::acquire_gil();
let args_tuple = PyTuple::new(gil.python(), &arg_handles);
func
.call(gil.python(), args_tuple, None)
func.call(gil.python(), args_tuple, None)
}

pub fn call(func: &Value, args: &[Value]) -> Result<Value, Failure> {
let output = call_function(func, args);
let gil = Python::acquire_gil();
output
.map(Value::from)
.map_err(|py_err| Failure::from_py_err(gil.python(), py_err))
}
Expand Down
27 changes: 21 additions & 6 deletions src/rust/engine/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::core::{Failure, Params, TypeId, Value};
use crate::externs;
use crate::nodes::{NodeKey, Select, Visualizer};

use cpython::Python;
use graph::{InvalidationResult, LastObserved};
use log::{debug, info, warn};
use parking_lot::Mutex;
Expand Down Expand Up @@ -522,18 +523,32 @@ impl Scheduler {
}

fn maybe_break_execution_loop(python_signal_fn: &Value) -> Option<ExecutionTermination> {
match externs::call(&python_signal_fn, &[]) {
match externs::call_function(&python_signal_fn, &[]) {
Ok(value) => {
if externs::is_truthy(&*value) {
if externs::is_truthy(&value) {
Some(ExecutionTermination::KeyboardInterrupt)
} else {
None
}
}
Err(e) => Some(ExecutionTermination::Fatal(format!(
"Error when checking Python signal state: {}",
e
))),
Err(mut e) => {
let gil = Python::acquire_gil();
let py = gil.python();
if e
.instance(py)
.cast_as::<cpython::exc::KeyboardInterrupt>(py)
.is_ok()
{
Some(ExecutionTermination::KeyboardInterrupt)
} else {
let failure = Failure::from_py_err(py, e);
std::mem::drop(gil);
Some(ExecutionTermination::Fatal(format!(
"Error when checking Python signal state: {}",
failure
)))
}
}
}
}

Expand Down

0 comments on commit 42d03b9

Please sign in to comment.