Asterisk WebSocket

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.

  1. Indefinite waiting during WebSocket shutdown

    In res/ari/ari_websockets.c, the ARI reader used ast_wait_for_input() with a socket descriptor obtained from ast_websocket_fd().

    When the WebSocket was closing, that descriptor could become -1. Passing it to poll() 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.

  2. 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 synchronous stasis_unsubscribe_and_join() call with stasis_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.

Will you be creating an issue and accompanying pull request to fix?

And #2 has an issue already open: [bug]: res_stasis_device_state: unsubscribe_device_state() joins a stasis subscription while the ARI session lock is held, deadlocking ARI · Issue #2170 · asterisk/asterisk · GitHub