Skip to content
Snippets Groups Projects
  • akuesters's avatar
    Python wrapper: thread safe recipe (#882) · f30c4204
    akuesters authored and Benjamin Cumming's avatar Benjamin Cumming committed
    Ensure that errors in Python callbacks that are called from multithreaded C++ code propogate the correct Python error back to the parent Python callback site, and that no callbacks are called from other threads if an error has already ocurred.
    - protects each recipe callback with a mutex, stores python exception, catches and throws python exception if occured
    - methods calling recipe (in simulation and partition_load_balance) are protected as well by try catch, resets and rethrows python exception (if occured) or else throws C++ exception
    
    fixes #792
    f30c4204
error.cpp 337 B
#include <pybind11/pybind11.h>

#include "error.hpp"

namespace pyarb {

std::exception_ptr py_exception;
std::mutex py_callback_mutex;

void py_reset_and_throw() {
    if (py_exception) {
        std::exception_ptr copy = py_exception;
        py_exception = nullptr;
        std::rethrow_exception(copy);
    }
}

} // namespace pyarb