From 0b5f854bf0975eb769dc772d02e4619af5493b61 Mon Sep 17 00:00:00 2001 From: Mitch Hendrickson Date: Mon, 15 Oct 2012 23:09:14 -0700 Subject: [PATCH] Fix 30s timeout when closing a socket that has no remaining data, introduced in 375950f6; setting the socket nonblocking makes recv() return instantly but not select() --- mongoose.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mongoose.c b/mongoose.c index b72fa76a..c42a9759 100644 --- a/mongoose.c +++ b/mongoose.c @@ -511,6 +511,7 @@ struct mg_connection { int throttle; // Throttling, bytes/sec. <= 0 means no throttle time_t last_throttle_time; // Last time throttled data was sent int64_t last_throttle_bytes;// Bytes sent this second + int is_closing; // Flag indicating connection is being torn down }; const char **mg_get_valid_option_names(void) { @@ -1478,7 +1479,7 @@ static int pull(FILE *fp, struct mg_connection *conn, char *buf, int len) { // pipe, fread() may block until IO buffer is filled up. We cannot afford // to block and must pass all read bytes immediately to the client. nread = read(fileno(fp), buf, (size_t) len); - } else if (!wait_until_socket_is_readable(conn)) { + } else if (!conn->is_closing && !wait_until_socket_is_readable(conn)) { nread = -1; } else if (conn->ssl != NULL) { nread = SSL_read(conn->ssl, buf, len); @@ -4515,6 +4516,8 @@ static void close_socket_gracefully(struct mg_connection *conn) { } static void close_connection(struct mg_connection *conn) { + conn->is_closing = 1; + if (conn->ssl) { SSL_free(conn->ssl); conn->ssl = NULL;