What if your app could gracefully handle sudden stops without losing data or crashing?
Why Signal handling and process management in Deno? - Purpose & Use Cases
Imagine you run a server that needs to stop gracefully when you press Ctrl+C or when the system sends a shutdown signal.
You try to catch these signals manually by checking constantly if a key was pressed or if the process should stop.
Manually checking for signals or process status is like constantly looking over your shoulder--it wastes time and can miss important events.
This leads to servers that crash abruptly, lose data, or leave resources open, causing bigger problems.
Signal handling and process management lets your program listen for system signals like interrupts or termination requests.
It automatically triggers your cleanup code so your app can close files, save data, and shut down smoothly.
while(true) { if (checkForCtrlC()) { cleanup(); break; } }
Deno.addSignalListener("SIGINT", () => {
cleanup();
Deno.exit();
});This makes your programs reliable and professional, able to handle unexpected stops without losing data or leaving things broken.
Think of a web server that saves user sessions and closes database connections properly when you stop it with Ctrl+C, instead of crashing and losing everything.
Manual signal checks waste resources and risk errors.
Signal handling listens for system events and runs cleanup automatically.
Process management helps apps stop safely and cleanly.
Practice
Solution
Step 1: Understand what signals are
Signals are system messages sent to a program, such as when a user presses Ctrl+C.Step 2: Identify signal handling purpose
Signal handling lets the program catch these signals and respond, for example, to clean up resources before exiting.Final Answer:
To respond to system events like Ctrl+C or termination requests -> Option AQuick Check:
Signal handling = respond to system events [OK]
- Confusing signal handling with error handling
- Thinking signal handling speeds up code
- Assuming signals manage memory automatically
Solution
Step 1: Recall Deno's signal API usage
Deno.signal returns an async iterable for the given signal.Step 2: Identify correct syntax to listen continuously
Using a for-await-of loop over Deno.signal(Deno.Signal.SIGINT) listens repeatedly for SIGINT events.Final Answer:
for await (const _ of Deno.signal(Deno.Signal.SIGINT)) { console.log('Interrupted') } -> Option DQuick Check:
Use for-await-of with Deno.signal for signals [OK]
- Using .then() which only handles one signal once
- Trying to use event emitter style which Deno does not support
- Using non-existent Deno.listenSignal method
const sig = Deno.signal(Deno.Signal.SIGINT);
for await (const _ of sig) {
console.log('Signal received');
sig.dispose();
}Solution
Step 1: Understand the for-await-of loop over signal
The loop waits for SIGINT signals and runs the body each time one occurs.Step 2: Effect of calling sig.dispose()
Calling dispose() stops the signal listener, so after the first Ctrl+C, the loop ends and no more signals are handled.Final Answer:
Signal received (printed once), then program stops listening for SIGINT -> Option AQuick Check:
dispose() stops signal listening after first event [OK]
- Thinking dispose() only pauses temporarily
- Assuming the loop continues printing on every Ctrl+C
- Believing dispose() causes syntax errors
const p = Deno.run({ cmd: ["ls", "-l"] });
p.status().then(status => {
console.log(`Process exited with code ${status.code}`);
});
p.close();Solution
Step 1: Understand process lifecycle in Deno
Deno.run starts a child process and returns a process handle.Step 2: Analyze order of close() call
Calling p.close() immediately releases resources before the process finishes, which can cause issues.Final Answer:
Calling p.close() before the process finishes causes resource leaks -> Option CQuick Check:
Close process handle only after process ends [OK]
- Calling close() too early before process ends
- Confusing Deno.run with Deno.spawn (spawn is newer API)
- Thinking cmd should be a string instead of array
Solution
Step 1: Understand graceful shutdown
On Ctrl+C, the program should catch SIGINT and clean up child processes before exiting.Step 2: Combine signal listening and child process control
Using for-await-of on Deno.signal(Deno.Signal.SIGINT) allows catching Ctrl+C; then calling child.kill() stops the child, and awaiting child.status() ensures it ends cleanly.Final Answer:
Use for-await-of on Deno.signal(Deno.Signal.SIGINT) to catch Ctrl+C, then call child.kill() and await child.status() before exiting -> Option BQuick Check:
Signal handling + kill child + await status = graceful exit [OK]
- Killing child before signal received
- Ignoring signals and leaving child running
- Not awaiting child.status() causing premature exit
Ready to prove it?
Pass the test and this pattern moves unknown
