Skip to content

Throwing a c++ exception in a rvalue causes an abort #1878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
madebr opened this issue Aug 12, 2019 · 3 comments
Closed

Throwing a c++ exception in a rvalue causes an abort #1878

madebr opened this issue Aug 12, 2019 · 3 comments
Labels

Comments

@madebr
Copy link

madebr commented Aug 12, 2019

Issue description

When throwing a c++ exception inside a method of an object that is an rvalue (=has no name associated) AND printing in the destructor, then the program aborts.

Reproducible example code

pybind11 c++ python module definition:

#include <pybind11/pybind11.h>
#include <exception>
#include <iostream>

namespace py = pybind11;

class callback {
public:
    callback() {}
    ~callback() {
        std::cout << "c++: callback::~callback()" << std::endl;
        py::print("Here does the crash happen");
   
    }

    void on_fatal() {
        std::cout << "c++: callback::on_fatal" << std::endl;
        throw std::runtime_error("whatever");
    }
};

PYBIND11_MODULE(pymodule, m) {
    py::class_<callback> class_callback(m, "Callback");
    class_callback
        .def(py::init<>())
        .def("on_fatal", &callback::on_fatal);
}

When using the python module as follows, the python interpreter aborts:

import pymodule
pymodule.Callback().on_fatal()

The output is:

terminate called after throwing an instance of 'pybind11::error_already_set'
  what():  SystemError: <built-in method join of str object at 0x7fbe724d4bf0> returned a result with an error set
Aborted (core dumped)

By assigning the just created object to a variable, the abort disappears:

import pymodule
cb = pymodule.Callback()
cb.on_fatal()
@arpit15
Copy link

arpit15 commented Aug 13, 2019

@madebr
Copy link
Author

madebr commented Aug 13, 2019

Thanks for the pointer. The error happens here:

auto line = sep.attr("join")(strings);

@YannickJadoul
Copy link
Collaborator

Confirmed to still be an issue in 2.5.0. The issue is that somehow, a Python error is already set, yet the Callback object is collected and accesses Python again (without throwing an error, this time), confusing Python/pybind11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants