UNIX SIGNALS
A handler runs in the middle of whatever you were doing
A SIGTERM handler calls printf, frees its buffers and calls exit(0). It has worked in every test. In production it sometimes hangs. Where was main when the hanging SIGTERM arrived?
A signal interrupts your process between two instructions, wherever it is. The kernel pushes a frame and runs your handler on the same thread. Main does not continue until the handler returns.
That makes the handler dangerous. If main was inside malloc holding the allocator’s lock, and the handler calls printf, malloc or free, the handler waits for a lock that only main can release. Main is frozen under the handler, so the process hangs. Only async-signal-safe functions, such as write and _exit, are allowed in a handler. The usual pattern: set a volatile sig_atomic_t flag, return, and let the main loop do the cleanup.
void on_term(int sig) {
printf("shutting down\n"); /* stdio lock, may malloc */
free_all_buffers(); /* free(): arena lock */
exit(0); /* runs atexit, flushes */
}
...
signal(SIGTERM, on_term);
while (1) {
char *buf = malloc(4096); /* SIGTERM here + handler = deadlock */
handle_request(buf);
free(buf);
}static volatile sig_atomic_t shutdown_flag = 0;
void on_term(int sig) { shutdown_flag = 1; } /* async-signal-safe */
struct sigaction sa = { .sa_handler = on_term };
sigaction(SIGTERM, &sa, NULL);
while (!shutdown_flag) {
char *buf = malloc(4096);
handle_request(buf);
free(buf);
}
printf("shutting down\n"); /* normal context: anything goes */
free_all_buffers();
exit(0);
/* Main blocks in poll()/read()? Use the self-pipe trick:
the handler write()s one byte to a pipe that poll() watches
(write is async-signal-safe), or use signalfd on Linux. */ Watch both invariants. UNSAFE, NO SIGNAL holds both because no signal arrives. UNSAFE, LUCKY exits 0 but still breaks the first invariant: the strip counts every unsafe call the handler made. UNSAFE, IN MALLOC hangs with the arena lock held. FIX: FLAG takes the same signal at the same point, makes 0 unsafe calls, and exits 0 from the main loop.
This server passes every test, because no test ever sends it SIGTERM. Inject one SIGTERM at a point that hangs the process forever.
GOAL: main deadlocks
Return \{ handler, iteration \}. The grader calls iteration() in a loop and delivers SIGTERM (calls handler()) at every point inside it, including while api.malloc and api.free hold the arena lock. The handler may only use async-signal-safe calls (api.write, api._exit) or set a flag. After the signal, the loop must call api.cleanup() once and api.exit(0) by the next iteration. With no signal it must keep running.
Graceful shutdown on SIGTERM in containers and systemd units, SIGHUP config reloads, SIGCHLD reaping, and logging from a crash handler. Runtimes hide this for you: Go's signal.Notify delivers signals on a channel, and Python runs handlers between bytecodes on the main thread.
“What can you do inside a signal handler?” Explain async-signal-safety with the malloc-lock deadlock, give the volatile sig_atomic_t flag pattern, mention the self-pipe trick or signalfd for a main loop blocked in poll, and say that SIGKILL cannot be caught.