Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/core/bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,14 @@ int init_subsystems(config_t *cfg)
void cleanup_subsystems(void)
{
#ifdef SHELLCLAW_GATEWAY
/* HTTP/WS callbacks still call auth_validate_token(ctx->auth). Join the
* lws thread before freeing auth_ctx (same order as tools_init failure). */
ws_shutdown_signal();
http_stop();
if (g_auth_ctx) {
auth_cleanup(g_auth_ctx);
g_auth_ctx = NULL;
}
http_stop();
ws_cleanup();
#endif
tools_cleanup();
Expand Down
2 changes: 1 addition & 1 deletion src/core/bootstrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int init_subsystems(config_t *cfg);
/** Register tools from config (called from init_subsystems). */
int tools_init(const config_t *cfg);

/** Tear down all subsystems in reverse init order. */
/** Tear down all subsystems in reverse init order (HTTP thread before auth_ctx). */
void cleanup_subsystems(void);

config_t *bootstrap_get_cfg(void);
Expand Down
33 changes: 31 additions & 2 deletions tests/test_gateway_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,31 @@ static int test_api_asap_log_401(void)
return 0;
}

static int test_shutdown_does_not_crash(pid_t pid, const char *token)
{
int i;
int status = 0;

if (token && token[0]) {
for (i = 0; i < 16; i++) {
long code = 0;
char *body = NULL;
(void)http_get_auth(gw_url("/api/status"), token, &code, &body);
free(body);
}
}
ASSERT(kill(pid, SIGTERM) == 0);
ASSERT(waitpid(pid, &status, 0) == pid);
if (WIFSIGNALED(status)) {
int sig = WTERMSIG(status);
if (sig == SIGSEGV || sig == SIGABRT || sig == SIGBUS || sig == SIGILL) {
fprintf(stderr, "FAIL: gateway crashed on shutdown with signal %d\n", sig);
return 1;
}
}
return 0;
}

static int test_api_asap_log(const char *token)
{
long code;
Expand Down Expand Up @@ -690,8 +715,12 @@ int main(int argc, char **argv)
if (test_api_sessions(token) != 0) { fprintf(stderr, "test_api_sessions failed\n"); failed++; }
if (test_api_asap_log(token) != 0) { fprintf(stderr, "test_api_asap_log failed\n"); failed++; }
}
kill(pid, SIGTERM);
waitpid(pid, NULL, 0);
if (test_shutdown_does_not_crash(pid, token) != 0) {
fprintf(stderr, "test_shutdown_does_not_crash failed\n");
failed++;
kill(pid, SIGKILL);
waitpid(pid, NULL, 0);
}
unlink(config_path);
unlink(tokens_path);
unlink(pairing_file);
Expand Down
Loading