From de21a5b5b94e1ebacc65f8be27690fe1ec54071a Mon Sep 17 00:00:00 2001
From: Andy Green <andy@warmcat.com>
Date: Tue, 31 Jul 2018 13:21:56 +0800
Subject: [PATCH] protocol_init: make errors fatal

---
 lib/core/context.c                       | 4 +++-
 lib/core/service.c                       | 8 +++++---
 lib/plat/unix/unix-service.c             | 5 ++++-
 lib/roles/http/client/client-handshake.c | 3 ++-
 4 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/lib/core/context.c b/lib/core/context.c
index 5557b814..f5dd745c 100644
--- a/lib/core/context.c
+++ b/lib/core/context.c
@@ -185,7 +185,7 @@ lws_protocol_vh_priv_get(struct lws_vhost *vhost,
 {
 	int n = 0;
 
-	if (!vhost || !vhost->protocol_vh_privs)
+	if (!vhost || !vhost->protocol_vh_privs || !prot)
 		return NULL;
 
 	while (n < vhost->count_protocols && &vhost->protocols[n] != prot)
@@ -316,6 +316,8 @@ lws_protocol_init(struct lws_context *context)
 				vh->protocol_vh_privs[n] = NULL;
 				lwsl_err("%s: protocol %s failed init\n", __func__,
 					 vh->protocols[n].name);
+
+				return 1;
 			}
 		}
 
diff --git a/lib/core/service.c b/lib/core/service.c
index 2f6656bc..cc7faa1c 100644
--- a/lib/core/service.c
+++ b/lib/core/service.c
@@ -553,8 +553,10 @@ lws_service_periodic_checks(struct lws_context *context,
 #endif
 
 	if (!context->protocol_init_done)
-		if (lws_protocol_init(context))
+		if (lws_protocol_init(context)) {
+			lwsl_err("%s: lws_protocol_init failed\n", __func__);
 			return -1;
+		}
 
 	time(&now);
 
@@ -805,7 +807,7 @@ lws_service_periodic_checks(struct lws_context *context,
 	    context->tls_ops->periodic_housekeeping)
 		context->tls_ops->periodic_housekeeping(context, now);
 
-	return timed_out;
+	return 0;
 }
 
 LWS_VISIBLE int
@@ -820,7 +822,7 @@ lws_service_fd_tsi(struct lws_context *context, struct lws_pollfd *pollfd,
 
 	/* the socket we came to service timed out, nothing to do */
 	if (lws_service_periodic_checks(context, pollfd, tsi) || !pollfd)
-		return 0;
+		return -2;
 
 	/* no, here to service a socket descriptor */
 	wsi = wsi_from_fd(context, pollfd->fd);
diff --git a/lib/plat/unix/unix-service.c b/lib/plat/unix/unix-service.c
index 00c1fc92..e61ef599 100644
--- a/lib/plat/unix/unix-service.c
+++ b/lib/plat/unix/unix-service.c
@@ -166,8 +166,11 @@ faked_service:
 		c--;
 
 		m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
-		if (m < 0)
+		if (m < 0) {
+			lwsl_err("%s: lws_service_fd_tsi returned %d\n",
+				 __func__, m);
 			return -1;
+		}
 		/* if something closed, retry this slot */
 		if (m)
 			n--;
diff --git a/lib/roles/http/client/client-handshake.c b/lib/roles/http/client/client-handshake.c
index bda434ff..f64188bb 100644
--- a/lib/roles/http/client/client-handshake.c
+++ b/lib/roles/http/client/client-handshake.c
@@ -572,7 +572,7 @@ send_hs:
 	return wsi;
 
 oom4:
-	if (lwsi_role_client(wsi) && lwsi_state_est(wsi)) {
+	if (lwsi_role_client(wsi) /* && lwsi_state_est(wsi) */) {
 		wsi->protocol->callback(wsi,
 			LWS_CALLBACK_CLIENT_CONNECTION_ERROR,
 			wsi->user_space, (void *)cce, strlen(cce));
@@ -591,6 +591,7 @@ oom4:
 	 */
 	lws_vhost_lock(wsi->vhost);
 	lws_dll_lws_remove(&wsi->dll_active_client_conns);
+	wsi->vhost->context->count_wsi_allocated--;
 	lws_vhost_unlock(wsi->vhost);
 #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
 	lws_header_table_detach(wsi, 0);
-- 
GitLab