Skip to content

[invalid defect] Cannot exit all the processes when CTRL + C in readline  #52597

Closed
@fzn0x

Description

@fzn0x

Version

Node v21.6.0

Platform

Microsoft Windows NT 10.0.22621.0 x64

Subsystem

node:readline

What steps will reproduce the bug?

  1. I'm using this code to apply 2 ways of SIGINT, process SIGINT and readline SIGINT
// Import the readline module to mock user interactions via the console.
const readline = require('readline');

// Mock the client and its dependencies with basic stubs.
class Client {
    async sendRequest(url: string) {
        return 'Response from ' + url; // Dummy response
    }
}

// Function to simulate response conversion from gemtext.
function fromGemtext(response: string) {
    return response; // Simply return the same dummy response for simplicity
}

const client = new Client();
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

process.on('SIGINT', function () {
    console.log('Graceful shutdown');
    process.exit(0);
}).on('SIGINT', function () {
    process.emit('SIGINT');
});

process.on('SIGINT', function () {
  // graceful shutdown
  process.kill(process.pid, 'SIGINT');
});

// Versioning for the dummy package.
const version = '1.0.0';  // Hardcoded version
console.log(`Dummy Client v${version}`);

console.log("Dummy CLI for testing. Type '/c' to exit or any URL to simulate a request.");

// Function to handle the "gemini" requests in a dummy manner.
async function handleGeminiRequest(inputUrl: string) {
    try {
        const response = await client.sendRequest(inputUrl);
        console.log(fromGemtext(response));
    } catch (err) {
        if (err instanceof Error)
            console.log('Error:', err.message);
    }
}

// Function to create commands.
function createCommands(string: string) {
    if (string.trim() === '/c') {
        return 'exit';
    }
    return 'url-request'; // Default to URL request for any other input
}

// Function to prompt and process commands.
function promptAndProcessCommand() {
    console.log(`Platform ${process.platform}, Node ${process.version}, isTTY? ${process.stdout.isTTY}`);
    if (process.platform === "win32") {
        rl.on("SIGINT", function () {
            console.log("Process terminated (SIGINT).");
            process.kill(process.pid, 'SIGINT');
        });
    }

    rl.question('> ', async (cmd: string) => {
        const command = createCommands(cmd);
        switch (command) {
            case 'exit':
                console.log('Bye!');
                rl.close();
                process.exit(0);
            case 'url-request':
                await handleGeminiRequest(cmd);
                promptAndProcessCommand(); // Continue the command loop
                break;
        }
    });
}

// Start the command loop.
promptAndProcessCommand();
  1. run the code
  2. when > is visible try to press CTRL + C

How often does it reproduce? Is there a required condition?

N/A. not a flaky bug.

What is the expected behavior? Why is that the expected behavior?

When I press CTRL + C it should exit my program gracefully

What do you see instead?

I need to press Enter after CTRL + C

Additional information

Related:

Metadata

Metadata

Assignees

No one assigned

    Labels

    readlineIssues and PRs related to the built-in readline module.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions