Node: readline: readline.close() result in process.stdout is unexpected

Created on 28 Nov 2019  路  5Comments  路  Source: nodejs/node

  • Version: v12.13.0
  • Platform: 64-bit (Windows)
  • Subsystem:
const readline = require("readline");

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
setTimeout(() => {
    rl.close();
}, 1000);
setInterval(() => {
    console.log('test')
}, 1000);

win10 output(empty)


linux output

test
test
...
const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

setTimeout(() => {
  rl.close();    // process.stdin.isRaw =false
  process.stdin.setRawMode(true); //stop at here
  console.log("asd");
}, 1000);

this code will block without any error

readline windows

All 5 comments

  1. look at the source code 锛宨f replace rl.close() with process.stdin.pause(); process.stdin.setRawMode(false);, the output is the same (win10 empty)
//test.js
const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

setTimeout(() => {
  process.stdin.pause();
  process.stdin.setRawMode(false);

  setInterval(() => {
    console.log("test");
  }, 1000);
}, 1000);
  1. if change console.log to fs.writeFileSync ,it is ok.
  2. But if add both console.log and fs.writeFileSync , the process seems like block after console.log
const readline = require("readline");
const fs = require("fs");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

setTimeout(() => {
  process.stdin.pause();
  process.stdin.setRawMode(false);

  setInterval(() => {
    fs.writeFileSync("./temp", "test2", { flag: "a" });
    console.log("test");
  }, 1000);
}, 1000);

terminal output is empty,file ./temp only has one string test2

 $ cat ./temp
 $ test2

I have looked at the docs,but I'm still confused with these result. Or if these are not bug, what is the recommend way to repeat use readline.createInterface and close readline ?

execute rl.terminal = false before rl.close()can solve this problem.

31762

Node 14.6 has fixed this problem.

Bisected to https://github.com/nodejs/node/commit/a9ca4204e0fff3783211dec65e4548318a9395e3 (precisely https://github.com/libuv/libuv/commit/aeab873bbe4efb94baacc17ca9ab38a45009e6e8). This issue is fixed and should be closed.

Nice ! Thanks for the information. @chengzhuo5 @Hakerh400

Was this page helpful?
0 / 5 - 0 ratings

Related issues

srl295 picture srl295  路  3Comments

danielstaleiny picture danielstaleiny  路  3Comments

willnwhite picture willnwhite  路  3Comments

filipesilvaa picture filipesilvaa  路  3Comments

mcollina picture mcollina  路  3Comments