|
cwist
Modern C web framework with libttak runtime
|
Graceful shutdown implementation. More...
#include <cwist/sys/app/shutdown.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#include <cwist/sys/platform.h>#include <sys/socket.h>
Go to the source code of this file.
Macros | |
| #define | _POSIX_C_SOURCE 200809L |
Functions | |
| void | cwist_shutdown_request (void) |
| Request a graceful shutdown of the server. | |
| void | cwist_shutdown_install_handlers (void) |
| Install CWIST's SIGTERM/SIGINT handlers. | |
| void | cwist_shutdown_restore_handlers (void) |
| Restore the SIGTERM/SIGINT handlers saved by the first cwist_shutdown_install_handlers() call. | |
| void | cwist_shutdown_reset (void) |
| Reset shutdown state so the process can serve again. | |
Variables | |
| atomic_int | g_cwist_running = 1 |
| Global running flag. Checked by all server event loops. | |
| int | g_cwist_listen_fd = -1 |
| Registered TCP listening socket (for signal-handler closure). | |
| int | g_cwist_udp_fd = -1 |
| Registered UDP socket for HTTP/3 (for signal-handler closure). | |
| int | g_cwist_drain_timeout_sec = 5 |
| Drain timeout in seconds before process exits. | |
Graceful shutdown implementation.
Definition in file shutdown.c.
Definition at line 6 of file shutdown.c.
Install CWIST's SIGTERM/SIGINT 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().
Request a graceful shutdown of the server.
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.
Reset shutdown state so the process can serve again.
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.
Restore the SIGTERM/SIGINT handlers saved by the first cwist_shutdown_install_handlers() call.
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().
| int g_cwist_drain_timeout_sec = 5 |
Drain timeout in seconds before process exits.
Definition at line 24 of file shutdown.c.
| int g_cwist_listen_fd = -1 |
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().
| atomic_int g_cwist_running = 1 |
Global running flag. Checked by all server event loops.
Definition at line 17 of file shutdown.c.
Referenced by cwist_http2_serve_connection_ex(), cwist_http3_server_loop(), cwist_http_async_send_response(), cwist_http_coalesce_flush(), cwist_http_server_loop(), cwist_https_server_loop(), cwist_shutdown_request(), and cwist_shutdown_reset().
| int g_cwist_udp_fd = -1 |
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().