// Swapped contexts are broken, starting from pybind11 v2.8.0. Use thread contexts by default.
simgrid::s4u::Engine::set_config("contexts/factory:thread");
+ // Internal exception used to kill actors and sweep the RAII chimney (free objects living on the stack)
+ static py::object pyForcefulKillEx(py::register_exception<simgrid::ForcefulKillException>(m, "ActorKilled"));
+
py::register_exception<simgrid::NetworkFailureException>(m, "NetworkFailureException");
py::register_exception<simgrid::TimeoutException>(m, "TimeoutException");
py::register_exception<simgrid::HostFailureException>(m, "HostFailureException");
[](py::object cb) {
py::function fun = py::reinterpret_borrow<py::function>(cb);
fun.inc_ref(); // FIXME: why is this needed for tests like actor-kill and actor-lifetime?
- simgrid::s4u::this_actor::on_exit([fun](bool /*failed*/) {
+ simgrid::s4u::this_actor::on_exit([fun](bool failed) {
py::gil_scoped_acquire py_context; // need a new context for callback
try {
- fun();
+ fun(failed);
} catch (const py::error_already_set& e) {
xbt_die("Error while executing the on_exit lambda: %s", e.what());
}
});
},
- py::call_guard<py::gil_scoped_release>(), "")
+ py::call_guard<py::gil_scoped_release>(),
+ "Define a lambda to be called when the actor ends. It takes a bool parameter indicating whether the actor "
+ "was killed. If False, the actor finished peacefully.")
.def("get_pid", &simgrid::s4u::this_actor::get_pid, "Retrieves PID of the current actor")
.def("get_ppid", &simgrid::s4u::this_actor::get_ppid,
"Retrieves PPID of the current actor (i.e., the PID of its parent).");
if (py::isinstance<py::function>(res))
res();
} catch (const py::error_already_set& ex) {
- XBT_VERB("Actor killed");
- if (ex.matches(PyExc_RuntimeError)) {
- simgrid::ForcefulKillException::do_throw();
- throw;
+ if (ex.matches(pyForcefulKillEx)) {
+ XBT_VERB("Actor killed");
+ simgrid::ForcefulKillException::do_throw(); // Forward that ForcefulKill exception
}
+ throw;
}
});
},
try {
fun(*args);
} catch (const py::error_already_set& ex) {
- XBT_VERB("Actor killed");
- if (ex.matches(PyExc_RuntimeError)) {
- simgrid::ForcefulKillException::do_throw();
- throw;
+ if (ex.matches(pyForcefulKillEx)) {
+ XBT_VERB("Actor killed");
+ simgrid::ForcefulKillException::do_throw(); // Forward that ForcefulKill exception
}
+ throw;
}
});
},