Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions inc/signalwire-client-c/transport/websocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ SWCLT_DECLARE(ks_status_t) swclt_wss_connect(
uint32_t timeout_ms,
const SSL_CTX *ssl);

SWCLT_DECLARE(void) swclt_wss_stop(swclt_wss_t *wss);
SWCLT_DECLARE(void) swclt_wss_destroy(swclt_wss_t **wss);

SWCLT_DECLARE(ks_status_t) swclt_wss_write(swclt_wss_t *wss, char *data);
Expand Down
9 changes: 8 additions & 1 deletion src/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -672,10 +672,17 @@ SWCLT_DECLARE(void) swclt_conn_destroy(swclt_conn_t **conn)
if ((*conn)->blade_connect_rpl) {
BLADE_CONNECT_RPL_DESTROY(&(*conn)->blade_connect_rpl);
}
swclt_wss_destroy(&(*conn)->wss);

/* 1. Stop websocket reader: prevents new jobs from being queued
* 2. Drain thread pool: lets pending jobs finish (they use wss_mutex)
* 3. Destroy websocket: now safe to free the mutex
*/

swclt_wss_stop((*conn)->wss);
if ((*conn)->incoming_frame_pool) {
ks_thread_pool_destroy(&(*conn)->incoming_frame_pool);
}
swclt_wss_destroy(&(*conn)->wss);
ttl_tracker_destroy(&(*conn)->ttl);
ks_hash_destroy(&(*conn)->outstanding_requests);
ks_mutex_destroy(&(*conn)->failed_mutex);
Expand Down
19 changes: 11 additions & 8 deletions src/transport/websocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,19 +312,22 @@ static ks_status_t __connect_socket(swclt_wss_t *ctx)
return status;
}

SWCLT_DECLARE(void) swclt_wss_stop(swclt_wss_t *wss)
{
if (wss && wss->reader_thread) {
ks_log(KS_LOG_DEBUG, "Stopping websocket reader thread");
ks_thread_request_stop(wss->reader_thread);
ks_thread_join(wss->reader_thread);
ks_thread_destroy(&wss->reader_thread);
}
}

SWCLT_DECLARE(void) swclt_wss_destroy(swclt_wss_t **wss)
{
if (wss && *wss) {
ks_pool_t *pool = (*wss)->pool;
ks_log(KS_LOG_INFO, "Shutting down websocket");
if ((*wss)->reader_thread) {
ks_thread_request_stop((*wss)->reader_thread);
}

if ((*wss)->reader_thread) {
ks_thread_join((*wss)->reader_thread);
ks_thread_destroy(&(*wss)->reader_thread);
}
swclt_wss_stop(*wss);
if ((*wss)->wss_mutex) {
ks_mutex_destroy(&(*wss)->wss_mutex);
}
Expand Down
Loading