Thread._wait_for_tstate_lock()永不返回 [英] Thread._wait_for_tstate_lock() never returns

查看:1128
本文介绍了Thread._wait_for_tstate_lock()永不返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,当我按Ctrl + C时,我的程序似乎在死锁中运行.我正在尝试捕获键盘中断并优雅地停止所有正在运行的线程,但是我还没有到那里.

My program appears to run in a deadlock sometimes when I hit Ctrl+C. I'm trying to catch the keyboard interrupt and gracefully stop all running threads, but I'm not quite there yet.

我正在使用concurrent.futures.ThreadPoolExecutor.为了找到死锁的位置,我使用了

I'm using a concurrent.futures.ThreadPoolExecutor. To find the location of the deadlock, I'm using the this receipe from ActiveState.

现在,这是完整的堆栈跟踪:

Now, here's the full stacktrace:

# ThreadID: 4856
File: "c:\users\niklas\appdata\local\programs\python\python36\lib\threading.py", line 884, in _bootstrap
  self._bootstrap_inner()
File: "c:\users\niklas\appdata\local\programs\python\python36\lib\threading.py", line 916, in _bootstrap_inner
  self.run()
File: "C:\Users\niklas\repos\nodepy\craftr\lib\utils\tracer.py", line 66, in run
  self.stacktraces()
File: "C:\Users\niklas\repos\nodepy\craftr\lib\utils\tracer.py", line 80, in stacktraces
  fout.write(stacktraces())
File: "C:\Users\niklas\repos\nodepy\craftr\lib\utils\tracer.py", line 28, in stacktraces
  for filename, lineno, name, line in traceback.extract_stack(stack):

# ThreadID: 6068
File: "c:\users\niklas\appdata\local\programs\python\python36\lib\runpy.py", line 193, in _run_module_as_main
  "__main__", mod_spec)
File: "c:\users\niklas\appdata\local\programs\python\python36\lib\runpy.py", line 85, in _run_code
  exec(code, run_globals)
File: "C:\Users\niklas\repos\nodepy\craftr\.nodepy_modules\.bin\craftr.exe\__main__.py", line 9, in <module>
  sys.exit(nodepy.main.main())
File: "c:\users\niklas\repos\nodepy\nodepy\nodepy\main.py", line 103, in main
  ctx.load_module(ctx.main_module, do_init=False)
File: "c:\users\niklas\repos\nodepy\nodepy\nodepy\context.py", line 253, in load_module
  module.load()
File: "c:\users\niklas\repos\nodepy\nodepy\nodepy\loader.py", line 43, in load
  exec(code, vars(self.namespace))
File: "C:\Users\niklas\repos\nodepy\craftr\lib\main.py", line 110, in <module>
  sys.exit(main())
File: "C:\Users\niklas\repos\nodepy\craftr\lib\main.py", line 106, in main
  return backend.build_main(backend_args, session, module)
File: "C:\Users\niklas\repos\nodepy\craftr\lib\build_backends\default.py", line 194, in build_main
  executor.run(actions)
File: "C:\Users\niklas\repos\nodepy\craftr\lib\build_backends\default.py", line 171, in run
  self.wait()
File: "C:\Users\niklas\repos\nodepy\craftr\lib\build_backends\default.py", line 137, in wait
  self.pool.shutdown(wait=True)
File: "c:\users\niklas\appdata\local\programs\python\python36\lib\concurrent\futures\thread.py", line 144, in shutdown
  t.join()
File: "c:\users\niklas\appdata\local\programs\python\python36\lib\threading.py", line 1056, in join
  self._wait_for_tstate_lock()
File: "c:\users\niklas\appdata\local\programs\python\python36\lib\threading.py", line 1072, in _wait_for_tstate_lock
  elif lock.acquire(block, timeout):

从此追溯中我无法理解.看来Thread._wait_for_tstate_lock()从不返回(我检查了多次,它总是挂在那一行).除了主线程(6068)和示踪线程(4856)之外,没有其他线程在运行.

I can't make sense from this traceback. It appears that Thread._wait_for_tstate_lock() never returns (I checked multiple times, it always hangs at that line). There is no thread running other than the main thread (6068) and the tracer thread (4856).

我不太了解threading.Thread的实现细节.是什么原因导致Thread._tstate_lock.acquire()无限期阻塞?

I don't quite understand the implementation details of threading.Thread. What could cause Thread._tstate_lock.acquire() to block indefinitely?

更新2017年11月7日-凌晨11:00(CEWT)

多次调用pool.shutdown()时似乎会发生这种情况...

This seems to happen when pool.shutdown() is called multiple times...

推荐答案

我不是100%肯定这是您在使用Windows时看到此消息的原因,但是我在Linux和Python 3.6上遇到了类似的问题场景.

I'm not 100% certain it's the reason you're seeing this as you are using Windows, but I encountered this on Linux with Python 3.6 in a similar scenario.

我在concurrent.futures.ThreadPoolExecutor上使用了.shutdown(),程序似乎挂断了. 有时,它最终会在30-60秒后退出.

I was using .shutdown() on a concurrent.futures.ThreadPoolExecutor and the program would seem to hang up. Sometimes it would finally exit after 30-60 seconds.

Ctrl-C总是导致回溯,表明它位于_wait_for_tstate_lock()

Ctrl-C always resulted in a traceback showing it was sitting in _wait_for_tstate_lock()

注意:在Python 3中,第二个Ctrl-C实际上退出了

我的问题是在提交的函数在循环中使用time.sleep()时发生的.

My problem occurred when the function submitted was using time.sleep() in a loop.

看着github上当前nodepy代码中的HtmlFileTracer实现,我看到了与我正在做的事情类似的情况(连续循环和睡眠一段时间,除非设置了某种标志)

Looking at the HtmlFileTracer implementation in current nodepy code on github I see a similar scenario to what I was doing, (continuously loop and sleep for an interval unless some sort of flag was set)

这篇关于Thread._wait_for_tstate_lock()永不返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