From 01c08b508927adfb9ca6857db076c849f945e1be Mon Sep 17 00:00:00 2001
From: antirez <antirez@gmail.com>
Date: Wed, 30 Sep 2015 17:23:34 +0200
Subject: [PATCH] Fix processEventsWhileBlocked() to handle PENDING_WRITE
 clients.

After the introduction of the list with clients with pending writes, to
process clients incrementally outside of the event loop we also need to
process the pending writes list.
---
 src/networking.c | 8 ++++++--
 src/server.h     | 2 +-
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/networking.c b/src/networking.c
index 6dc4864b..7ec95676 100644
--- a/src/networking.c
+++ b/src/networking.c
@@ -938,9 +938,10 @@ void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
  * we can just write the replies to the client output buffer without any
  * need to use a syscall in order to install the writable event handler,
  * get it called, and so forth. */
-void handleClientsWithPendingWrites(void) {
+int handleClientsWithPendingWrites(void) {
     listIter li;
     listNode *ln;
+    int processed = listLength(server.clients_pending_write);
 
     listRewind(server.clients_pending_write,&li);
     while((ln = listNext(&li))) {
@@ -960,6 +961,7 @@ void handleClientsWithPendingWrites(void) {
             freeClientAsync(c);
         }
     }
+    return processed;
 }
 
 /* resetClient prepare the client to process the next command */
@@ -1821,7 +1823,9 @@ int processEventsWhileBlocked(void) {
     int iterations = 4; /* See the function top-comment. */
     int count = 0;
     while (iterations--) {
-        int events = aeProcessEvents(server.el, AE_FILE_EVENTS|AE_DONT_WAIT);
+        int events = 0;
+        events += aeProcessEvents(server.el, AE_FILE_EVENTS|AE_DONT_WAIT);
+        events += handleClientsWithPendingWrites();
         if (!events) break;
         count += events;
     }
diff --git a/src/server.h b/src/server.h
index b5a24829..4c4017f7 100644
--- a/src/server.h
+++ b/src/server.h
@@ -1110,7 +1110,7 @@ int listenToPort(int port, int *fds, int *count);
 void pauseClients(mstime_t duration);
 int clientsArePaused(void);
 int processEventsWhileBlocked(void);
-void handleClientsWithPendingWrites(void);
+int handleClientsWithPendingWrites(void);
 int clientHasPendingReplies(client *c);
 void unlinkClient(client *c);