Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent multiple initialization of dynamic library

I am working on Python version 2.7. I have a module extension for Python written in C. The module initialization function PyMODINIT_FUNC initmymodule contains some code for initializing OpenSSL library. My module built as shared library and loading via imp.load_dynamic

This module may loading many times and I can't control it. Django and python doing that. And when it loading twice then OPENSSL_config function calling twice too. And it leading to process crash.

I can't control it from C-code, I can't control it from Python-code.

Here look at the docs http://docs.python.org/2.7/library/imp.html It says:

imp.load_dynamic Load and initialize a module implemented as a dynamically loadable shared library and return its module object. If the module was already initialized, it will be initialized again.

Nice.

I found that the similar problem was solved in Python version 3.4 http://hg.python.org/cpython/file/ad51ed93377c/Python/import.c#l459

Modules which do support multiple initialization set their m_size field to a non-negative number (indicating the size of the module-specific state). They are still recorded in the extensions dictionary, to avoid loading shared libraries twice.

But what shall I do in Python 2.7?

like image 559
checkraise Avatar asked May 20 '13 15:05

checkraise


1 Answers

Maybe to do workaround by registering own custom import hook where you could control the case which causes you problem (prevent double initialization). Some references for writing custom import hooks:

  • Python import hooks article
  • PEP-302 New Import Hooks - python 2.3+
  • create and register custom import/reload functions - example of implementation in project lazy_reload

This is hackish solution, so I suggest extra caution if this is to be used in production systems.

like image 135
Robert Lujo Avatar answered Oct 23 '22 01:10

Robert Lujo