Bird
Raised Fist0
Denoframework~20 mins

Signal handling and process management in Deno - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Signal Mastery in Deno
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a Deno process receives a SIGINT signal?
Consider this Deno script that listens for the SIGINT signal (Ctrl+C). What will be printed when you press Ctrl+C during its execution?
Deno
Deno.addSignalListener("SIGINT", () => {
  console.log("Signal SIGINT received, cleaning up...");
  Deno.exit(0);
});

console.log("Process running. Press Ctrl+C to stop.");
setInterval(() => {}, 1000);
AThe message "Process running. Press Ctrl+C to stop." is printed repeatedly every second.
BThe message "Signal SIGINT received, cleaning up..." is printed, then the process exits immediately.
CNothing happens; the process ignores Ctrl+C and continues running.
DThe process throws an uncaught exception and crashes.
Attempts:
2 left
💡 Hint
Think about what Deno.addSignalListener does when a signal is caught.
📝 Syntax
intermediate
2:00remaining
Which code snippet correctly spawns a child process in Deno?
You want to run the command `echo Hello` as a child process in Deno. Which snippet correctly does this and prints the output?
A
const p = Deno.run({ cmd: ["echo", "Hello"] });
const output = await p.output();
console.log(new TextDecoder().decode(output));
p.close();
B
const p = Deno.spawn({ command: "echo Hello" });
const output = await p.output();
console.log(output);
p.close();
C
const p = Deno.run({ command: "echo Hello" });
const output = await p.output();
console.log(new TextDecoder().decode(output));
p.close();
D
const p = Deno.spawn({ cmd: ["echo", "Hello"] });
const output = await p.output();
console.log(new TextDecoder().decode(output));
p.close();
Attempts:
2 left
💡 Hint
Check the correct method name and property names for spawning processes in Deno.
🔧 Debug
advanced
2:00remaining
Why does this Deno script not exit after receiving SIGTERM?
This script listens for SIGTERM but does not exit as expected. What is the reason?
Deno
Deno.addSignalListener("SIGTERM", () => {
  console.log("SIGTERM received");
});

console.log("Waiting for SIGTERM...");
setInterval(() => {}, 1000);
AThe setInterval prevents signal listeners from running.
BSIGTERM is not supported on the current platform, so the listener never triggers.
CThe script never calls Deno.exit(), so it keeps running despite the signal.
DThe console.log inside the listener causes an error stopping the handler.
Attempts:
2 left
💡 Hint
Think about what happens after the signal listener runs.
❓ state_output
advanced
2:00remaining
What is the output of this Deno script managing multiple signals?
Analyze the output when this script receives SIGINT and then SIGTERM signals in order.
Deno
let sigintCount = 0;
let sigtermCount = 0;

Deno.addSignalListener("SIGINT", () => {
  sigintCount++;
  console.log(`SIGINT count: ${sigintCount}`);
  if (sigintCount >= 2) Deno.exit(0);
});

Deno.addSignalListener("SIGTERM", () => {
  sigtermCount++;
  console.log(`SIGTERM count: ${sigtermCount}`);
});

console.log("Process started. Send signals now.");
setInterval(() => {}, 1000);
AOn first SIGINT: prints 'SIGINT count: 1'. On SIGTERM: prints 'SIGTERM count: 1'. On second SIGINT: prints 'SIGINT count: 2' and exits.
BOn first SIGINT: prints 'SIGINT count: 1' and exits immediately. SIGTERM is never handled.
COn SIGTERM: prints 'SIGTERM count: 1' and exits. SIGINT signals are ignored.
DThe script crashes on receiving any signal due to missing await.
Attempts:
2 left
💡 Hint
Count how many times each signal handler runs and when exit is called.
🧠 Conceptual
expert
2:00remaining
Which statement about Deno signal handling and process management is true?
Select the one true statement about how Deno handles signals and child processes.
ADeno automatically terminates child processes when the parent process exits, without extra code.
BDeno does not support listening to SIGTERM signals on any platform.
CDeno's signal handling requires importing a special module; it is not available by default.
DSignal listeners in Deno run asynchronously and can prevent process exit if they do not call Deno.exit().
Attempts:
2 left
💡 Hint
Consider how signal listeners affect process lifecycle and exit.

Practice

(1/5)
1. What is the main purpose of signal handling in a Deno program?
easy
A. To respond to system events like Ctrl+C or termination requests
B. To speed up the execution of asynchronous code
C. To automatically restart the program after an error
D. To manage memory allocation for the program

