简体   繁体   English

使用多线程服务器进行结构化异常处理

[英]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. 我有一个服务器软件,可同时运行大约400个连接用户。 But if there is a crash all 400 users are affected. 但如果发生崩溃,则所有400名用户都会受到影响。 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 使用SEH:400中只有1个用户遇到大多数崩溃的问题
  • Without SEH: If any user gets a crash, all 400 are affected. 没有SEH:如果任何用户崩溃,所有400都会受到影响。
  • But sometimes with SEH: Server hangs, all 400 are affected and future users that try to connect. 但有时使用SEH:服务器挂起,所有400个都会受到影响,未来的用户会尝试连接。

Using SEH because your program crashes randomly is a bad idea. 使用SEH因为你的程序随机崩溃是一个坏主意。 It's not magic pixie dust that you can sprinkle on your program to make it stop crashing. 你可以在你的程序上撒上魔法小精灵粉尘,让它停止崩溃。 Tracking down and fixing the bugs that cause the crashes is the right solution. 追踪并修复导致崩溃的错误是正确的解决方案。

Using SEH when you really need to handle a structured exception is fine. 当你真的需要处理结构化异常时使用SEH就可以了。 Larry Osterman made a followup post explaining what situations require SEH: memory mapped files, RPC, and security boundary transitions . 拉里·奥斯特曼(Larry Osterman)做了一个后续帖子,解释了什么情况需要SEH:内存映射文件,RPC和安全边界转换

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. 不要将SEH用于一般异常处理 - 正如您所发现的那样,它可以并且将使您对死锁完全开放,并且您仍然可以崩溃。

Fix the bugs in your program ? 修复程序中的错误? ;) ;)

Personally I'd keep the SEH handlers in, have them dump out a call stack of where the access violation or whatever happened and fix the problems. 就个人而言,我会保留SEH处理程序,让他们转出一个调用堆栈,其中包含访问冲突或发生的任何事情,并解决问题。 The 'sometimes the server hangs' problem is probably due to deadlocks caused by the thread that had the SEH exception keeping something locked and so is unlikely to be related to the fact that you're using SEH itself. “有时服务器挂起”的问题可能是由于线程导致的死锁导致SEH异常保持锁定状态,因此不太可能与您使用SEH本身有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM