Skip to content

Commit 00c3ee2

Browse files
caglarpirmeta-codesync[bot]
authored andcommitted
Stop OpenSfM feature bindings killing the interpreter on a C++ throw
Summary: An exception escaping these bindings reaches `std::terminate` and takes the whole process down. There is no Python unwinding, so a caller cannot record the failure: the work is simply lost rather than retried. That is currently the top source of stranded Mapillary clusters (T234633115). `mapillary_initial_worker` crashes ~224x/day and **210 of 224 (94%) have `__cxxabiv1::__terminate` -> `std::terminate()` in the stack** -- uncaught C++ exceptions, not memory corruption; the signal is a consequence of `terminate`, not its cause. 139 of them (62%) are in this module. Coredumps show the process dying at 12.7 GB average and 44.6 GB peak RSS, which is the shape an allocation failure takes, and `__clang_call_terminate` is exactly what clang emits when an exception escapes a `noexcept` frame. `guarded()` funnels everything -- including exception types pybind11 cannot translate -- into a `std::runtime_error` that pybind reliably turns into a Python exception. Argument types are deduced from the wrapped function, so reference parameters stay references and nothing is copied that was not copied before. For the two `gil_scoped_release` bindings the catch sits inside the released region, so the exception is already translatable by the time the guard's destructor re-acquires the GIL during unwinding. This does not stop the underlying failure. It converts an instant, invisible process kill into an ordinary Python exception that the caller can log, retry with a smaller batch, and report -- which is what closes the leak. `compute_vlad_descriptor` gives a directly reachable case: it throws `std::runtime_error("Zero VLAD centers or zero length VLAD words.")`, and its signature `VecXf(const MatXf&, const MatXf&)` matches one of the sampled crash stacks exactly. ___ Differential Revision: D118622746 fbshipit-source-id: c19955cebe38b1209913b127f7cae33e5bff0cd5
1 parent 1a8bfcb commit 00c3ee2

1 file changed

Lines changed: 45 additions & 5 deletions

File tree

‎opensfm/src/features/python/pybind.cc‎

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,40 @@
55
#include <pybind11/pybind11.h>
66
#include <pybind11/stl.h>
77

8+
#include <stdexcept>
9+
#include <string>
10+
#include <utility>
11+
12+
namespace {
13+
14+
// Funnel every C++ exception leaving a binding into a type pybind11 is
15+
// guaranteed to translate.
16+
//
17+
// Without this, an exception pybind cannot translate -- or one escaping a
18+
// noexcept frame -- reaches std::terminate and kills the interpreter outright.
19+
// The caller then dies with no Python unwinding, so it records no failure and
20+
// its work is silently lost rather than retried (T234633115: this is the top
21+
// remaining source of stranded Mapillary clusters, ~224 crashes/day, 94% of
22+
// them std::terminate, at 12-44 GB RSS). A Python exception instead lets the
23+
// caller log the failure, shrink its batch, and retry.
24+
//
25+
// Args are deduced from the wrapped function, so reference parameters stay
26+
// references and nothing is copied that was not copied before.
27+
template <typename R, typename... Args>
28+
auto guarded(const char* name, R (*fn)(Args...)) {
29+
return [name, fn](Args... args) -> R {
30+
try {
31+
return fn(std::forward<Args>(args)...);
32+
} catch (const std::exception& e) {
33+
throw std::runtime_error(std::string(name) + ": " + e.what());
34+
} catch (...) {
35+
throw std::runtime_error(std::string(name) + ": unknown C++ exception");
36+
}
37+
};
38+
}
39+
40+
} // namespace
41+
842
PYBIND11_MODULE(pyfeatures, m) {
943
py::enum_<DESCRIPTOR_TYPE>(m, "AkazeDescriptorType")
1044
.value("SURF_UPRIGHT", SURF_UPRIGHT)
@@ -50,15 +84,21 @@ PYBIND11_MODULE(pyfeatures, m) {
5084
.def_readwrite("save_keypoints", &AKAZEOptions::save_keypoints)
5185
.def_readwrite("verbosity", &AKAZEOptions::verbosity);
5286

53-
m.def("akaze", features::akaze);
87+
m.def("akaze", guarded("akaze", features::akaze));
5488

55-
m.def("hahog", features::hahog, py::arg("image"),
89+
m.def("hahog", guarded("hahog", features::hahog), py::arg("image"),
5690
py::arg("peak_threshold") = 0.003, py::arg("edge_threshold") = 10,
5791
py::arg("target_num_features") = 0);
5892

59-
m.def("match_using_words", features::match_using_words);
60-
m.def("compute_vlad_descriptor", features::compute_vlad_descriptor,
93+
m.def("match_using_words",
94+
guarded("match_using_words", features::match_using_words));
95+
// The guard is inside the released-GIL region, so the exception is already a
96+
// translatable type by the time gil_scoped_release's destructor re-acquires
97+
// the GIL during unwinding.
98+
m.def("compute_vlad_descriptor",
99+
guarded("compute_vlad_descriptor", features::compute_vlad_descriptor),
61100
py::call_guard<py::gil_scoped_release>());
62-
m.def("compute_vlad_distances", features::compute_vlad_distances,
101+
m.def("compute_vlad_distances",
102+
guarded("compute_vlad_distances", features::compute_vlad_distances),
63103
py::call_guard<py::gil_scoped_release>());
64104
}

0 commit comments

Comments
 (0)