@@ -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 );
0 commit comments