Skip to content

Commit c633376

Browse files
authored
Merge pull request #155 from robotpy/notifier-sleep
Sleep instead of busy-waiting when hanging the thread
2 parents dc6dfeb + b00ba9c commit c633376

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

subprojects/robotpy-wpilib/wpilib/src/rpy/Notifier.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@
2020
using namespace frc;
2121
using namespace pybind11::literals;
2222

23+
// Hang the thread since returning to the caller is going to crash when we try
24+
// to obtain the GIL again
25+
// - this is a daemon thread so it's fine?
26+
// - Python 3.14 does this too
27+
static void _hang_thread_if_finalizing() {
28+
if (Py_IsFinalizing()) {
29+
while (true) {
30+
std::this_thread::sleep_for(std::chrono::seconds(1000));
31+
}
32+
}
33+
}
34+
2335
PyNotifier::PyNotifier(std::function<void()> handler) {
2436
if (!handler) {
2537
throw FRC_MakeError(err::NullParameter, "handler");
@@ -67,21 +79,11 @@ PyNotifier::PyNotifier(std::function<void()> handler) {
6779
}
6880
}
6981
} catch (...) {
70-
if (Py_IsFinalizing()) {
71-
// Hang the thread since returning to the caller is going to crash
72-
// when we try to obtain the GIL again
73-
// - this is a daemon thread so it's fine?
74-
// - Python 3.14 does this too
75-
while(true) {}
76-
}
77-
82+
_hang_thread_if_finalizing();
7883
throw;
7984
}
8085

81-
if (Py_IsFinalizing()) {
82-
// see above
83-
while(true) {}
84-
}
86+
_hang_thread_if_finalizing();
8587
});
8688

8789
py::gil_scoped_acquire acquire;

0 commit comments

Comments
 (0)