Search code examples
c++exceptionseh

Structured exception handling with a multi-threaded server


This article gives a good overview on why structured exception handling is bad. Is there a way to get the robustness of stopping your server from crashing, while getting past the problems mentioned in the article?

I have a server software that runs about 400 connected users concurrently. But if there is a crash all 400 users are affected. We added structured exception handling and enjoyed the results for a while, but eventually had to remove it because of some crashes causing the whole server to hang (which is worse than just having it crash and restart itself).

So we have this:

  • With SEH: only 1 user of the 400 get a problem for most crashes
  • Without SEH: If any user gets a crash, all 400 are affected.
  • But sometimes with SEH: Server hangs, all 400 are affected and future users that try to connect.

Solution

  • Break your program up into worker processes and a single server process. The server process will handle initial requests and then hand them off the the worker processes. If a worker process crashes, only the users on that worker are affected. Don't use SEH for general exception handling - as you have found out, it can and will leave you wide open to deadlocks, and you can still crash anyway.