python / python/cpython

make pyrepl more robust against crashes

未關閉
#122,468 2 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

topic-repl type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

Bug report

Bug description:

This issue is motivated by a discussion with @serhiy-storchaka on #122456. Pyrepl/code.InteractiveInterpreter does not handle exceptions at various points, see the linked commend for an example.

Directly after writing this, I also got the following traceback that I now can't reproduce, by pressing ctrl-r then searching for a string, and then pressing ctrl-c at somehow the wrong point:

>>> Traceback (most recent call last):
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/simple_interact.py", line 147, in run_multiline_interactive_console
    statement = multiline_input(more_lines, ps1, ps2)
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/readline.py", line 385, in multiline_input
    return reader.readline()
           ~~~~~~~~~~~~~~~^^
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/reader.py", line 773, in readline
    self.handle1()
    ~~~~~~~~~~~~^^
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/reader.py", line 730, in handle1
    event = self.console.get_event(block)
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/unix_console.py", line 391, in get_event
    self.push_char(self.__read(1))
                   ~~~~~~~~~~~^^^
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/unix_console.py", line 203, in __read
    self.input_buffer = os.read(self.input_fd, 10000)
                        ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/__main__.py", line 6, in <module>
    __pyrepl_interactive_console()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/main.py", line 59, in interactive_console
    run_multiline_interactive_console(console)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/simple_interact.py", line 164, in run_multiline_interactive_console
    r.pop_input_trans()
    ~~~~~~~~~~~~~~~~~^^
  File "/home/cfbolz/projects/cpython/Lib/_pyrepl/reader.py", line 546, in pop_input_trans
    self.input_trans = self.input_trans_stack.pop()
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~^^
IndexError: pop from empty list

This crashed pyrepl. I think it would be better if we printed the exception, but then actually stayed in pyrepl, to make sure we don't lose the repl state which might have been annoying to get to.

Another way to crash pyrepl, but not the classic repl is to close sys.stderr and then raising an exception.

We could make pyrepl less crash-prone that by adding an except BaseException around the pyrepl loop in run_multiline_interactive_console.

Any opinions, @pablogsal, @ambv?

CPython versions tested on:

3.13, CPython main branch

Operating systems tested on:

Linux

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 Lib/_pyrepl/simple_interact.py 中的 run_multiline_interactive_console 開始,然後追蹤 traceback 中顯示的經過 reader.py 和 unix_console.py 的例外路徑。重現 Ctrl-C/搜尋失敗以及 sys.stderr 已關閉的情況。當 pyrepl 回報例外,且在不遺失 REPL 狀態的情況下仍可使用時,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
cli
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。