Skip to content

Commit

Permalink
Guard against GIL acquisition post Python shutdown.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvicini committed Sep 12, 2024
1 parent 2e43390 commit b70dd3b
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/python/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <shared_mutex>

#include <mitsuba/core/bitmap.h>
#include <mitsuba/core/jit.h>
#include <mitsuba/core/logger.h>
Expand All @@ -6,6 +8,11 @@
#include <mitsuba/core/profiler.h>
#include <mitsuba/python/python.h>


// Flag & mutex to explicitly track if Python is still available.
static bool ready_flag = true;
static std::shared_mutex ready_mutex;

// core
MI_PY_DECLARE(atomic);
MI_PY_DECLARE(filesystem);
Expand Down Expand Up @@ -80,6 +87,13 @@ NB_MODULE(mitsuba_ext, m) {
Py_INCREF(o);
},
[](PyObject *o) noexcept {
std::shared_lock ready_guard(ready_mutex);
/* If the Python interpreter has already been shut down, we can no longer use
* its reference counting mechanism. This leaks memory on interpreter shutdown.
* However, this should only affect static, thread local objects, for which
* enforcing an orderly shutdown is difficult. */
if (!ready_flag)
return;
nb::gil_scoped_acquire guard;
Py_DECREF(o);
}
Expand Down Expand Up @@ -178,6 +192,14 @@ NB_MODULE(mitsuba_ext, m) {
Thread::static_shutdown();
Thread::static_initialization();
}

/* After this point, we can no longer guarantee that the Python
* interpreter is available on all threads. */
{
nb::gil_scoped_release g;
std::unique_lock ready_guard(ready_mutex);
ready_flag = false;
}
}));

/* Callback function cleanup static data strucutres, this should be called
Expand Down

0 comments on commit b70dd3b

Please sign in to comment.