防止服务器宕机 : Socket operation on non-socket 错误不会导致服务器崩溃

This commit is contained in:
CakeCN
2026-01-08 00:31:48 +08:00
parent eba1e8052c
commit 2e1139d101

View File

@@ -48,8 +48,9 @@ try:
def custom_excepthook(exc_type, exc_value, exc_traceback): def custom_excepthook(exc_type, exc_value, exc_traceback):
"""全局异常钩子,捕获所有未处理的异常""" """全局异常钩子,捕获所有未处理的异常"""
if exc_type.__name__ in ['OSError', 'IOError'] and 'Bad file descriptor' in str(exc_value): error_str = str(exc_value)
# 处理 Bad file descriptor 错误,不导致服务器宕机 if exc_type.__name__ in ['OSError', 'IOError'] and any(msg in error_str for msg in ['Bad file descriptor', 'Socket operation on non-socket', 'Operation on closed file']):
# 处理各种socket错误不导致服务器宕机
if not is_duplicate_error(exc_value): if not is_duplicate_error(exc_value):
print(f"[Global Error Handler] {exc_type.__name__}: {exc_value}") print(f"[Global Error Handler] {exc_type.__name__}: {exc_value}")
print("[Traceback]") print("[Traceback]")