Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graceful Handling of Segfault

I'm writing a program in Python that uses a closed source API in Linux. The API sometimes works, and sometimes segfaults - crashing my program also. However, if the program runs for 10 seconds, its past the point where it has a chance of segfaulting and runs forever (errors only happen in the beginning).

I think I need some type of script that:

starts my python program,

waits 10 seconds,

checks if python is still running

if it is running, the script should end itself without ending python

if python is NOT running, then repeat.

Is such a program possible? Will a segfault kill the script also?

like image 825
bkanuka Avatar asked Apr 19 '11 00:04

bkanuka


1 Answers

Yes, such a program is perfectly possible. You just have to run these two programs in separate processes - SEGFAULT only kills the process in which it has occured.

If you are under Linux, you can use either bash or python if you want. Just start the script that is failling in separate process. Code in python could look similar to this:

import subprocess
import time

start = time.clock()
ret = subprocess.call(['myprog', 'myarg0', ...])
end = time.clock()
if end - start > threshold:
    restart()

Also, maybe a return code from such process has some meaningful value when it has finished because of SEGFAULT.

like image 189
pajton Avatar answered Oct 03 '22 23:10

pajton