From 76bfa120eea4910f36d1668715dd3551605769cb Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Sun, 12 Mar 2023 15:08:50 +0000 Subject: [PATCH] Fix CPU pin if clients don't shutdown() their socket properly. If we haven't read any bytes yet, then we try a few times a few ms apart to see if we get anything. If not, treat it as an EOF. Otherwise, read bytes until we get an EOF or EAGAIN. EAGAIN after a consistent read of bytes is treaded as an EOF immediately. --- src/Json.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Json.c b/src/Json.c index 41b3ea8..50ac50d 100644 --- a/src/Json.c +++ b/src/Json.c @@ -25,6 +25,7 @@ #include #include +#include #include #include @@ -705,7 +706,12 @@ JsonFree(HashMap * object) static int JsonConsumeWhitespace(JsonParserState * state) { + static const int nRetries = 5; + static const int delay = 2; + int c; + int tries = 0; + int readFlg = 0; while (1) { @@ -721,7 +727,17 @@ JsonConsumeWhitespace(JsonParserState * state) if (errno == EAGAIN) { clearerr(state->stream); - continue; + tries++; + + if (tries >= nRetries || readFlg) + { + break; + } + else + { + UtilSleepMillis(delay); + continue; + } } else { @@ -729,6 +745,13 @@ JsonConsumeWhitespace(JsonParserState * state) } } + /* As soon as we've successfully read a byte, treat + * future EAGAINs as EOF, because some clients don't + * properly shutdown their sockets. + */ + readFlg = 1; + tries = 0; + if (!isspace(c)) { break;