This project has moved. For the latest updates, please go here.

Failed to attach debugger (take_gil: NULL tstate)

Sep 19, 2012 at 1:00 AM


I am unable to attach the debugger to a running python 3.2 process.  When I do the same with python 2.6 I can attach ok.

My setup:

- Run a script sitting in a sleep loop printing a message ( python 3.2.2150.1013 )
- PVTS 1.1.50508
- Attach to running process python.exe

- Script window shows:

Fatal Python error: take_gil: NULL tstate

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

- A popup then appears:

A debugger is attached to python.exe but not configured to debug this unhandled exception.  To debug this exception, detach the current debugger.
- In VS2010:

Failed to attach debugger: timeout while attaching

Any suggestions?


btw Thanks for PVTS I think it is a great addition to VS2010!



Sep 21, 2012 at 9:34 AM

I managed to find a workaround by removing the call to PyEval_InitThreads() in PyDebugAttachx86.dll (~ln900).  The target needs to be running Python code for a successful attach.  I have posted this in case there are others who wish to debug with 3.2.....


// if (!threadsInited()) { 
//    if(*curPythonThread == nullptr) {
//        // no threads are currently running, it is safe to initialize multi threading.
//        initThreads();
//        releaseLock();
// }else 
if(!addedPendingCall) { // someone holds the GIL but no one is actively adding any pending calls. We can pend our call // and initialize threads. addPendingCall(&AttachCallback, initThreads); addedPendingCall = true; } }



Sep 24, 2012 at 3:21 AM

I found a very simple workaround.  If multi-threading has been initialized on the main thread then the attach works fine.  This can be achieved by starting a dummy thread that starts and ends straight away...

from threading import Thread

For embedded applications you could call PyEval_InitThreads() after Py_Initialize().

Sep 24, 2012 at 5:14 PM

Thanks for the report and the proposed fix...  I haven't had a chance to look at it yet, but will soon.  I'm going to go ahead and open a bug based upon this so it doesn't get lost.

Sep 24, 2012 at 5:14 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.