Skip to content

Commit

Permalink
Strip back the test to just the minimum
Browse files Browse the repository at this point in the history
  • Loading branch information
dagardner-nv committed Aug 18, 2023
1 parent a0ec220 commit dbbab41
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 54 deletions.
16 changes: 2 additions & 14 deletions python/mrc/tests/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <glog/logging.h>
#include <pybind11/cast.h>
#include <pybind11/pybind11.h>
#include <pybind11/pytypes.h>

#include <memory>
#include <sstream>
Expand All @@ -32,18 +33,7 @@ namespace mrc::pytests {

namespace py = pybind11;

// Simple test class which acquires the GIL in it's destructor
struct ObjUsingGil
{
ObjUsingGil() = default;
~ObjUsingGil()
{
LOG(INFO) << "ObjUsingGil::~ObjUsingGil()";
py::gil_scoped_acquire gil;
LOG(INFO) << "ObjUsingGil::~ObjUsingGil()+gil";
}
};

// Simple test class which invokes Python's GC. Needed to repro #
struct ObjCallingGC
{
ObjCallingGC() = default;
Expand Down Expand Up @@ -76,8 +66,6 @@ PYBIND11_MODULE(utils, py_mod)
},
py::arg("msg") = "");

py::class_<ObjUsingGil>(py_mod, "ObjUsingGil").def(py::init<>());

py::class_<ObjCallingGC>(py_mod, "ObjCallingGC").def(py::init<>()).def_static("finalize", &ObjCallingGC::finalize);

py_mod.attr("__version__") = MRC_CONCAT_STR(mrc_VERSION_MAJOR << "." << mrc_VERSION_MINOR << "."
Expand Down
57 changes: 17 additions & 40 deletions python/tests/test_gil_tls.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,30 @@
import gc
import threading
import weakref

import mrc
from mrc.tests.utils import ObjCallingGC
from mrc.tests.utils import ObjUsingGil

TLS = threading.local()


class Holder:
def test_gc_called_in_thread_finalizer():
mrc.logging.log("Building pipeline")

def __init__(self, obj):
"""Intentionally create a cycle to delay obj's destruction"""
self.obj = obj
self.cycle = self
def source_gen():
mrc.logging.log("source_gen")
x = ObjCallingGC()
weakref.finalize(x, x.finalize)
TLS.x = x
yield x

def __del__(self):
mrc.logging.log("Holder.__del__")
self.obj = None
def init_seg(builder: mrc.Builder):
builder.make_source("souce_gen", source_gen)

pipe = mrc.Pipeline()
pipe.make_segment("seg1", init_seg)

class ThreadTest(threading.Thread):

def _create_obs(self):
TLS.h = Holder(ObjUsingGil())
TLS.ocg = ObjCallingGC()
weakref.finalize(TLS.ocg, TLS.ocg.finalize)

def run(self):
mrc.logging.log("Running thread")
self._create_obs()
mrc.logging.log("Thread complete")


def test_gil_tls():
t = ThreadTest()
t.start()
t.join()
mrc.logging.log("Thread joined")


def main():
mrc.logging.init_logging(__name__)
gc.disable()
gc.set_debug(gc.DEBUG_STATS)
test_gil_tls()
mrc.logging.log("Exiting main")


if __name__ == "__main__":
main()
options = mrc.Options()
executor = mrc.Executor(options)
executor.register_pipeline(pipe)
executor.start()
executor.join()

0 comments on commit dbbab41

Please sign in to comment.