nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨

Home Page:https://nodejs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[windows] [readline] [https] Closing input halts event loop

coolaj86 opened this issue · comments

  • Version: 10.2.1, 10.6.0
  • Platform: Windows 7, Windows 10
  • Subsystem: readline https

It turns out that if you have readline open as a terminal, ask a question, make an http request, then close readline, the even loop appears to halt on the next http request.

No error. No timeout. Just sits there for minutes or hours or days.

Does not affect Mac or Linux, just Windows.

Took me a few hours of debugging over a few days to figure out that I wasn't doing anything wrong in my code and then reduce it down far enough to produce an isolated test case that proves I'm not going insane. :)

'use strict';

var https = require("https");
var readline = require('readline');

var rl = readline.createInterface({
  input: process.stdin
, output: process.stdout
  // Setting explicitly due to https://github.com/nodejs/node/issues/21319
  // however, on windows it is being run directly
, terminal: true
});

rl.question('Type anything: ', makeRequests);

function makeRequests() {

  var req = https.request('https://telebit.cloud/_apis/telebit.cloud/index.json', function (resp) {
    console.log("ONE response received");

    resp.on('data', function () {
      console.log("ONE got some data");
    });
    resp.on('end', nextRequest);

    function nextRequest() {
      console.log("ONE completed the request");
      rl.close();
      console.log("closed the readline");

      var req = https.request('https://telebit.cloud/_apis/telebit.cloud/index.json', function (resp) {
        console.log("TWO response received");
        resp.on('data', function () {
          console.log("TWO got some data");
        });
        resp.on('end', function () {
          console.log("TWO completed the request");
        });
      });
      req.on('error', function (err) {
        console.error(err);
      });
      req.end();

      //
      // TESTING THE EVENT LOOP
      //
      console.log("(before loop is checked)");
      process.nextTick(function () {
        console.log("(same loop)"); // never shows
      });
      setTimeout(function () {
        console.log("(future loop)"); // never shows
      }, 100);
    }

  });
  req.on('error', function (err) {
    console.error(err);
  });
  req.end();

}

Possibly related bugs

Possible workarounds

  • Explicitly set terminal: false
  • Don't call rl.close()

Cannot reproduce on 10.6.0

Hm.. while I get those (same loop) and (future loop) messages, Node does not exit until I hit enter.

@bzoz What OS and version of OS are you on?

Also, do you ever see "TWO completed the request"?

Win10 17134.165

@coolaj86, are you maybe using ConEmu or some other console emulator?

Able to reproduce the issue on reported version v10.6.0 using Windows 8.1 x64 and command prompt.

C:\> node 21771.js
Type anything: a
ONE response received
ONE got some data
ONE completed the request
closed the readline
(before loop is checked)

Also, inserting

setTimeout(() =>
  console.log('ok'),
1e4);

at the beginning of the script and waiting 10 seconds prints nothing, but the cpu usage is 0.

Windows Server 2016 Datacenter v1607 is also affected, with node v10.13.0 and 11.2, on both commdand prompt and powershell

Both the suggested workarounds fix this issue

@ronag this seems streams related and I guess you might want to have a look at this.

@BridgeAR I wouldn't mind looking into this but I don't have a Windows setup anymore. Any advice in regards to how to have a virtual setup? Is there a any online service which could just give me a e.g. a ssh to windows machine?

@ronag I can provide you a virtual machine (VirtualBox or VMWare) with Windows operating system installed (I have almost any Windows version from Windows 95 to the latest Windows 10). Just let me know which Windows version you want and how much RAM and HDD space the virtual machine should have so that you can run it smoothly on your system.

@Hakerh400: Much appreciated! A Windows 10 with 1G ram and 64 GB HDD should be enough.

Windows10.zip

Username: user
Password: pass

+1 - Still an issue on node v12.16.3/Windows 10/Powershell

#30701
Node 14.6 has fixed this problem.