IPython 按住任意键时崩溃

IPython crashing when I hold any key

当我通过 ssh 连接到一台特定的远程机器并启动一个 IPython 会话时,只要我按住一个键大约半秒钟(例如退格键),它就会崩溃。

错误输出粘贴在下面:

  File "/home/zach/local/anaconda3/bin/ipython", line 11, in <module>
    sys.exit(start_ipython())
  File "/home/zach/local/anaconda3/lib/python3.7/site-packages/IPython/__init__.py", line 125, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
  File "/home/zach/local/anaconda3/lib/python3.7/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/home/zach/local/anaconda3/lib/python3.7/site-packages/IPython/terminal/ipapp.py", line 356, in start
    self.shell.mainloop()
  File "/home/zach/local/anaconda3/lib/python3.7/site-packages/IPython/terminal/interactiveshell.py", line 498, in mainloop
    self.interact()
  File "/home/zach/local/anaconda3/lib/python3.7/site-packages/IPython/terminal/interactiveshell.py", line 481, in interact
    code = self.prompt_for_code()
  File "/home/zach/local/anaconda3/lib/python3.7/site-packages/IPython/terminal/interactiveshell.py", line 410, in prompt_for_code
    **self._extra_prompt_options())
  File "/home/zach/local/anaconda3/lib/python3.7/site-packages/prompt_toolkit/shortcuts/prompt.py", line 738, in prompt
    return run_sync()
  File "/home/zach/local/anaconda3/lib/python3.7/site-packages/prompt_toolkit/shortcuts/prompt.py", line 727, in run_sync
    return self.app.run(inputhook=self.inputhook, pre_run=pre_run2)
  File "/home/zach/local/anaconda3/lib/python3.7/site-packages/prompt_toolkit/application/application.py", line 709, in run
    return run()
  File "/home/zach/local/anaconda3/lib/python3.7/site-packages/prompt_toolkit/application/application.py", line 682, in run
    run_until_complete(f, inputhook=inputhook)
  File "/home/zach/local/anaconda3/lib/python3.7/site-packages/prompt_toolkit/eventloop/defaults.py", line 123, in run_until_complete
    return get_event_loop().run_until_complete(future, inputhook=inputhook)
  File "/home/zach/local/anaconda3/lib/python3.7/site-packages/prompt_toolkit/eventloop/posix.py", line 66, in run_until_complete
    self._run_once(inputhook)
  File "/home/zach/local/anaconda3/lib/python3.7/site-packages/prompt_toolkit/eventloop/posix.py", line 85, in _run_once
    self._inputhook_context.call_inputhook(ready, inputhook)
  File "/home/zach/local/anaconda3/lib/python3.7/site-packages/prompt_toolkit/eventloop/inputhook.py", line 78, in call_inputhook
    threading.Thread(target=thread).start()
  File "/home/zach/local/anaconda3/lib/python3.7/threading.py", line 847, in start
    _start_new_thread(self._bootstrap, ())
RuntimeError: can't start new thread

If you suspect this is an IPython bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@python.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
    %config Application.verbose_crash=True

它让我从这里进入一个中断的 bash 会话,我的击键不会出现在屏幕上,尽管我可以执行 ls、man、pwd、ipython 等命令。我只能通过按 Control D 然后再按 Control C 来终止 bash 会话。特别是,消息建议我按 %tb 等等是不可能的。

其他程序没有争用线程。查看错误,看起来事件循环可能正在尝试创建一个线程来处理每个按键,这最终导致无法分配更多线程。这似乎有点牵强,因为按住一个键肯定是预期的行为。

这似乎与问题 https://ipython.org/faq.html#ipython-crashes-under-os-x-when-using-the-arrow-keys.

可能相似

它本身似乎不是 Python 问题,因为如果我使用 Python 而不是 IPython,问题就会消失。我最初使用 Anaconda ipython,但在 /usr/bin/ipython 中切换到系统 ipython,结果相同。还尝试了全新安装的 Anaconda,但出现了同样的问题。还尝试在具有相同 OS 的另一台机器上全新安装 Anaconda,但问题没有发生。

我正在寻找在这个问题上取得进展的想法。任何想法表示赞赏,如果需要,我将 post 后续数据。

Python 3.7.3(默认,2019 年 3 月 27 日,22:11:17) IPython7.5.0 Ubuntu 18.04.2 LTS

现在已经修复了,但对我来说还是有些神秘。我一直跟踪堆栈跟踪,通过 CPython 一直到 pthreads 库调用。 pthreads 文档指出,该错误基本上只会在堆内存不足或已分配最大线程数时才会出现。我使用 ulimit 将每个进程的虚拟内存设置为无限制(大约 3 GB)。这解决了这个问题。

显然,虚拟内存限制干扰了分配线程的能力。显而易见的解决方案是需要更多内存,尽管很难相信需要超过 3 GB 来响应按键操作。另一种可能性是每个线程分配的数量是虚拟内存限制的函数——我记得 pthreads 文档中的类似内容,尽管它有点超出我的理解范围。