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

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

Closed
fzn0x opened this issue Apr 19, 2024 · 4 comments
Closed

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

fzn0x opened this issue Apr 19, 2024 · 4 comments
Labels
readline Issues and PRs related to the built-in readline module.

Comments

@fzn0x
Copy link

fzn0x commented Apr 19, 2024

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:

@fzn0x fzn0x changed the title SIGINT not working for me Cannot exit all the processes when CTRL + C in readline Apr 19, 2024
@RedYetiDev RedYetiDev added the readline Issues and PRs related to the built-in readline module. label Apr 20, 2024
@RedYetiDev
Copy link
Member

Hi, thanks for the bug report! I'm unable to reproduce this on Kali Linux in NodeJS v21.7.3. Could you try using the latest version of NodeJS and let me know if this issue still occurs?

@fzn0x
Copy link
Author

fzn0x commented Apr 20, 2024

Hi, thanks for the bug report! I'm unable to reproduce this on Kali Linux in NodeJS v21.7.3. Could you try using the latest version of NodeJS and let me know if this issue still occurs?

Hi, it is still not working as expected in Microsoft Windows NT 10.0.22621.0 x64 even with the latest version 21.7.3.

@RedYetiDev
Copy link
Member

Hmm, I'll take a look

@fzn0x
Copy link
Author

fzn0x commented Apr 20, 2024

@RedYetiDev I resolved the issue. I'm using a custom Node command and attempted to integrate some Bun code into the core, which led to this defect. Therefore, the defect originated from Bun. Thank you again for your attention!

I will open ticket there instead. and ssst Don't tell everyone, Node.js is the most stable Javascript Runtime engine in the Ecosystem now ;) This was my mistake.

@fzn0x fzn0x closed this as completed Apr 20, 2024
@fzn0x fzn0x changed the title Cannot exit all the processes when CTRL + C in readline [invalid defect] Cannot exit all the processes when CTRL + C in readline Apr 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
readline Issues and PRs related to the built-in readline module.
Projects
None yet
Development

No branches or pull requests

2 participants