Solution

  1. Step 1: Understand what signals are

    Signals are system messages sent to a program, such as when a user presses Ctrl+C.
  2. Step 2: Identify signal handling purpose

    Signal handling lets the program catch these signals and respond, for example, to clean up resources before exiting.
  3. Final Answer:

    To respond to system events like Ctrl+C or termination requests -> Option A
  4. Quick Check:

    Signal handling = respond to system events [OK]
Hint: Signals catch system events like Ctrl+C to handle them [OK]
Common Mistakes:
  • Confusing signal handling with error handling
  • Thinking signal handling speeds up code
  • Assuming signals manage memory automatically
2. Which of the following is the correct way to listen for a SIGINT signal in Deno?
easy
A. Deno.listenSignal('SIGINT', () => console.log('Interrupted'))
B. Deno.signal(Deno.Signal.SIGINT).then(() => console.log('Interrupted'))
C. Deno.on('SIGINT', () => console.log('Interrupted'))
D. for await (const _ of Deno.signal(Deno.Signal.SIGINT)) { console.log('Interrupted') }

Solution

  1. Step 1: Recall Deno's signal API usage

    Deno.signal returns an async iterable for the given signal.
  2. 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.
  3. Final Answer:

    for await (const _ of Deno.signal(Deno.Signal.SIGINT)) { console.log('Interrupted') } -> Option D
  4. Quick Check:

    Use for-await-of with Deno.signal for signals [OK]
Hint: Use for-await-of loop with Deno.signal to handle signals [OK]
Common Mistakes:
  • 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
3. What will the following Deno code output when you press Ctrl+C once?
const sig = Deno.signal(Deno.Signal.SIGINT);
for await (const _ of sig) {
  console.log('Signal received');
  sig.dispose();
}
medium
A. Signal received (printed once), then program stops listening for SIGINT
B. SyntaxError because dispose() cannot be called inside loop
C. Signal received printed repeatedly on every Ctrl+C press
D. Signal received (printed once), then program continues running

Solution

  1. Step 1: Understand the for-await-of loop over signal

    The loop waits for SIGINT signals and runs the body each time one occurs.
  2. 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.
  3. Final Answer:

    Signal received (printed once), then program stops listening for SIGINT -> Option A
  4. Quick Check:

    dispose() stops signal listening after first event [OK]
Hint: dispose() stops signal listening; loop ends after first signal [OK]
Common Mistakes:
  • Thinking dispose() only pauses temporarily
  • Assuming the loop continues printing on every Ctrl+C
  • Believing dispose() causes syntax errors
4. Identify the error in this Deno code snippet that tries to spawn a child process and handle its exit:
const p = Deno.run({ cmd: ["ls", "-l"] });
p.status().then(status => {
  console.log(`Process exited with code ${status.code}`);
});
p.close();
medium
A. Deno.run does not exist; should use Deno.spawn instead
B. Missing await before p.status() causes unhandled promise
C. Calling p.close() before the process finishes causes resource leaks
D. The cmd array should be a string, not an array

Solution

  1. Step 1: Understand process lifecycle in Deno

    Deno.run starts a child process and returns a process handle.
  2. Step 2: Analyze order of close() call

    Calling p.close() immediately releases resources before the process finishes, which can cause issues.
  3. Final Answer:

    Calling p.close() before the process finishes causes resource leaks -> Option C
  4. Quick Check:

    Close process handle only after process ends [OK]
Hint: Call close() only after process completes to avoid leaks [OK]
Common Mistakes:
  • 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
5. You want to write a Deno script that runs a child process and gracefully handles Ctrl+C to kill the child before exiting. Which approach correctly combines signal handling and process management?
hard
A. Call child.kill() immediately after spawning, then listen for SIGINT to exit
B. Use for-await-of on Deno.signal(Deno.Signal.SIGINT) to catch Ctrl+C, then call child.kill() and await child.status() before exiting
C. Use Deno.run with cmd and ignore signals; rely on OS to kill child on exit
D. Spawn child process with Deno.spawn and never call kill(), just exit on SIGINT

Solution

  1. Step 1: Understand graceful shutdown

    On Ctrl+C, the program should catch SIGINT and clean up child processes before exiting.
  2. 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.
  3. 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 B
  4. Quick Check:

    Signal handling + kill child + await status = graceful exit [OK]
Hint: Catch SIGINT, kill child, await status for clean exit [OK]
Common Mistakes:
  • Killing child before signal received
  • Ignoring signals and leaving child running
  • Not awaiting child.status() causing premature exit
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard