Skip to content

Commit f01521f

Browse files
committed
Simplify adding new swclt_wss_stop() API.
1 parent a9250e8 commit f01521f

3 files changed

Lines changed: 19 additions & 21 deletions

File tree

inc/signalwire-client-c/transport/websocket.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ SWCLT_DECLARE(ks_status_t) swclt_wss_connect(
9797
uint32_t timeout_ms,
9898
const SSL_CTX *ssl);
9999

100+
SWCLT_DECLARE(void) swclt_wss_stop(swclt_wss_t *wss);
100101
SWCLT_DECLARE(void) swclt_wss_destroy(swclt_wss_t **wss);
101102

102103
SWCLT_DECLARE(ks_status_t) swclt_wss_write(swclt_wss_t *wss, char *data);

src/connection.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -672,22 +672,16 @@ SWCLT_DECLARE(void) swclt_conn_destroy(swclt_conn_t **conn)
672672
if ((*conn)->blade_connect_rpl) {
673673
BLADE_CONNECT_RPL_DESTROY(&(*conn)->blade_connect_rpl);
674674
}
675-
/* Stop the websocket reader thread FIRST to prevent new jobs from being
676-
* added to the incoming_frame_pool. The reader thread calls on_incoming_frame
677-
* which adds jobs to the pool, so we must stop it before destroying the pool. */
678-
if ((*conn)->wss && (*conn)->wss->reader_thread) {
679-
ks_thread_request_stop((*conn)->wss->reader_thread);
680-
ks_thread_join((*conn)->wss->reader_thread);
681-
ks_thread_destroy(&(*conn)->wss->reader_thread);
682-
}
683-
/* Now destroy the incoming frame thread pool. This waits for all pending
684-
* jobs to complete. These jobs may call swclt_wss_write() which uses the
685-
* websocket mutex, so we must do this BEFORE destroying the websocket. */
675+
676+
/* 1. Stop websocket reader: prevents new jobs from being queued
677+
* 2. Drain thread pool: lets pending jobs finish (they use wss_mutex)
678+
* 3. Destroy websocket: now safe to free the mutex
679+
*/
680+
681+
swclt_wss_stop((*conn)->wss);
686682
if ((*conn)->incoming_frame_pool) {
687683
ks_thread_pool_destroy(&(*conn)->incoming_frame_pool);
688684
}
689-
/* Now safe to destroy the websocket - no threads are using it anymore.
690-
* swclt_wss_destroy will see reader_thread already stopped/joined. */
691685
swclt_wss_destroy(&(*conn)->wss);
692686
ttl_tracker_destroy(&(*conn)->ttl);
693687
ks_hash_destroy(&(*conn)->outstanding_requests);

src/transport/websocket.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -304,19 +304,22 @@ static ks_status_t __connect_socket(swclt_wss_t *ctx)
304304
return status;
305305
}
306306

307+
SWCLT_DECLARE(void) swclt_wss_stop(swclt_wss_t *wss)
308+
{
309+
if (wss && wss->reader_thread) {
310+
ks_log(KS_LOG_DEBUG, "Stopping websocket reader thread");
311+
ks_thread_request_stop(wss->reader_thread);
312+
ks_thread_join(wss->reader_thread);
313+
ks_thread_destroy(&wss->reader_thread);
314+
}
315+
}
316+
307317
SWCLT_DECLARE(void) swclt_wss_destroy(swclt_wss_t **wss)
308318
{
309319
if (wss && *wss) {
310320
ks_pool_t *pool = (*wss)->pool;
311321
ks_log(KS_LOG_INFO, "Shutting down websocket");
312-
if ((*wss)->reader_thread) {
313-
ks_thread_request_stop((*wss)->reader_thread);
314-
}
315-
316-
if ((*wss)->reader_thread) {
317-
ks_thread_join((*wss)->reader_thread);
318-
ks_thread_destroy(&(*wss)->reader_thread);
319-
}
322+
swclt_wss_stop(*wss);
320323
if ((*wss)->wss_mutex) {
321324
ks_mutex_destroy(&(*wss)->wss_mutex);
322325
}

0 commit comments

Comments
 (0)