Skip to content

Commit

Permalink
Add GIL lock to pprotect python code.
Browse files Browse the repository at this point in the history
This is part 2 of fix to bug opencog/atomspace#671
  • Loading branch information
linas committed Feb 27, 2016
1 parent e3f2ea2 commit 6c7a0a9
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions opencog/cogserver/modules/python/PythonModule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,19 @@ static bool already_loaded = false;
PythonModule::~PythonModule()
{
logger().info("[PythonModule] destructor");

PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

unregisterAgentsAndRequests();
do_load_py_unregister();

for (PythonAgentFactory* af : _agentFactories) delete af;
for (PythonRequestFactory* rf : _requestFactories) delete rf;

already_loaded = false;

PyGILState_Release(gstate);
}

bool PythonModule::unregisterAgentsAndRequests()
Expand Down Expand Up @@ -113,11 +119,15 @@ void PythonModule::init()
"Python not initialized, missing global_python_init()");
}

PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

// Many python libraries (e.g. ROS) expect sys.argv to be set. So,
// avoid the error print, and let them know who we are.
static const char *argv0 = "cogserver";
PySys_SetArgv(1, (char **) &argv0);


// NOTE: Even though the Cython docs do not say that you need to call this
// more than once, you need to call the import functions in each separate
// shared library that accesses Cython defined api. If you don't then you
Expand All @@ -138,6 +148,8 @@ void PythonModule::init()

if (config().has("PYTHON_PRELOAD")) preloadModules();
do_load_py_register();

PyGILState_Release(gstate);
}

bool PythonModule::preloadModules()
Expand All @@ -156,7 +168,8 @@ bool PythonModule::preloadModules()
return true;
}

/// do_load_py -- load python code, given a file name. (Implements the loadpy command)
/// do_load_py -- load python code, given a file name. (Implements the
/// loadpy command)
///
/// It is expected that the file contains a python module. The module
/// should implement either a mind-agent, or it should contain a 'request'
Expand Down Expand Up @@ -227,7 +240,8 @@ std::string PythonModule::do_load_py(Request *dummy, std::list<std::string> args
bool is_shell = thingsInModule.req_is_shell[i];

// CogServer commands in Python
// Register request with cogserver using dotted name: module.RequestName
// Register request with cogserver using dotted name:
// module.RequestName
std::string dottedName = moduleName + "." + s;
// register the agent with a custom factory that knows how to
PythonRequestFactory* fact =
Expand Down

0 comments on commit 6c7a0a9

Please sign in to comment.