Skip to content

fix(gateway): wake WebChat writes on the lws service thread - #127

Closed
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-identification-2a2f
Closed

cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-identification-2a2f

Conversation

@cursor

@cursor cursor Bot commented Oct 17, 2026

Copy link
Copy Markdown

WebChat replies and provider-status broadcasts called lws_callback_on_writable() from the agent thread while lws_service() ran on the gateway thread. In libwebsockets 4.3 that call updates the poll set without the per-thread lock, so a live reply can race the service thread and touch a wsi that LWS_CALLBACK_CLOSED is already tearing down. The process can crash, or the queued frame is never flushed and the browser never sees the reply.

ws_send_to and ws_broadcast_text now only queue the text and call lws_cancel_service(), which is safe from another thread. LWS_CALLBACK_EVENT_WAIT_CANCELLED (delivered on the service thread to every protocol, with a fake wsi) calls ws_on_service_cancel() to mark real connections that still have queued text. http_stop clears the stored context before lws_context_destroy.

Validation: make test_ws (including the new wake tests) passed. src/gateway/ws.c, http_lws.c, and http.c compiled with -Wall -Wextra against libwebsockets 4.3.3. GATEWAY=0 make test built and ran the unit suite; test_routes_hardware still tries to link after its GATEWAY=0 skip line, which is existing makefile behavior and unrelated to this change.

Open in Web聽View Automation聽

lws_callback_on_writable updates the poll set without the per-thread
lock. Replies and provider-status broadcasts called it from the agent
thread while lws_service ran on the gateway thread, racing pollfd
updates and a closing wsi. Queue the frame, cancel the service wait,
and mark writable only from EVENT_WAIT_CANCELLED.

Co-authored-by: esadrianno <esadrianno@gmail.com>
@adriannoes

Copy link
Copy Markdown
Collaborator

Superseded by #128: consolidated into the operator-authored branch fix/gateway-ws-wake-writes (same fix, rebased on #119 and re-validated with make test_ws, CI=true make test, make static). Closing in favor of the consolidation.

@adriannoes adriannoes closed this Oct 17, 2026
@adriannoes
adriannoes deleted the cursor/critical-bug-identification-2a2f branch October 17, 2026 14:40
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.

2 participants