stdin.pause() does not stop the TTY read before spawnSync(); child loses keystrokes on Windows
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 55/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- javascript, node.js
調査の方向性
Read lib/internal/bootstrap/switches/is_main_thread.js around getStdin() and the pause handler, then inspect the linked history for the deferred onpause() behavior. Reproduce the Windows case from the issue using spawnSync() with inherited stdin. Done means pausing stdin stops the parent TTY read before the synchronous child starts, while the existing resume(); pause(); behavior still works.
索引モデルが issue の本文から書いたものです。
説明
Version
v24.22.3
Platform
Microsoft Windows 11 Pro 10.0.26300 x64
(Windows Terminal 1.25.3131.0, PowerShell 7.7.0)
Subsystem
process, tty
What steps will reproduce the bug?
const { spawnSync } = require('child_process');
process.stdin.setRawMode(true);
process.stdin.resume();
console.log('press any key to launch the editor');
process.stdin.once('data', () => {
process.stdin.setRawMode(false);
process.stdin.pause();
spawnSync(process.env.EDITOR || 'vim', ['--clean'], { stdio: 'inherit' });
process.exit(0);
});
Run it in a Windows console, press a key, then type in vim.
A version without vim, where the child prints every chunk it receives
(type abcdef, then q):
const { spawnSync } = require('child_process');
const child = `process.stdin.setRawMode(true);
process.stdin.on('data', (d) => {
console.log('child got', JSON.stringify(d.toString()));
if (d.includes('q')) process.exit(0);
});`;
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.once('data', () => {
process.stdin.setRawMode(false);
process.stdin.pause();
spawnSync(process.execPath, ['-e', child], { stdio: 'inherit' });
process.exit(0);
});
How often does it reproduce? Is there a required condition?
Always on Windows with the repro above, when setRawMode(false) and
pause() are called (in either order) and then the event loop is blocked
(spawnSync() with inherited stdin) in the same tick. A variant without
setRawMode() reproduces intermittently (see below).
It does not reproduce if the blocking call is deferred by at least one tick
(process.nextTick, queueMicrotask, setImmediate, setTimeout), if
async spawn() is used, or if the native read is stopped manually
(process.stdin._handle.readStop()) before spawnSync().
What is the expected behavior? Why is that the expected behavior?
After process.stdin.pause() returns, the parent should no longer read from
the terminal, so the child that inherits it receives all keystrokes.
What do you see instead?
Right after vim starts, it does not respond to keystrokes; after a while it
starts working. With the child that prints its input, the first keystrokes
are not echoed and never reach the child. After Enter is pressed, the child
receives input normally.
Additional information
process.stdin.pause() emits 'pause', but the TTY read is only stopped in
a process.nextTick() callback (onpause() in
lib/internal/bootstrap/switches/is_main_thread.js, deferred since
5d704596062 / #19377). spawnSync() blocks the event loop before that tick
runs, so the parent's native read remains active while the child runs:
process.stdin.pause();
process.stdin._handle.reading; // still true
On Windows this is visible because of how libuv reads a TTY in line mode
(deps/uv/src/win/tty.c):
uv_tty_set_mode()stops and restarts reading if the handle is reading,
sosetRawMode(false)while reading starts a line-mode read.- A line-mode read runs
ReadConsoleW()on a thread-pool thread
(uv_tty_line_read_thread). That thread consumes console input
regardless of whether the event loop is running, so it competes with the
child for keystrokes until the read completes. - A raw-mode read only waits for input on another thread
(RegisterWaitForSingleObject) and reads it on the loop thread, so it
does not take input while the loop is blocked. - After a line-mode read completes, libuv queues the next
ReadConsoleW()
only afterread_cbreturns (uv_process_tty_read_line_req). So
pause()+spawnSync()inside a'data'handler is fine in plain line
mode. In the repro,setRawMode(false)inside the handler restarts reading
immediately, so theReadConsoleW()thread is already running when
spawnSync()is called.
Observed variants (Windows):
| Variant | Result |
|---|---|
raw → setRawMode(false) + pause() + spawnSync() in 'data' |
NG |
raw kept, pause() + spawnSync() in 'data' |
OK |
line mode only, pause() + spawnSync() in 'data' |
OK |
line mode only, pause() + spawnSync() from a timer |
Intermittent (sometimes NG) |
The last variant shows that setRawMode(false) is not required: any
pending line-mode read that is still active when spawnSync() starts can
take the child's input. It is intermittent presumably because the parent's
ReadConsoleW() and the child's own console reads race for the same input
buffer.
To show that the parent takes the input, the timer variant resumes stdin in
the same tick after spawnSync() returns (before the deferred onpause()
discards the completed read) and prints what the parent received. In a
failing run, the first q was not echoed and did not reach the child; after
Enter, the child received the next q:
> node repro-keys.js line-timer
variant=line-timer: launching the child in 1s (do not type yet)
child: type keys, q to quit
child got "q"
child exited
parent got "q\r"
parent done
The keystrokes typed while the child was running (q, Enter) were consumed
by the parent's pending line-mode ReadConsoleW().
Proposed fix
Call onpause() synchronously in the 'pause' handler, in addition to the
existing deferred call, in lib/internal/bootstrap/switches/is_main_thread.js
(getStdin()):
stdin.on('pause', () => {
+ onpause();
process.nextTick(onpause);
});
-
The deferred call is still needed: with
resume(); pause();in the same
tick,resume_()restarts reading one tick later, and the deferred
onpause()stops it again. -
onpause()only stops reading whenstdin._handle.reading && !stdin.readableFlowing, so calling it twice is harmless. -
Stopping the read manually right after
pause()(the same operation that
onpause()performs) fixes the original repro on current Node.js:process.stdin.pause(); process.stdin._readableState.reading = false; process.stdin._handle.reading = false; process.stdin._handle.readStop(); spawnSync('vim', ['--clean'], { stdio: 'inherit' }); // works
- 主要言語
- JavaScript
- スター
- 124k
- フォーク
- 38k
- 平均マージ
- 4日 2時間
- マージ済み PR(30日)
- 266
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
nodejs/node のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 69/100
メンテナーはふだん 1 日以内に返信
-
fs: document readStream.start and readStream.end対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープンdoc
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
nodejs/node#67848 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
doc: document the Buffer.prototype.*Slice() methods対応中かも @AyushCipher が今日担当しました。 オープンdoc
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
nodejs/node#67847 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
`styleText` fast path bypasses `options` and `options.validateStream` validation対応中かも @GBra-4-669 が 8 日前に担当しました。 オープンutil
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
riscv64
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
nodejs/node#67655 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
似ている issue
-
bug triage needed
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 2 日以内に返信
-
resource-submission validation-passed
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
hesreallyhim/awesome-claude-code#3773 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 73/100
tabatkins/railroad-diagrams#129 ·
-
web: api() turns a null or object-valued error body into a TypeError or "[object Object]" and loses the status対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信