0

I am using GMSH, a C-library with a Python API.

Sometimes, GMSH crashes and it forces my Python script to quit. However, I am doing more than one runs in a for loop and if one of them crashes, I would like to continue my for loop.

Obviously, the first thing I did was to use:

try:
    gmsh.model.mesh.generate()
except:
    print("error!")

However, it didn't work! If the error was natural, it did work, but if it was a run-time really unexpected error, my Python program always crashed.

How can i make sure that my Python program never quits no matter what happens in C or binary side?

  • As the linked answer says, pretty much the only way to do this reliably is to run whatever native code you need in a separate process and restart that process if it crashes. There are sort-of hacky things you could do to force your program to continue, but that's a terrible idea unless you _know for sure_ that the code crashed in a "harmless" way (and if you knew that, it should be possible to simply get it to not crash in the first place). – Cubic Aug 21 '23 at 16:00
  • *How can i make sure that my Python program never quits no matter what happens in C or binary side?* You **can't**. Signals like `SIGSEGV` occur because something tried to access memory it's not allowed to - which means something somewhere in memory is wrong or corrupt. Ignoring that problem isn't going to make it go away. Your Python process exists *on top of* that binary environment. When that binary environment is wrong or corrupt, nothing can be relied upon to work. – Andrew Henle Aug 21 '23 at 16:46
  • Identify the cause of the crash, understand and avoid it. The question is about lame workarounds, which qualifies it as an *XY Problem*. – CristiFati Aug 21 '23 at 18:58
  • Thank you for all the answers. I don't have any clue about how to compile a C code for its Python API, and I don't have any interest in fixing the source code. I just wanted to batch-run the same program with different inputs to find out which inputs will run without errors, that's all. It's not about ignoring the problem but detecting the problematic inputs in my case. It feels weird not to be able to test a broken program with a simple Python script. – Sina Atalay Aug 22 '23 at 08:22

0 Answers0