Global g_server variable in server.c is not thread-safe for signal handling #66

Closed
opened 2026-07-20 17:14:08 +02:00 by TapTap · 0 comments
Owner

Description

In src/server/server.c lines 115-123, a global variable g_server is used for cleanup on signals:

static Server* g_server = NULL;

static void cleanup(int sig) {
    (void)sig;
    if (g_server) {
        server_delete(&g_server);
    }
    _exit(0);
}

While the server uses fork() per connection (not threads), the _exit(0) in the signal handler is safe. However, the server_delete() call isn't async-signal-safe — it calls close(), SSL_CTX_free(), and free(), none of which are guaranteed to be safe inside a signal handler.

Location

src/server/server.c:115-123

Suggested Fix

Change the signal handler to only set a flag, and let the main loop check the flag:

static volatile sig_atomic_t g_shutdown_requested = 0;

static void cleanup(int sig) {
    (void)sig;
    g_shutdown_requested = 1;
}

Then in the accept loop, check g_shutdown_requested and break out to clean up properly.

Severity

Low

Category

Quality

## Description In `src/server/server.c` lines 115-123, a global variable `g_server` is used for cleanup on signals: ```c static Server* g_server = NULL; static void cleanup(int sig) { (void)sig; if (g_server) { server_delete(&g_server); } _exit(0); } ``` While the server uses `fork()` per connection (not threads), the `_exit(0)` in the signal handler is safe. However, the `server_delete()` call isn't async-signal-safe — it calls `close()`, `SSL_CTX_free()`, and `free()`, none of which are guaranteed to be safe inside a signal handler. ## Location `src/server/server.c:115-123` ## Suggested Fix Change the signal handler to only set a flag, and let the main loop check the flag: ```c static volatile sig_atomic_t g_shutdown_requested = 0; static void cleanup(int sig) { (void)sig; g_shutdown_requested = 1; } ``` Then in the accept loop, check `g_shutdown_requested` and break out to clean up properly. ## Severity Low ## Category Quality
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: TapTap/FastSync#66