Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 98a9089

Browse files
danielchatfieldpiscisaureus
authored andcommitted
readline: pause stdin before turning off terminal raw mode
On windows, libuv will immediately make a `ReadConsole` call (in the thread pool) when a 'flowing' `uv_tty_t` handle is switched to line-buffered mode. That causes an immediate issue for some users, since libuv can't cancel the `ReadConsole` operation on Windows 8 / Server 2012 and up if the program switches back to raw mode later. But even if this will be fixed in libuv at some point, it's better to avoid the overhead of starting work in the thread pool and immediately cancelling it afther that. See also f34f1e3, where the same change is made for the opposite flow, e.g. move `resume()` after `_setRawMode(true)`. Fixes #5927 This is a backport of dfb0461 (see #5930) to the v0.8 branch.
1 parent 42f926e commit 98a9089

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

lib/readline.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,10 @@ Interface.prototype._refreshLine = function() {
260260

261261
Interface.prototype.close = function() {
262262
if (this.closed) return;
263+
this.pause();
263264
if (this.terminal) {
264265
this._setRawMode(false);
265266
}
266-
this.pause();
267267
this.closed = true;
268268
this.emit('close');
269269
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var assert = require('assert');
23+
var readline = require('readline');
24+
25+
var rl = readline.createInterface(process.stdin, process.stdout);
26+
rl.resume();
27+
28+
var hasPaused = false;
29+
30+
var origPause = rl.pause;
31+
rl.pause = function() {
32+
hasPaused = true;
33+
origPause.apply(this, arguments);
34+
}
35+
36+
var origSetRawMode = rl._setRawMode;
37+
rl._setRawMode = function(mode) {
38+
assert.ok(hasPaused);
39+
origSetRawMode.apply(this, arguments);
40+
}
41+
42+
rl.close();

0 commit comments

Comments
 (0)