New submission from Tony Roberts <t...@pyxll.com>:

In dynload_win.c LoadLibraryExW is called with the GIL held.

This can cause a deadlock in an uncommon case where the GIL also needs to be 
acquired when another thread is being detached.

Both LoadLibrary and FreeLibrary acquire the Windows loader-lock. If 
FreeLibrary is called on a module that acquires the GIL when detaching, a 
dead-lock occurs when another thread with the GIL held blocks on the 
loader-lock by calling LoadLibrary.

This can happen when Python is embedded in another application via an 
extension, and where that application may create threads that call into that 
extension that results in Python code being called. Because the application is 
creating the thread, the extension that's embedding Python doesn't know when 
the thread will terminate. The first time the extension is called from that 
thread and it needs to run some Python code it has to create a new thread 
state, and when the thread terminates that thread state should be destroyed. In 
other situations the thread state would be destroyed as part of cleaning up the 
thread, but here the extension does not know when the thread terminates and so 
must do it on thread detach in DllMain. Attempting to destroy the thread state 
this way requires acquiring the GIL, which can cause the deadlock described 
above.

The safest way to avoid this deadlock (without potentially leaking thread 
states) would be to release the GIL before calling LoadLibrary.

----------
components: Windows
messages: 319876
nosy: Tony Roberts, paul.moore, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
status: open
title: LoadLibraryExW called with GIL held can cause deadlock
versions: Python 2.7, Python 3.4, Python 3.5, Python 3.6, Python 3.7, Python 3.8

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33895>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to