Node: readline hangs the process on Win10

Created on 13 Feb 2020  ·  10Comments  ·  Source: nodejs/node

  • Version: node v13.8.0
  • Platform: Win 10 64-bit
  • Subsystem: Git For Windows 2.25.0, mintty 3.1.0 (x86_64-pc-msys)

What steps will reproduce the bug?

#!/usr/bin/env node

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

readline.question('> ', answer => {
    console.log('answer', answer);
    readline.close();
});

Running the above relying on the hash-bang/she-bang, ./readline.js, will make it hang.

Running the above using node, node readline.js, everything works as expected

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior?

The program should exit back to the shell after the user has pressed Enter

What do you see instead?

The program hangs and I can't kill it either by Ctrl+D nor Ctrl+C

Additional information

A workaround is to wrap the close() call in a setTimeout like so

#!/usr/bin/env node

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

readline.question('> ', answer => {
    console.log('answer', answer);
    setTimeout(() => readline.close());
});

This comment is similar, but that one states that it works in Win 10. That is not my experience.
https://github.com/nodejs/node/issues/17495#issuecomment-414333565

readline windows

Most helpful comment

Unable to reproduce using Command Prompt and Powershell.

Able to reproduce using MinTTY with all versions of Node.js. It may be a MinTTY issue rather than Node.js issue. It works if Node.js is invoked as node (MinTTY searches in PATH), but does not work if full path to the Node.js executable is provided.

All 10 comments

Unable to reproduce using Command Prompt and Powershell.

Able to reproduce using MinTTY with all versions of Node.js. It may be a MinTTY issue rather than Node.js issue. It works if Node.js is invoked as node (MinTTY searches in PATH), but does not work if full path to the Node.js executable is provided.

While I too can't reproduce this exact same issue (OP's code) but I'm seeing similar issue in https://github.com/enquirer/enquirer/issues/245. The workaround (wrapping rl.close() in setTimeout) fixes the issue.

I can reproduce in default Windows CMD, and PowerShell.
I'm not using MinTTY.

There does not appear to be any bug here as the behavior is expected, but there can definitely be some API improvements to be made here.

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

const oldClose = readline.Interface.prototype.close
readline.Interface.prototype.close = function () {
  this.terminal = false
  oldClose.call(this)
};

Node 14.6 has fixed this problem.

Node 14.6 has fixed this problem.

Sweet! I hade a look at the change log (https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V14.md#14.6.0) But nothing caught my eye. Do you know what changed fixed the problem?

I dont know the reason but this problem hasnt appeared since upgrading node from 14.5 to 14.6.
This problem first appeared in version 10.2, and there are no related changes in the change log too.

------------------ 原始邮件 ------------------
发件人: "nodejs/node" <[email protected]>;
发送时间: 2020年8月13日(星期四) 下午5:17
收件人: "nodejs/node"<[email protected]>;
抄送: "710959013"<[email protected]>;"Comment"<[email protected]>;
主题: Re: [nodejs/node] readline hangs the process on Win10 (#31762)

Node 14.6 has fixed this problem.

Sweet! I hade a look at the change log (https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V14.md#14.6.0) But nothing caught my eye. Do you know what changed fixed the problem?


You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.

Can confirm. My above issue seems to have been resolved with Node v14.8.0

It`s related to this commit: libuv/libuv@aeab873

Was this page helpful?
0 / 5 - 0 ratings