This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Tony Roberts
Recipients Tony Roberts, paul.moore, steve.dower, tim.golden, zach.ware
Date 2018-06-18.10:45:00
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1529318701.15.0.56676864532.issue33895@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2018-06-18 10:45:01Tony Robertssetrecipients: + Tony Roberts, paul.moore, tim.golden, zach.ware, steve.dower
2018-06-18 10:45:01Tony Robertssetmessageid: <1529318701.15.0.56676864532.issue33895@psf.upfronthosting.co.za>
2018-06-18 10:45:01Tony Robertslinkissue33895 messages
2018-06-18 10:45:00Tony Robertscreate