Skip to content

fix(gateway): stop HTTP thread before freeing auth context - #72

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/high-severity-issues-7e6b
Draft

fix(gateway): stop HTTP thread before freeing auth context#72
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/high-severity-issues-7e6b

Conversation

@cursor

@cursor cursor Bot commented Aug 24, 2026

Copy link
Copy Markdown

Bug and impact

Gateway shutdown freed auth_ctx while the libwebsockets thread was still in lws_service. In-flight /api/* requests, /pair, and WebSocket upgrades call auth_validate_token / auth_pair on that pointer, so SIGTERM during authenticated traffic is a use-after-free (crash or memory corruption).

Trigger: gateway enabled, at least one authenticated HTTP or WS request in flight (or arriving), then SIGTERM/SIGINT. main_loop exits and cleanup_subsystems runs. The HTTP thread has running==1 until http_stop joins it.

The tools_init failure path already stopped HTTP first. Normal shutdown did not.

Root cause

cleanup_subsystems called auth_cleanup(g_auth_ctx) before http_stop(). http_stop is what clears running and pthread_joins the lws thread.

Fix

Signal WS, then http_stop() (join the HTTP thread), then auth_cleanup. Same order as the tools_init failure path.

Validation

tests/test_gateway_http.c now drives authenticated /api/status traffic and SIGTERM, then fails if the process dies with SIGSEGV/SIGABRT/SIGBUS/SIGILL. This environment could not build with GATEWAY=1 (no libcurl/libwebsockets packages), so the new assertion was not executed here; CI with GATEWAY=1 will run it.

Open in WebView Automation

The lws thread still calls auth_validate_token after SIGTERM. Freeing
auth_ctx first raced with in-flight /api and WebSocket auth checks.

Co-authored-by: esadrianno <esadrianno@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant