【发布时间】:2019-12-01 21:33:13
【问题描述】:
我正在用 Ruby 编写一个交互式终端程序,它有时会运行 STDIN.read_nonblock(256) 以刷新来自用户的任何缓冲输入。
运行我的 Ruby 程序后,如果我在同一个终端和 shell 中运行 git add -p(另一个交互式程序),那么 git 会发生故障:它不会花任何时间等待用户输入,而是只显示它的所有提示立即退出。
这是一个 shell 会话,展示了我如何使用 Ubuntu 18.04、Ruby 2.6.3 和 git 2.17.1 重现此问题:
$ mkdir testrepo && cd testrepo && git init .
Initialized empty Git repository in /home/david/tmp/testrepo/.git/
$ touch foo.txt
$ git add foo.txt
$ echo hi > foo.txt
$ git add -p # works fine
diff --git a/foo.txt b/foo.txt
index e69de29..45b983b 100644
--- a/foo.txt
+++ b/foo.txt
@@ -0,0 +1 @@
+hi
Stage this hunk [y,n,q,a,d,e,?]? q
$ ruby -v -e 'STDIN.read_nonblock(256)'
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
Traceback (most recent call last):
2: from -e:1:in `<main>'
1: from <internal:prelude>:73:in `read_nonblock'
<internal:prelude>:73:in `__read_nonblock': Resource temporarily unavailable - read would block (IO::EAGAINWaitReadable)
$ git add -p # bad: exits before waiting for input
diff --git a/foo.txt b/foo.txt
index e69de29..45b983b 100644
--- a/foo.txt
+++ b/foo.txt
@@ -0,0 +1 @@
+hi
Stage this hunk [y,n,q,a,d,e,?]?
$ git --version
git version 2.17.1
我可以在我的 Ruby 程序中添加任何解决方法来防止这种情况发生吗? (我也有兴趣了解可能发生的事情。)
顺便说一句,我注意到一个混乱的终端可以通过运行bash 来启动一个新的shell,然后按Ctrl+D 退出那个新的shell。
【问题讨论】: