[Libwebsockets] #331 client application crashes in libwebsocket_context_destroy on flaky network tests

Vikas Gahlan vikas.gahlan at chargepoint.com
Sun Oct 18 08:19:20 CEST 2015

Attached patch work for me.

Here is exact trace flow of the crash:

libwebsocket_client_connect_2: address mywebsocket.com
close: just_kill_connection
remove_wsi_socket_from_fds: wsi=0x7a0d8, sock=9, fds pos=1

below fix works for me:

>From 300e50b2fe6a033a109d5f1f15208cb8490363e4 Mon Sep 17 00:00:00 2001
From: vgahlan-cpi <vikas.gahlan at chargepoint.com>
Date: Sat, 17 Oct 2015 22:40:23 -0700
Subject: [PATCH] fdpolllist cleanup requires if wsi socket exists in fdtable
to avoid double free in libwebsocket_context_destroy

lib/client-handshake.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/lib/client-handshake.c b/lib/client-handshake.c
index 7293a41..d7d72e6 100644
--- a/lib/client-handshake.c
+++ b/lib/client-handshake.c
@@ -291,9 +291,12 @@ struct libwebsocket *libwebsocket_client_connect_2(
        return wsi;

-       lws_free(wsi->u.hdr.ah);
-       lws_free(wsi);
-       return NULL;
+        /*In case wsi added successfully in fd polllist, clean-up requires for fdtable and session data properly using libwebsocket_close_and_free_session().*/
+        if(wsi->position_in_fds_table < 0  || wsi->sock < 0 ){
+                lws_free(wsi->u.hdr.ah);
+                lws_free(wsi);
+                return NULL;
+        }

        libwebsocket_close_and_free_session(context, wsi,

Vikas Gahlan
ChargePoint | chargepoint.com
Ofiice: +91-124-4889450 Ext. 464 | Mobile +91-9873232008

This email and any attachments are intended for the sole use of the intended recipient(s) and
 contain(s) confidential information that may be proprietary, privileged or copyrighted under
 applicable law. If you are not the intended recipient, do not read, copy, or forward this email
 message or any attachments and delete this email message and any attachments immediately.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://libwebsockets.org/pipermail/libwebsockets/attachments/20151018/02dd5043/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-fdpolllist-cleanup-requires-if-wsi-socket-exists-in-.patch
Type: application/octet-stream
Size: 1102 bytes
Desc: 0001-fdpolllist-cleanup-requires-if-wsi-socket-exists-in-.patch
URL: <http://libwebsockets.org/pipermail/libwebsockets/attachments/20151018/02dd5043/attachment.obj>

More information about the Libwebsockets mailing list