forked from Telodendria/Telodendria
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.
This commit is contained in:
parent
62cd1cdc98
commit
76bfa120ee
1 changed files with 24 additions and 1 deletions
25
src/Json.c
25
src/Json.c
|
@ -25,6 +25,7 @@
|
|||
|
||||
#include <Memory.h>
|
||||
#include <Str.h>
|
||||
#include <Util.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue