Skip to content
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

readline hangs the process on Win10 #31762

Open
Tobbe opened this issue Feb 13, 2020 · 12 comments
Open

readline hangs the process on Win10 #31762

Tobbe opened this issue Feb 13, 2020 · 12 comments
Labels
readline Issues and PRs related to the built-in readline module. windows Issues and PRs related to the Windows platform.

Comments

@Tobbe
Copy link

Tobbe commented Feb 13, 2020

  • 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.
#17495 (comment)

@Hakerh400
Copy link
Contributor

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.

@laggingreflex
Copy link

While I too can't reproduce this exact same issue (OP's code) but I'm seeing similar issue in enquirer/enquirer#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.

ahmedkotb added a commit to ahmedkotb/enquirer that referenced this issue May 8, 2020
@jasnell
Copy link
Member

jasnell commented Jun 25, 2020

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.

@jasnell jasnell added the readline Issues and PRs related to the built-in readline module. label Jun 25, 2020
@chengzhuo5
Copy link

chengzhuo5 commented Aug 10, 2020

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

@chengzhuo5
Copy link

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

@chengzhuo5
Copy link

Node 14.6 has fixed this problem.

@Tobbe
Copy link
Author

Tobbe commented Aug 13, 2020

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?

@chengzhuo5
Copy link

chengzhuo5 commented Aug 13, 2020 via email

@laggingreflex
Copy link

laggingreflex commented Aug 13, 2020

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

@chengzhuo5
Copy link

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

rivy added a commit to rivy/humblebundle-download that referenced this issue Nov 4, 2020
## discussion

Node-v10.2 through Node-v14.6 contain a bug which may cause an application
freeze on Windows platforms when 'readline' is closing (see discussion at
<nodejs/node#31762>).

As a work-around, using...

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

seems to fix the issue without any obvious negative side effects.

ref: <enquirer/enquirer#245>
ref: <nodejs/node#31762>
@BlackGlory
Copy link

BlackGlory commented Jan 10, 2021

Still not working correctly.

  • Windows 10 19042
  • Node.js 14.15.4
  • PowerShell 7.1.0
  • npm 6.14.11

cli.js

#!/usr/bin/env node

const readline = require("readline")
;(async () => {
  const stdin = readline.createInterface({ input: process.stdin })
  for await (const line of stdin) {
    console.log(line)
  }
})()
echo 'ok' | node cli.js
# ok

echo 'ok' | the-bin-field-in-package-json
# hang

It seems to be a duplicate of this issue.

@privatenumber
Copy link
Contributor

privatenumber commented Feb 23, 2023

I can reproduce this pretty consistently (90% of the time) using the following code. Tested on Node.js v16.17.1:

import readline from 'readline';
import { setTimeout } from 'timers/promises';

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

	return () => {
		console.log('unblock');
		r.close();
	};
};

(async () => {
	const blockA = block();
	await setTimeout(10);
	blockA();

	await setTimeout(10);
	block()();
})().catch(() => {});

Screenshot 2023-02-23 at 2 03 30 PM

As suggested in the comments above, I've been able to unblock it by setting .terminal = false or adding a setTimeout:

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

	return async () => {
		console.log('unblock');
		await setTimeout(0);
		r.terminal = false;
		r.close();
	};
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
readline Issues and PRs related to the built-in readline module. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

No branches or pull requests

8 participants