Skip to content

Commit

Permalink
Add work-around for issue #362 by invoking the python garbage collect…
Browse files Browse the repository at this point in the history
…or prior to finalizing the thread
  • Loading branch information
dagardner-nv committed Aug 17, 2023
1 parent 8906370 commit 50abe96
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions python/mrc/_pymrc/src/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ std::function<void()> create_gil_initializer()
std::function<void()> create_gil_finalizer()
{
bool python_finalizing = _Py_IsFinalizing() != 0;
LOG(INFO) << "create_gil_finalizer - finalizing: " << std::boolalpha << python_finalizing;

if (python_finalizing)
{
Expand All @@ -125,7 +124,6 @@ std::function<void()> create_gil_finalizer()
// Ensure we dont have the GIL here otherwise this deadlocks.
return [] {
bool python_finalizing = _Py_IsFinalizing() != 0;
LOG(INFO) << "gil_finalizer - finalizing: " << std::boolalpha << python_finalizing;

if (python_finalizing)
{
Expand All @@ -134,6 +132,10 @@ std::function<void()> create_gil_finalizer()
}

pybind11::gil_scoped_acquire gil;
// Work-around for #362, call gc.collect() prior to finalizing the thread state. This ensures that any
// collectable objects are done prior to the thread state being destroyed.
pybind11::object gc_mod = pybind11::module_::import("gc");
gc_mod.attr("collect")();

// Decrement the ref to destroy the GIL states
gil.dec_ref();
Expand Down

0 comments on commit 50abe96

Please sign in to comment.