Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Pass the boolean parameter to the on_exit python binding
[simgrid.git] / src / bindings / python / simgrid_python.cpp
index 9b684a5..206f1d0 100644 (file)
@@ -84,6 +84,9 @@ PYBIND11_MODULE(simgrid, m)
   // 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");
@@ -123,16 +126,18 @@ PYBIND11_MODULE(simgrid, m)
           [](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).");
@@ -218,11 +223,11 @@ PYBIND11_MODULE(simgrid, m)
                 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;
               }
             });
           },
@@ -744,11 +749,11 @@ PYBIND11_MODULE(simgrid, m)
               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;
               }
             });
           },