Skip to content

Readline hanging after first input in terminal #17495

@basickarl

Description

@basickarl

Tested on 8.1.0 and 9.0.0
Mac Sierra 10.12.6
Darwin 16.7.0 Darwin Kernel Version 16.7.0: Thu Jun 15 17:36:27 PDT 2017; root:xnu-3789.70.16~2/RELEASE_X86_64 x86_64

Was reading the documentation on readline: https://nodejs.org/docs/latest-v8.x/api/readline.html

States the the close method and it states:

The readline.Interface instance should be considered to be "finished"
once the 'close' event is emitted.

So naturally I would open a new interface, like I've done in the following example:

const readline = require('readline');

function question(question, defaultAnswer) {
    // Create the interface
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
    });
    return new Promise((resolve, reject) => {
        rl.question(question, (answer) => {
            // Relinquished control over the input and output streams
            rl.close();
            if (answer.length === 0) {
                resolve(defaultAnswer);
            } else {
                resolve(answer);
            }
        });
    });
}

(async () => {
    let answer;
    answer = await question('1? [y] ', 'y');
    console.log('answer', answer);
    answer = await question('2? [y] ', 'y');
    console.log('answer', answer);
    answer = await question('3? [y] ', 'y');
    console.log('answer', answer);
})();

Output:

$ node test.js
1? [y] y
answer y
2? [y]

It hangs however. I cannot input text not can I CTRL-C. I do not know if this is a Mac bug or a Node bug. This seems to be working on other Mac versions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions