4

I'm using a third party python library with C extensions. There is one function I call all the time. When I call this function with some special argument (that should be valid) it segfaults.

Let's assume for the moment that I can't file a bug report (will take to long to resolve).

It there any way to handle such problem with Python?

I can provide the code but the question is so general it won't change much...

mnowotka
  • 16,430
  • 18
  • 88
  • 134
  • If you don't have access to the code or a debug build of the extension, there's really not much you can do yourself. What you should try to do is to create a [**Minimal**, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) which have the crash, and use it in a bug-report to the author of the extension. – Some programmer dude Dec 15 '16 at 12:53
  • You could check the parameter values before calling for values that are known to cause a seg fault. – Paul Ogilvie Dec 15 '16 at 12:56
  • @PaulOgilvie - you can't predict which value will cause the error unfortunately. I'm processing 20M strings and I found a single one causing this issue. – mnowotka Dec 15 '16 at 13:11
  • 1
    Until you know what input is triggering the error (and have a plausible reason why it would do so) -- why trust its output when it doesn't crash? Black boxes which explode for no apparent reason are not reliable. At the very least, you could perhaps do a check on a random sample of the 20M that didn't fail and confirm that it works properly for those. – John Coleman Dec 15 '16 at 13:49
  • See also: [c - python tracing a segmentation fault - Stack Overflow](https://stackoverflow.com/a/48303413/5267751) for the specific case that you want to debug the segmentation fault. – user202729 Jan 17 '21 at 11:14
  • Does this answer your question? [using "try" to avoiding a segmentation fault](https://stackoverflow.com/questions/27950296/using-try-to-avoiding-a-segmentation-fault) – mkrieger1 Dec 26 '21 at 16:11

2 Answers2

4

No. A segmentation fault means the C code has performed invalid memory access; there's no guarantee the one that failed was the only one that had gone awry. This means you can't trust anything writable within that process's virtual memory space, including the Python runtime and memory allocation structures. Furthermore, there's a decent chance the failing call wasn't the first to do the wrong thing; it likely did so because something else had already corrupted its state. This is why memory access bugs are hard to debug. Python has less errors of this nature because it relies on data types that do understand their bounds, though if you break out of that shell using e.g. ctypes it can be just as fragile and dangerous.

Yann Vernier
  • 15,414
  • 2
  • 28
  • 26
1

You can use signal module to create a handler for SIGSEGV. Then use os.kill to stop the offending process. But then you'd probably have to call the function from another process.

import os
import signal

def sig_handler(signum, frame):
    pass # handle the segmentation fault here
signal.signal(signal.SIGSEGV, sig_handler)

os.kill(os.getpid(), signal.SIGSEGV)

Also, you probably don't know how it should be handled properly anyway aside from just ignoring it (don't worry the segfault pretty much means that the OS prevented the memory access to the area that's not supposed to have been accessed).

Leon Carlo Valencia
  • 7,979
  • 1
  • 11
  • 12