我认为我找到了根本原因,可能是Python2.7中的一个bug。在捕获所有可捕获信号并忽略它们后,我可以跟踪一些更多的错误消息,并得到socket.error的提示。问题是,这种错误将首先触发SIGTERM(尝试杀死进程),然后写入STDERR。我的机制捕获所有STDOUT和STDERR的方式无法记录消息,因为主进程已被杀死。这在守护程序中是一个问题。这些是在进程终止之前日志中的最后几行。
2013-05-07 11:05:53,194 - STDERR - ERROR - Traceback (most recent call last):
2013-05-07 11:05:53,304 - STDERR - ERROR - File "/var/lib/netcam_epd/lib/python2.7/SocketServer.py", line 582, in process_request_thread
2013-05-07 11:05:53,415 - STDERR - ERROR - self.finish_request(request, client_address)
2013-05-07 11:05:53,489 - STDERR - ERROR - File "/var/lib/netcam_epd/lib/python2.7/SocketServer.py", line 323, in finish_request
2013-05-07 11:05:53,587 - STDERR - ERROR - self.RequestHandlerClass(request, client_address, self)
2013-05-07 11:05:53,684 - STDERR - ERROR - File "/var/lib/netcam_epd/lib/python2.7/SocketServer.py", line 640, in __init__
2013-05-07 11:05:53,835 - STDERR - ERROR - self.finish()
2013-05-07 11:05:53,887 - STDERR - ERROR - File "/var/lib/netcam_epd/lib/python2.7/SocketServer.py", line 693, in finish
2013-05-07 11:05:54,084 - STDERR - ERROR - self.wfile.flush()
2013-05-07 11:05:54,182 - STDERR - ERROR - File "/var/lib/netcam_epd/lib/python2.7/socket.py", line 303, in flush
2013-05-07 11:05:54,326 - STDERR - ERROR - self._sock.sendall(view[write_offset:write_offset+buffer_size])
2013-05-07 11:05:54,387 - STDERR - ERROR - error: [Errno 32] Broken pipe
显然,这是由于尝试向一个不可写的套接字进行写操作所造成的。我认为应该在库中处理得更好,返回适当的值,而不仅仅是抛出错误/异常,因为套接字可能在正常运行时随时关闭。
我将验证是否确实是根本原因。