Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use GilScopedAcquire to create a new Python thread state when an actor is started.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Wed, 22 Jan 2020 20:54:30 +0000 (21:54 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Fri, 24 Jan 2020 21:00:20 +0000 (22:00 +0100)
The call to py_context.reset() is here to avoid deadlocks seen later with
SwappedContexts.

py::error_already_set::~error_already_set() uses py::gil_scoped_acquire which
does not play well with our GilScopedAcquire.

src/bindings/python/simgrid_python.cpp

index 81927f4..2451e47 100644 (file)
@@ -169,6 +169,7 @@ PYBIND11_MODULE(simgrid, m)
       .def("register_actor",
            [](Engine* e, const std::string& name, py::object fun_or_class) {
              e->register_actor(name, [fun_or_class](std::vector<std::string> args) {
+               GilScopedAcquire py_context;
                try {
                  /* Convert the std::vector into a py::tuple */
                  py::tuple params(args.size() - 1);
@@ -181,12 +182,14 @@ PYBIND11_MODULE(simgrid, m)
                  if (py::isinstance<py::function>(res))
                    res();
                } catch (const py::error_already_set& ex) {
-                 if (ex.matches(pyForcefulKillEx)) {
+                 bool ffk = ex.matches(pyForcefulKillEx);
+                 py_context.reset();
+                 if (ffk) {
                    XBT_VERB("Actor killed");
-                   /* Stop here that ForcefulKill exception which was meant to free the RAII stuff on the stack */
-                 } else {
-                   throw;
+                   /* Forward that ForcefulKill exception */
+                   simgrid::ForcefulKillException::do_throw();
                  }
+                 throw;
                }
              });
            },
@@ -297,19 +300,22 @@ PYBIND11_MODULE(simgrid, m)
       .def("create",
            [](py::str name, Host* host, py::object fun, py::args args) {
              return simgrid::s4u::Actor::create(name, host, [fun, args]() {
+               GilScopedAcquire py_context;
                try {
                  fun(*args);
                } catch (const py::error_already_set& ex) {
-                 if (ex.matches(pyForcefulKillEx)) {
+                 bool ffk = ex.matches(pyForcefulKillEx);
+                 py_context.reset();
+                 if (ffk) {
                    XBT_VERB("Actor killed");
-                   /* Stop here that ForcefulKill exception which was meant to free the RAII stuff on the stack */
-                 } else {
-                   throw;
+                   /* Forward that ForcefulKill exception */
+                   simgrid::ForcefulKillException::do_throw();
                  }
+                 throw;
                }
              });
            },
-           "Create an actor from a function or an object.")
+           py::call_guard<GilScopedRelease>(), "Create an actor from a function or an object.")
       .def_property("host", &Actor::get_host, &Actor::set_host, "The host on which this actor is located")
       .def_property_readonly("name", &Actor::get_cname, "The name of this actor.")
       .def_property_readonly("pid", &Actor::get_pid, "The PID (unique identifier) of this actor.")