We investigated an issue in Asterisk 22.9.0 where WebSocket connectivity stopped working and the HTTP server reported:
HTTP session count exceeded 1000 sessions.
Restarting Asterisk temporarily restored service. The investigation identified two problems in ARI connection cleanup that could retain sessions and block reconnections.
-
Indefinite waiting during WebSocket shutdown
In
res/ari/ari_websockets.c, the ARI reader usedast_wait_for_input()with a socket descriptor obtained fromast_websocket_fd().When the WebSocket was closing, that descriptor could become
-1. Passing it topoll()with an infinite timeout could leave the thread waiting indefinitely, preventing session cleanup. Runtime inspection found five threads in this condition.We replaced that call with
ast_websocket_wait_for_input(). This function checks whether the WebSocket is closing and returns an error in that state, allowing the reader to exit and proceed with cleanup. -
Deadlock while unsubscribing from device-state events
Thread dumps and source analysis indicated a circular wait during ARI session shutdown: cleanup held the session mutex while waiting for a device-state subscription to finish, but an event callback needed that same mutex to complete.
In
res/res_stasis_device_state.c, we replaced the synchronousstasis_unsubscribe_and_join()call withstasis_unsubscribe(). This allows cleanup to continue and release the mutex without waiting for callbacks at that point.We also added an atomic shutdown flag so callbacks that observe cancellation stop forwarding events. Module references keep the callback code loaded until the final subscription message is processed, and object-reference cleanup was adjusted for both normal and failure paths.
These conditions explain how a small number of ARI clients could generate many retained HTTP requests. Reconnection attempts could become blocked before completing the WebSocket handshake, consuming HTTP session capacity without appearing as established sessions in the ARI session listing. The HTTP and HTTPS listeners share this capacity, so the impact could affect both ports 8088 and 8089.
After rebuilding, installing the modified modules, and restarting Asterisk, the ARI connection recovered, event processing resumed, and the application queue remained empty in subsequent checks. Continued operation was reported as stable during the observation period.