You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Example 1
Deno.addSignalListener("SIGINT", () => {
console.log("interrupted!"); // Never prints
Deno.exit();
});
while (true) {
const val = prompt('Prompt: '); // Hit Ctrl-c while in this prompt
console.log(val); // logs null
}
As it stands right now, this program will log a null and the SIGINT handler will never run. However, if I remove the loop and run this the SIGINT handler runs (adapted from the deno docs):
// Example 2
let sigint = false;
Deno.addSignalListener("SIGINT", () => {
console.log("interrupted!");
sigint = true;
});
const val = prompt("Prompt: ");
console.log(val); // prints null
// Add a timeout to prevent process exiting immediately.
setTimeout(() => {}, 5000); // prints "interrupted!"
The only workaround I've found is that you can yield control using a setTimeout in a promise:
// Example 3
Deno.addSignalListener("SIGINT", () => {
console.log("interrupted!");
Deno.exit(1);
});
while (true) {
const val = prompt("Prompt: ");
const printHandlerPromise = new Promise((resolve, reject) => {
console.log(val);
setTimeout(() => {
resolve(null);
}, 0);
});
await printHandlerPromise;
}
It seems like Example 1 should do something other than silently swallow the SIGINT function call. Perhaps the handler should run after the user inputs text at the next prompt?
The text was updated successfully, but these errors were encountered:
finleyjb
changed the title
It should be possible to handle SIGINT that is raised inside looped prompt
It should be possible to handle SIGINT that is raised inside a looped prompt
Dec 24, 2024
import*asreadlinefrom"node:readline/promises";importprocessfrom"node:process";constrl=readline.createInterface({input: process.stdin,output: process.stdout,});rl.addListener("SIGINT",()=>{console.log("interrupted!");// Never printsDeno.exit();});while(true){constval=awaitrl.question("Prompt: ");// Hit Ctrl-c while in this promptconsole.log(val);// logs null}
prompt api is problematic in deno, its synchronous and it blocks all async tasks in deno, i imagine its not the case in browsers implementation
Version: Deno 2.0.0
Given the following code:
As it stands right now, this program will log a null and the
SIGINT
handler will never run. However, if I remove the loop and run this theSIGINT
handler runs (adapted from the deno docs):The only workaround I've found is that you can yield control using a
setTimeout
in a promise:It seems like Example 1 should do something other than silently swallow the
SIGINT
function call. Perhaps the handler should run after the user inputs text at the next prompt?The text was updated successfully, but these errors were encountered: