Inquirer.js: MaxListenersExceededWarning in Windows

Created on 2 Feb 2020  路  3Comments  路  Source: SBoudrias/Inquirer.js

I run packages/inquirer/examples/recursive.js.
In 7.0.4 (latest) on Windows, I get a MaxListenersExceededWarning:

> node recursive.js
? What's your favorite TV show? 1
? Want to enter another TV show favorite (just hit enter for YES)? Yes
? What's your favorite TV show? 2
? Want to enter another TV show favorite (just hit enter for YES)? Yes
? What's your favorite TV show? 3
? Want to enter another TV show favorite (just hit enter for YES)? Yes
? What's your favorite TV show? 4
? Want to enter another TV show favorite (just hit enter for YES)? Yes
? What's your favorite TV show? 5
? Want to enter another TV show favorite (just hit enter for YES)? Yes
? What's your favorite TV show? 6
? Want to enter another TV show favorite (just hit enter for YES)? Yes
? What's your favorite TV show? 7
? Want to enter another TV show favorite (just hit enter for YES)? Yes
? What's your favorite TV show? 8
? Want to enter another TV show favorite (just hit enter for YES)? Yes
? What's your favorite TV show? 9
? Want to enter another TV show favorite (just hit enter for YES)? Yes
? What's your favorite TV show? (node:40212) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 end listeners added to [ReadStream]. Use emitter.setMaxListeners() to increase limit
(node:40212) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 keypress listeners added to [ReadStream]. Use emitter.setMaxListeners() to increase limit    
? What's your favorite TV show? 10
? Want to enter another TV show favorite (just hit enter for YES)? No
Your favorite TV Shows: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

The MaxListenersExceededWarning occurs in 7.0.4 and 7.0.2

It does not occur in 7.0.3 and <=7.0.1.

My environment is

 System:
    OS: Windows 10 10.0.18363
    CPU: (8) x64 Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
    Memory: 1.19 GB / 7.85 GB
  Binaries:
    Node: 13.7.0 - C:\Program Files\nodejs\node.EXE
    npm: 6.13.6 - C:\Program Files\nodejs\npm.CMD

Most helpful comment

@SBoudrias I've diagnosed this issue while trying to fix it in Amplify CLI where we're leveraging Inquirer to drive the console prompts.
The root cause is that every prompt creates a new RXJS Observable to the process' stdin, and event based observables can only be released if they're subscribed and the subscription's unsubscribe method is called. Since inquirer only using RXJS operators it does not happen. Only the Editor prompt uses subscription, but only for the line and not for keypress. A single prompt or a prompt list lifecycle could handle it in a clean way, the very same way as Editor does, unsubscribed from keypress in onEnd handler. Since the same problem exist in extensions it would be better to handle in a higher level.
Another solution would be to just create the Observable object once and reuse it between prompts as stdin "rarely changes", but events.js could manage a list of rl objects and hand over the proper observable based on the rl object reference. /cc @dantasfiles

All 3 comments

@SBoudrias I've diagnosed this issue while trying to fix it in Amplify CLI where we're leveraging Inquirer to drive the console prompts.
The root cause is that every prompt creates a new RXJS Observable to the process' stdin, and event based observables can only be released if they're subscribed and the subscription's unsubscribe method is called. Since inquirer only using RXJS operators it does not happen. Only the Editor prompt uses subscription, but only for the line and not for keypress. A single prompt or a prompt list lifecycle could handle it in a clean way, the very same way as Editor does, unsubscribed from keypress in onEnd handler. Since the same problem exist in extensions it would be better to handle in a higher level.
Another solution would be to just create the Observable object once and reuse it between prompts as stdin "rarely changes", but events.js could manage a list of rl objects and hand over the proper observable based on the rl object reference. /cc @dantasfiles

@SBoudrias is it possible that this bug is caused by https://github.com/SBoudrias/Inquirer.js/pull/882 (https://github.com/SBoudrias/Inquirer.js/pull/882/commits/03412e5c0b1c9eb2ad8d559bfcc29b5cf7e8575c)? It appears that change makes it so that the readline interface does not get closed on windows.

@rizzlesauce that's possible; but closing the readline on windows was making the prompt unresponsive.

The best solution on the longer term is probably moving away from rxjs altogether. I've made a lot of progress on this https://github.com/SBoudrias/Inquirer.js/issues/692 - but I'm running out of time to complete a release.

I'm okay to review a PR if anyone wants to take the time.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

XhmikosR picture XhmikosR  路  7Comments

bugzpodder picture bugzpodder  路  4Comments

christav picture christav  路  5Comments

jahvi picture jahvi  路  6Comments

aaronshaf picture aaronshaf  路  7Comments