0

I have a python script with multiple threading-launched threads in which several threads occasionally freeze (apparently simultaneously). In this script, I've registered a signal handler to dump stack traces from all the running threads. When it's frozen, no dumped stacks appear. What could be causing this?

A couple of possibilities that come to mind:

  • A thread is not releasing a mutex, freezing any other threads that attempt to acquire it. I would expect the signal handler to work in this case, however. Am I mistaken?
  • I log various things to stdout and stderrr, which are redirected with a bash command line to a log file. Perhaps precisely timed output from 2 threads could be blocking at OS level? This script has been running for months without problems, though there was a kernel update just recently (it's Ubuntu 12.04). If this is the case, the signal is not being ignored, just not producing any output...
  • I have a few global variables that are read and written by the freezing threads. I had thought that python 2.7 has a global thread lock to make this safe, and it's not been a problem before.
4

1 回答 1

3

Python 的signal模块专门在主解释器线程上运行信号处理程序。如果您的主线程挂起并且无法执行 Python 代码,您的信号处理程序将不会运行。信号将被触发和捕获,但如果线程不能执行 Python 代码,那么什么都不会发生。

避免这种情况的最好方法是确保您的主线程(Python 解释器启动时存在的第一个线程)不会死锁。这可能意味着确保初始化后该线程上不会发生任何重要的事情。

于 2013-09-03T01:20:17.133 回答