handleClientsWithPendingWrites(): detect dead clients.

This commit is contained in:
antirez 2015-09-28 19:27:51 +02:00
parent 7a7b8bc244
commit 2fe94ee816

View File

@ -816,14 +816,13 @@ void freeClientsInAsyncFreeQueue(void) {
} }
} }
void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) { /* Write data in output buffers to client. Return C_OK if the client
client *c = privdata; * is still valid after the call, C_ERR if it was freed. */
int writeToClient(int fd, client *c) {
ssize_t nwritten = 0, totwritten = 0; ssize_t nwritten = 0, totwritten = 0;
size_t objlen; size_t objlen;
size_t objmem; size_t objmem;
robj *o; robj *o;
UNUSED(el);
UNUSED(mask);
while(c->bufpos > 0 || listLength(c->reply)) { while(c->bufpos > 0 || listLength(c->reply)) {
if (c->bufpos > 0) { if (c->bufpos > 0) {
@ -881,7 +880,7 @@ void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
serverLog(LL_VERBOSE, serverLog(LL_VERBOSE,
"Error writing to client: %s", strerror(errno)); "Error writing to client: %s", strerror(errno));
freeClient(c); freeClient(c);
return; return C_ERR;
} }
} }
if (totwritten > 0) { if (totwritten > 0) {
@ -896,8 +895,19 @@ void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE); aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
/* Close connection after entire reply has been sent. */ /* Close connection after entire reply has been sent. */
if (c->flags & CLIENT_CLOSE_AFTER_REPLY) freeClient(c); if (c->flags & CLIENT_CLOSE_AFTER_REPLY) {
freeClient(c);
return C_ERR;
}
} }
return C_OK;
}
/* Write event handler. Just send data to the client. */
void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
UNUSED(el);
UNUSED(mask);
writeToClient(fd,privdata);
} }
/* This function is called just before entering the event loop, in the hope /* This function is called just before entering the event loop, in the hope
@ -915,7 +925,7 @@ void handleClientsWithPendingWrites(void) {
listDelNode(server.clients_pending_write,ln); listDelNode(server.clients_pending_write,ln);
/* Try to write buffers to the client socket. */ /* Try to write buffers to the client socket. */
sendReplyToClient(server.el,c->fd,c,0); if (writeToClient(c->fd,c) == C_ERR) continue;
/* If there is nothing left, do nothing. Otherwise install /* If there is nothing left, do nothing. Otherwise install
* the write handler. */ * the write handler. */