Debugging Access Violations under Windows

We frequently develop C/C++ code which is tested using python/ctypes on windows. Of course, sometimes it happens that something goes wrong and we get an access violation.

Debugging this is usually easy, just attach a C debugger to the python process and wait until the seg fault happens. However, python catches access violation and tries to be nice to the average user showing stack traces and saves the python process from crashing, if possible.

While this is behaviour is nice for most use cases, the debugging is hardened. There are debuggers out there, which do not have the option to pause on handled access violation. AFAIK the popular Visual Studio Code is one of them, in contrast to standard Visual Studio where you have the option to pause on handled access violations.

So I’m wondering: Are there any options to stop python from catching the access violation and let the python process crash? Or am I missing something obvious?

I think you’re referring to the structured exception handling (SEH) handler that the release build of ctypes uses when calling foreign functions on Windows. It handles a native OS exception that’s raised in an FFI call by raising a Python OSError exception. This prevents debuggers from seeing the second-chance pass of native exception handling. (An attached debugger will still see the first-chance pass that allows a debugger to handle an OS exception before the application sees it.)

The debug build of ctypes (e.g. running a script via “python_d.exe”), on the other hand, does not use an SEH handler. Thus an attached debugger or post-mortem debugger will see the second-chance pass of the native Windows exception handling.

Thanks for clarification. Indeed I didn’t know about the different behaviour of the ctypes debug build.

For our use case, it would be cool to have an option for the behaviour at runtime (be it an environment variable, a ctypes API function, or whatever), just because of the difficulties involved with the debug build. After all, we don’t really want to debug python…

But I understand if this is considered as an edge case.