-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Input from TTY is strange #2504
Comments
This is seems to be a regression that could have been introduced by 8cee8f5 (didn't do |
If 8cee8f5 made this problem, I think that |
From what I can tell it has to do with For example, if you use: process.stdin;
console.log('ready');
var
fs = require('fs'),
buffer = new Buffer(1024),
fd = process.platform === 'win32' ? process.stdin.fd : fs.openSync('/dev/tty', 'r'),
readSize = fs.readSync(fd, buffer, 0, 1024);
console.log('INPUT: ' + buffer.toString('utf8', 0, readSize)); Then the first line will get read as expected if you type it after |
You can also replace I'm trying this all on Windows, so I haven't looked at it on *nix. Also, reverting 8cee8f5 does not affect this particular issue. |
Thank you, but... I tried your solution, and it did not read first line. ready
foo
bar
INPUT: bar I tried your sample code on Windows 8.1 64bit with iojs v2.3.1, v2.3.2 and v3.1.0. Of course v2.3.1 works fine. Also I tried For example, this code doesn't read first line: var
fs = require('fs'),
buffer = new Buffer(1024),
fd = process.platform === 'win32' ? process.stdin.fd : fs.openSync('/dev/tty', 'r'),
//readSize = fs.readSync(fd, buffer, 0, 1024);
readSize = fs.readSync(0, buffer, 0, 1024);
console.log('INPUT: ' + buffer.toString('utf8', 0, readSize)); Yes I know that var
fs = require('fs'),
buffer = new Buffer(1024),
//fd = process.platform === 'win32' ? process.stdin.fd : fs.openSync('/dev/tty', 'r'),
//readSize = fs.readSync(fd, buffer, 0, 1024);
readSize = fs.readSync(0, buffer, 0, 1024);
console.log('INPUT: ' + buffer.toString('utf8', 0, readSize)); But process.stdin;
var
fs = require('fs'),
buffer = new Buffer(1024),
readSize = fs.readSync(0, buffer, 0, 1024);
console.log('INPUT: ' + buffer.toString('utf8', 0, readSize)); I think that a point is that the programers don't know that |
@nodejs/streams : is it legal to use fs.readSync to read from process.stdin, which in this case is not a file? |
so is the issue that references to process.stdin initiate the readable stream, which does some buffering and that buffering prevents somebody from reading stdin like it was a file? |
It seems that programmers use This problem occurs when it reads from TTY. It seems no problem when it reads from the file or pipe. (e.g. |
It seems that the Readline module also is affected by this problem. For example: var readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('> ', function(answer) {
console.log('INPUT: ', answer);
readline.close();
}); In io.js v2.3.1-, no problem:
In io.js v2.3.2+, strange second line is shown:
Node.js v4.2.1 also returns same result:
|
It seems that REPL also is affected by this problem. I typed a first
In io.js v2.3.2+, strange second
Node.js v4.2.1 also returns same result:
|
It seems that only the first line of both readline module and REPL is strange, like readSync. For example:
|
I think you're looking at a limitation of the windows console. Once a line-buffered read is started, it cannot be interrupted. So possibly a recent patch changed streams behavior such that data flow from stdin is started, then paused; since the line-buffered read can't be cancelled, it needs to complete first before |
Thanks piscisaureus, but, sorry, my English is poor. 😓 |
I confirmed that the first code of this issue #2504 (comment) (i.e. And it seems that For example: var
fs = require('fs'),
buffer = new Buffer(1024),
fd = process.stdin.fd;
fs.read(fd, buffer, 0, 1024, null, function(err, bytesRead, buffer) {
if (err) { throw err; }
console.log('INPUT: ' + buffer.toString('utf8', 0, bytesRead));
}); In v2.3.1:
In v2.3.2:
In v4.2.1:
Till now, I confirmed issue in:
|
Filed #3490 for the revert. I could use some assistence with a regression test for this. |
This reverts 8cee8f5 which was causing a regression through a possible race condition on Windows 8 and 10. Fixes: nodejs#2996 Fixes: nodejs#2504
This reverts 8cee8f5 which was causing a regression through a possible race condition on Windows 8 and 10. Fixes: nodejs#2996 Fixes: nodejs#2504
I tried read from STDIN by
fs.readSync
that is givenprocess.stdin.fd
.For example:
In v2.3.1-, no problem:
In v2.3.2+, first line is ignored, and second line is accepted:
This problem occurs in only Windows + iojs v2.3.2+.
I don't use
process.stdin.fd
in non-Win becausefs.readSync
can't read it.Therefore,
process.stdin
of v2.3.2+ might have problem. (notfs.readSync
)The text was updated successfully, but these errors were encountered: