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

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

Closed
coolaj86 opened this issue Jul 11, 2018 · 16 comments
Closed

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

coolaj86 opened this issue Jul 11, 2018 · 16 comments
Labels
https Issues or PRs related to the https subsystem. readline Issues and PRs related to the built-in readline module. stream Issues and PRs related to the stream subsystem. windows Issues and PRs related to the Windows platform.

Comments

@coolaj86
Copy link
Contributor

coolaj86 commented Jul 11, 2018

  • 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()
@coolaj86 coolaj86 changed the title [windows] [readline] [https] Closing input causes requests to freeze [windows] [readline] [https] Closing input halts event loop Jul 11, 2018
@bzoz
Copy link
Contributor

bzoz commented Jul 12, 2018

Cannot reproduce on 10.6.0

@bzoz
Copy link
Contributor

bzoz commented Jul 12, 2018

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

@coolaj86
Copy link
Contributor Author

coolaj86 commented Jul 12, 2018

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

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

@bzoz
Copy link
Contributor

bzoz commented Jul 12, 2018

Win10 17134.165

@bzoz
Copy link
Contributor

bzoz commented Jul 12, 2018

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

@ghost
Copy link

ghost commented Jul 12, 2018

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.

@totomz
Copy link

totomz commented Nov 16, 2018

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

totomz added a commit to porketta/Inquirer.js that referenced this issue Nov 16, 2018
This workaround fix a nodejs issue (nodejs/node#21771)
that cause porcli to not work on windows
@Trott Trott added windows Issues and PRs related to the Windows platform. readline Issues and PRs related to the built-in readline module. https Issues or PRs related to the https subsystem. labels Nov 29, 2018
@BridgeAR BridgeAR added the stream Issues and PRs related to the stream subsystem. label Dec 14, 2019
@BridgeAR
Copy link
Member

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

@ronag
Copy link
Member

ronag commented Dec 20, 2019

@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?

@Hakerh400
Copy link
Contributor

@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.

@ronag
Copy link
Member

ronag commented Dec 20, 2019

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

@Hakerh400
Copy link
Contributor

Hakerh400 commented Dec 20, 2019

Windows10.zip

Username: user
Password: pass

@tjhiggins
Copy link

tjhiggins commented May 22, 2020

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

@chengzhuo5
Copy link

#30701
Node 14.6 has fixed this problem.

@huseyinacacak-janea
Copy link
Contributor

I've investigated this issue with Node v18.20.2, v20.16.0 and v22.7.0. All of them worked as expected.
@coolaj86 Do you think this can be closed as this issue is now fixed in the current versions of Node?

@RedYetiDev
Copy link
Member

Per the comment above.

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

No branches or pull requests