From 140ac6e9cb0965bd7aea7f37a19c3a68ed7b5e7d Mon Sep 17 00:00:00 2001
From: Andrejs Hanins <ahanins@gmail.com>
Date: Fri, 6 Nov 2015 18:18:32 +0200
Subject: [PATCH] Subject: [PATCH] Fix for close ack sending

It was forgotten in two places that pending close ack should be
processed when wsi state is WSI_STATE_RETURNED_CLOSE_ALREADY, but
not WSI_STATE_ESTABLISHED. As a result, close ack wasn't sent out
to the peer.
---
 lib/output.c  | 4 +++-
 lib/service.c | 6 ++++--
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/lib/output.c b/lib/output.c
index 977d1de4..44b2ab7e 100644
--- a/lib/output.c
+++ b/lib/output.c
@@ -270,7 +270,9 @@ LWS_VISIBLE int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf,
 
 	/* websocket protocol, either binary or text */
 
-	if (wsi->state != WSI_STATE_ESTABLISHED)
+	if (wsi->state != WSI_STATE_ESTABLISHED &&
+	    !(wsi->state == WSI_STATE_RETURNED_CLOSE_ALREADY &&
+	      protocol == LWS_WRITE_CLOSE))
 		return -1;
 
 	/* if we are continuing a frame that already had its header done */
diff --git a/lib/service.c b/lib/service.c
index 1571a198..32e09025 100644
--- a/lib/service.c
+++ b/lib/service.c
@@ -93,8 +93,10 @@ lws_handle_POLLOUT_event(struct libwebsocket_context *context,
 #endif
 	/* pending control packets have next priority */
 	
-	if (wsi->state == WSI_STATE_ESTABLISHED &&
-	    wsi->u.ws.ping_pending_flag) {
+	if ((wsi->state == WSI_STATE_ESTABLISHED &&
+	     wsi->u.ws.ping_pending_flag) ||
+	    (wsi->state == WSI_STATE_RETURNED_CLOSE_ALREADY &&
+	     wsi->u.ws.payload_is_close)) {
 
 		if (wsi->u.ws.payload_is_close)
 			write_type = LWS_WRITE_CLOSE;
-- 
GitLab