Skip to content

Commit a9250e8

Browse files
committed
Fix use-after-free crash in websocket mutex during connection teardown
1 parent 7dd7f39 commit a9250e8

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

src/connection.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,10 +672,23 @@ 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-
swclt_wss_destroy(&(*conn)->wss);
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. */
676686
if ((*conn)->incoming_frame_pool) {
677687
ks_thread_pool_destroy(&(*conn)->incoming_frame_pool);
678688
}
689+
/* Now safe to destroy the websocket - no threads are using it anymore.
690+
* swclt_wss_destroy will see reader_thread already stopped/joined. */
691+
swclt_wss_destroy(&(*conn)->wss);
679692
ttl_tracker_destroy(&(*conn)->ttl);
680693
ks_hash_destroy(&(*conn)->outstanding_requests);
681694
ks_mutex_destroy(&(*conn)->failed_mutex);

0 commit comments

Comments
 (0)