Skip to content

Commit c4d3404

Browse files
Handle EINTR safely in client read path
Agent-Logs-Url: https://github.com/WEBcodeX1/http-1.2/sessions/52de29c4-0332-4c1e-9fd4-e87a34608c1f Co-authored-by: clauspruefer <17313789+clauspruefer@users.noreply.github.com>
1 parent 25a3224 commit c4d3404

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/Client.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "Client.hpp"
22

3+
#include <cerrno>
4+
35
using namespace std;
46

57
Client::Client(Filedescriptor_t ClientFD, char* BufferAddress) :
@@ -20,7 +22,11 @@ Client::~Client()
2022

2123
ssize_t Client::receiveData()
2224
{
23-
ssize_t RcvBytes = read(_ClientFD, _ReceiveBuffer, SOCKET_RECEIVE_BUFFER_SIZE);
25+
ssize_t RcvBytes;
26+
do {
27+
RcvBytes = read(_ClientFD, _ReceiveBuffer, SOCKET_RECEIVE_BUFFER_SIZE);
28+
} while (RcvBytes == -1 && errno == EINTR);
29+
2430
DBG(220, "RcvBytes:" << RcvBytes << " ClientFD:" << _ClientFD);
2531

2632
if (RcvBytes > 0) {

src/ClientHandler.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "ClientHandler.hpp"
22

3+
#include <cerrno>
34
#include <memory>
45

56
using namespace std;
@@ -83,9 +84,18 @@ void ClientHandler::readClientData(const uint16_t FDCount)
8384

8485
if (Clients.contains(ReadFD)) {
8586
const auto RecvStatus = Clients[ReadFD]->receiveData();
87+
const int RecvErrno = errno;
8688
if (RecvStatus == 0) {
8789
Clients.erase(ReadFD);
8890
close(ReadFD);
91+
} else if (
92+
RecvStatus < 0 &&
93+
RecvErrno != EAGAIN &&
94+
RecvErrno != EWOULDBLOCK &&
95+
RecvErrno != EINTR
96+
) {
97+
Clients.erase(ReadFD);
98+
close(ReadFD);
8999
}
90100
}
91101
}

0 commit comments

Comments
 (0)