cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
shutdown.h File Reference

Unified Graceful Shutdown support for CWIST. More...

#include <stdatomic.h>
#include <cwist/sys/platform.h>
#include <signal.h>
Include dependency graph for shutdown.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void cwist_shutdown_install_handlers (void)
 Install SIGTERM and SIGINT handlers for graceful shutdown.
 
void cwist_shutdown_restore_handlers (void)
 Put back the SIGTERM and SIGINT handlers that were in place before cwist_shutdown_install_handlers(). Does nothing if they are not installed. cwist_app_listen_ex() calls it before returning, so the signals request a graceful stop only while a server runs.
 
void cwist_shutdown_request (void)
 Request a graceful shutdown, as SIGTERM/SIGINT do: clear the running flag and shut down the listening sockets, so cwist_app_listen() drains and returns. Safe from any thread, a handler or a signal handler; repeated calls are harmless.
 
void cwist_shutdown_reset (void)
 Reset shutdown state (useful in test suites).
 

Variables

atomic_int g_cwist_running
 Global running flag. Checked by all server event loops.
 
int g_cwist_listen_fd
 Registered TCP listening socket (for signal-handler closure).
 
int g_cwist_udp_fd
 Registered UDP socket for HTTP/3 (for signal-handler closure).
 
int g_cwist_drain_timeout_sec
 Drain timeout in seconds before process exits.
 

Detailed Description

Unified Graceful Shutdown support for CWIST.

Provides a global atomic 'running' flag and signal handling for SIGTERM/SIGINT. All protocol server loops (HTTP/1.1, HTTP/2, HTTP/3) check this flag.

Definition in file shutdown.h.

Function Documentation

◆ cwist_shutdown_install_handlers()

void cwist_shutdown_install_handlers ( void  )

Install SIGTERM and SIGINT handlers for graceful shutdown.

The handlers in place before the first install are saved for cwist_shutdown_restore_handlers().

Install SIGTERM and SIGINT handlers for graceful shutdown.

Both signals are routed to cwist_shutdown_handler() with SA_RESTART. The pre-existing handlers are saved on the first install after a restore so cwist_shutdown_restore_handlers() can put them back; a second install without a restore does not overwrite the saved handlers. The previous handlers are only recorded when both sigaction() calls succeed. No-op on WASI hosts, which own the instance lifecycle.

Definition at line 111 of file shutdown.c.

References cap.

Referenced by cwist_app_listen_ex().

◆ cwist_shutdown_request()

void cwist_shutdown_request ( void  )

Request a graceful shutdown, as SIGTERM/SIGINT do: clear the running flag and shut down the listening sockets, so cwist_app_listen() drains and returns. Safe from any thread, a handler or a signal handler; repeated calls are harmless.

Request a graceful shutdown, as SIGTERM/SIGINT do: clear the running flag and shut down the listening sockets, so cwist_app_listen() drains and returns. Safe from any thread, a handler or a signal handler; repeated calls are harmless.

Clears the g_cwist_running flag and releases the listening and UDP descriptors (if open). Closing the listen descriptor also calls shutdown(SHUT_RDWR) so accept() loops blocked in other threads wake up with EINVAL. Safe to call more than once and from any thread: each descriptor is claimed atomically, so a second call cannot close a descriptor number that has been reused for an unrelated file.

The UDP descriptor is only claimed, never closed here: the HTTP/3 loop thread may be inside recvmmsg() on it, and closing under it both races with the read and can later close a reused descriptor number. The loop observes g_cwist_running within its poll timeout (<= 1s) and the owning server path closes the descriptor after joining that thread.

Definition at line 63 of file shutdown.c.

References cap, g_cwist_listen_fd, g_cwist_running, and g_cwist_udp_fd.

◆ cwist_shutdown_reset()

void cwist_shutdown_reset ( void  )

Reset shutdown state (useful in test suites).

Reset shutdown state (useful in test suites).

Sets g_cwist_running back to 1 and marks the listening and UDP descriptors as closed (-1). Does not close any open descriptors or touch signal handlers.

Definition at line 173 of file shutdown.c.

References cap, g_cwist_listen_fd, g_cwist_running, and g_cwist_udp_fd.

◆ cwist_shutdown_restore_handlers()

void cwist_shutdown_restore_handlers ( void  )

Put back the SIGTERM and SIGINT handlers that were in place before cwist_shutdown_install_handlers(). Does nothing if they are not installed. cwist_app_listen_ex() calls it before returning, so the signals request a graceful stop only while a server runs.

Put back the SIGTERM and SIGINT handlers that were in place before cwist_shutdown_install_handlers(). Does nothing if they are not installed. cwist_app_listen_ex() calls it before returning, so the signals request a graceful stop only while a server runs.

Does nothing if no previous handlers were saved. After restoring, the saved state is cleared so a later install records the handlers then in effect. No-op on WASI hosts.

Definition at line 133 of file shutdown.c.

References cap.

Referenced by cwist_app_listen_ex().

Variable Documentation

◆ g_cwist_drain_timeout_sec

int g_cwist_drain_timeout_sec
extern

Drain timeout in seconds before process exits.

Definition at line 24 of file shutdown.c.

◆ g_cwist_listen_fd

int g_cwist_listen_fd
extern

Registered TCP listening socket (for signal-handler closure).

Definition at line 18 of file shutdown.c.

Referenced by cwist_async_server_loop(), cwist_shutdown_request(), and cwist_shutdown_reset().

◆ g_cwist_running

◆ g_cwist_udp_fd

int g_cwist_udp_fd
extern

Registered UDP socket for HTTP/3 (for signal-handler closure).

Definition at line 19 of file shutdown.c.

Referenced by cwist_shutdown_request(), and cwist_shutdown_reset().