diff options
author | John Spray <john.spray@redhat.com> | 2017-10-16 16:51:34 +0200 |
---|---|---|
committer | John Spray <john.spray@redhat.com> | 2017-11-01 13:21:42 +0100 |
commit | 29193a47e6cf8297d9b1ceecc7695f2c85434999 (patch) | |
tree | 8b8f8d382ffd24953682679b6b82e7bb1ff0361a /src/mgr/PyModuleRegistry.cc | |
parent | mgr: refactor PyOSDMap etc implementation (diff) | |
download | ceph-29193a47e6cf8297d9b1ceecc7695f2c85434999.tar.xz ceph-29193a47e6cf8297d9b1ceecc7695f2c85434999.zip |
mgr: update for SafeThreadState
A bunch of the previous commits were done
before this class existed, so updating in
one go instead of trying to edit history
in fine detail.
Signed-off-by: John Spray <john.spray@redhat.com>
Diffstat (limited to 'src/mgr/PyModuleRegistry.cc')
-rw-r--r-- | src/mgr/PyModuleRegistry.cc | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/src/mgr/PyModuleRegistry.cc b/src/mgr/PyModuleRegistry.cc index 3f4e60b86d2..bab057c6c28 100644 --- a/src/mgr/PyModuleRegistry.cc +++ b/src/mgr/PyModuleRegistry.cc @@ -155,6 +155,7 @@ int PyModuleRegistry::init(const MgrMap &map) // Drop the GIL and remember the main thread state (current // thread state becomes NULL) pMainThreadState = PyEval_SaveThread(); + assert(pMainThreadState != nullptr); std::list<std::string> failed_modules; @@ -191,14 +192,15 @@ int PyModule::load(PyThreadState *pMainThreadState) // Configure sub-interpreter and construct C++-generated python classes { - Gil gil(pMainThreadState); - - pMyThreadState = Py_NewInterpreter(); + SafeThreadState sts(pMainThreadState); + Gil gil(sts); - if (pMyThreadState == nullptr) { + auto thread_state = Py_NewInterpreter(); + if (thread_state == nullptr) { derr << "Failed to create python sub-interpreter for '" << module_name << '"' << dendl; return -EINVAL; } else { + pMyThreadState.set(thread_state); // Some python modules do not cope with an unpopulated argv, so lets // fake one. This step also picks up site-packages into sys.path. const char *argv[] = {"ceph-mgr"}; @@ -289,6 +291,15 @@ int PyModule::load(PyThreadState *pMainThreadState) return 0; } +PyModule::~PyModule() +{ + if (pMyThreadState.ts != nullptr) { + Gil gil(pMyThreadState, true); + Py_XDECREF(pClass); + Py_XDECREF(pStandbyClass); + } +} + void PyModuleRegistry::standby_start(MonClient *monc) { Mutex::Locker l(lock); |