From 36750b84fdf2f35320c9cf621659d1e880b5e0ca Mon Sep 17 00:00:00 2001
From: David Galeano <davidgaleano@turbulenz.biz>
Date: Wed, 9 Jan 2013 16:17:04 +0800
Subject: [PATCH] Return NULL if the handshake failed to complete,
 libwebsocket_service_fd closes and frees the websocket in that case.

---
 lib/client-handshake.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/lib/client-handshake.c b/lib/client-handshake.c
index ceb5f76f..556b518f 100644
--- a/lib/client-handshake.c
+++ b/lib/client-handshake.c
@@ -138,9 +138,15 @@ struct libwebsocket *__libwebsocket_client_connect_2(
 	wsi->mode = LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE;
 	pfd.fd = wsi->sock;
 	pfd.revents = POLLIN;
-	if (libwebsocket_service_fd(context, &pfd) < 0)
+
+	n = libwebsocket_service_fd(context, &pfd);
+
+	if (n < 0)
 		goto oom4;
 
+	if (n) /* returns 1 on failure after closing wsi */
+		return NULL;
+
 	return wsi;
 
 oom4:
-- 
GitLab