Skip to content

Commit 9fff8f3

Browse files
committed
Simplify adding new swclt_wss_stop() API.
1 parent e1ca286 commit 9fff8f3

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
@@ -312,19 +312,22 @@ static ks_status_t __connect_socket(swclt_wss_t *ctx)
312312
return status;
313313
}
314314

315+
SWCLT_DECLARE(void) swclt_wss_stop(swclt_wss_t *wss)
316+
{
317+
if (wss && wss->reader_thread) {
318+
ks_log(KS_LOG_DEBUG, "Stopping websocket reader thread");
319+
ks_thread_request_stop(wss->reader_thread);
320+
ks_thread_join(wss->reader_thread);
321+
ks_thread_destroy(&wss->reader_thread);
322+
}
323+
}
324+
315325
SWCLT_DECLARE(void) swclt_wss_destroy(swclt_wss_t **wss)
316326
{
317327
if (wss && *wss) {
318328
ks_pool_t *pool = (*wss)->pool;
319329
ks_log(KS_LOG_INFO, "Shutting down websocket");
320-
if ((*wss)->reader_thread) {
321-
ks_thread_request_stop((*wss)->reader_thread);
322-
}
323-
324-
if ((*wss)->reader_thread) {
325-
ks_thread_join((*wss)->reader_thread);
326-
ks_thread_destroy(&(*wss)->reader_thread);
327-
}
330+
swclt_wss_stop(*wss);
328331
if ((*wss)->wss_mutex) {
329332
ks_mutex_destroy(&(*wss)->wss_mutex);
330333
}

0 commit comments

Comments
 (0)