Closed
Description
Version
Node v21.6.0
Platform
Microsoft Windows NT 10.0.22621.0 x64
Subsystem
node:readline
What steps will reproduce the bug?
- 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();
- run the code
- when
>
is visible try to pressCTRL + 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:
- SIGINT not triggering with readline #4758
- Have to send two SIGINT to kill when prompt is open SBoudrias/Inquirer.js#293 (Ticket closed as stale)
- Ctrl+C during np cli exits with status 0 sindresorhus/np#528 (Demanding inquirer to fixes it, when I track all the issues, the issue is coming from how
node:readline
core works, correct me if I'm wrong.)