PyFinalize problem from multiple DLLs - a possible work-around

Mark Hammond mhammond at skippinet.com.au
Sun Oct 20 21:06:18 EDT 2002


Paul Miller wrote:
> Hi folks. We're embedding Python and have come across a conflict when
> multiple DLLs that link to Python are used. Here is the scenario:
> 
> 	Application
> 		Extension1.dll
> 			python22.dll
> 		Extension2.dll
> 			python22.dll
> 
> Although neither Extension knows about the other, they both link to
> python, which seems to be sharing its global state between the two. This
> causes problems when we do this:
> 
> 	Application Startup
> 		Extension1_Startup
> 			PyInitialize
> 		Extension2_Startup
> 			PyInitialize
> 
> 		Extension1_Shutdown
> 			PyFinalize
> 		Extension2_Shutdown
> 			PyFinalize	<--- oops

I don't think the extensions should be initializing or finalizing Python 
itself.  Simply have the application init and finalize Python - 
presumably the app must somehow bootstrap this Python import process, so 
it doesn't seem a burden.

On the other hand, while I don't really believe *you* need it, a little 
safety in the init/term functions would not go astray.

There is another unrelated bug caused by multiple PyInit/Final calls 
that _do_ actually init and term - eg, consider a server process that 
wants to "unload" Python when it has finished with it, but be able to 
"reload" it when it needs it again.  In this scenario, there are 
certainly memory leaks, and other design issues that are much harder to 
solve (specifically, unless extension modules are informed of the 
finalize/reinitialize, their own internal structures or objects may 
become invalid)

> 1. confirm this problem really exists
> 2. agree my solution probably fixes it (I have the code to proove it)
> 3. tell me where to submit the patch?

The process should be:
* Create a patch a python.sourceforge.net (use a patch rather than a bug)
* Possibly mail python-dev at python.org with the URL to the patch, and a 
description of your specific problem

Mark.




More information about the Python-list mailing list