fix(gateway): stop HTTP thread before freeing auth context - #72
Draft
cursor[bot] wants to merge 1 commit into
Draft
fix(gateway): stop HTTP thread before freeing auth context#72cursor[bot] wants to merge 1 commit into
cursor[bot] wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug and impact
Gateway shutdown freed
auth_ctxwhile the libwebsockets thread was still inlws_service. In-flight/api/*requests,/pair, and WebSocket upgrades callauth_validate_token/auth_pairon 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_loopexits andcleanup_subsystemsruns. The HTTP thread hasrunning==1untilhttp_stopjoins it.The tools_init failure path already stopped HTTP first. Normal shutdown did not.
Root cause
cleanup_subsystemscalledauth_cleanup(g_auth_ctx)beforehttp_stop().http_stopis what clearsrunningandpthread_joins the lws thread.Fix
Signal WS, then
http_stop()(join the HTTP thread), thenauth_cleanup. Same order as the tools_init failure path.Validation
tests/test_gateway_http.cnow drives authenticated/api/statustraffic and SIGTERM, then fails if the process dies with SIGSEGV/SIGABRT/SIGBUS/SIGILL. This environment could not build withGATEWAY=1(no libcurl/libwebsockets packages), so the new assertion was not executed here; CI withGATEWAY=1will run it.