Skip to content

Commit d294ce8

Browse files
committed
[BUG] Fix nonblocking send/recv handling in the ext/http embedded server
The embedded HttpServer mishandled nonblocking send()/recv() results: - sendMore() fell through to sendBuffer.erase(0, sent) when send() returned -1 (EWOULDBLOCK). With sent == -1 the erase count becomes SIZE_MAX and the whole unsent response is wiped. It now never erases on a failed send, and re-registers for writability on a genuine would-block so the buffer is kept. Fixes #4281. - onSocketReadable() closed the connection on any recv() <= 0, including the transient -1/EWOULDBLOCK case. It now closes only on an orderly shutdown (recv() == 0) or a real error, and ignores a transient would-block. Fixes #4282. - Both paths now capture the socket error immediately after send()/recv(), before LOG_TRACE can clobber errno / the Winsock last error. Add a Socket::ErrorAgain alias (EAGAIN, or WSAEWOULDBLOCK on Windows), since EAGAIN and EWOULDBLOCK may be distinct values on some POSIX systems. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
1 parent 780d404 commit d294ce8

3 files changed

Lines changed: 32 additions & 9 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Increment the:
1515

1616
## [Unreleased]
1717

18+
* [BUG] Fix nonblocking send/recv handling in the ext/http embedded server
19+
[#4283](https://github.com/open-telemetry/opentelemetry-cpp/pull/4283)
20+
1821
* [CODE HEALTH] Fix more clang tidy warnings (member initialization)
1922
[#4270](https://github.com/open-telemetry/opentelemetry-cpp/pull/4270)
2023

‎ext/include/opentelemetry/ext/http/server/http_server.h‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,15 +282,24 @@ class HttpServer : private SocketTools::Reactor::SocketCallback
282282

283283
char buffer[2048] = {0};
284284
int received = socket.recv(buffer, sizeof(buffer));
285+
int recvError = (received < 0) ? socket.error() : 0;
285286
LOG_TRACE("HttpServer: [%s] received %d", conn.request.client.c_str(), received);
286-
if (received <= 0)
287+
if (received > 0)
287288
{
289+
conn.receiveBuffer.append(buffer, buffer + received);
290+
handleConnection(conn);
291+
}
292+
else if (received == 0)
293+
{
294+
// The peer performed an orderly shutdown.
295+
handleConnectionClosed(conn);
296+
}
297+
else if (recvError != SocketTools::Socket::ErrorWouldBlock &&
298+
recvError != SocketTools::Socket::ErrorAgain)
299+
{
300+
// A real receive error; a transient would-block is retried on the next readable event.
288301
handleConnectionClosed(conn);
289-
return;
290302
}
291-
conn.receiveBuffer.append(buffer, buffer + received);
292-
293-
handleConnection(conn);
294303
}
295304

296305
void onSocketWritable(SocketTools::Socket socket) override
@@ -339,12 +348,21 @@ class HttpServer : private SocketTools::Reactor::SocketCallback
339348
}
340349

341350
int sent = conn.socket.send(conn.sendBuffer.data(), static_cast<int>(conn.sendBuffer.size()));
351+
int sendError = (sent < 0) ? conn.socket.error() : 0;
342352
LOG_TRACE("HttpServer: [%s] sent %d", conn.request.client.c_str(), sent);
343-
if (sent < 0 && conn.socket.error() != SocketTools::Socket::ErrorWouldBlock)
353+
if (sent < 0)
344354
{
355+
if (sendError == SocketTools::Socket::ErrorWouldBlock ||
356+
sendError == SocketTools::Socket::ErrorAgain)
357+
{
358+
// Backpressure: keep the unsent data and wait for the socket to become writable.
359+
m_reactor.addSocket(conn.socket,
360+
SocketTools::Reactor::Writable | SocketTools::Reactor::Closed);
361+
}
362+
// Never erase the buffer on a failed send: a negative count would wipe it.
345363
return true;
346364
}
347-
conn.sendBuffer.erase(0, sent);
365+
conn.sendBuffer.erase(0, static_cast<size_t>(sent));
348366

349367
if (!conn.sendBuffer.empty())
350368
{

‎ext/include/opentelemetry/ext/http/server/socket_tools.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,11 @@ struct Socket
411411
enum // NOLINT(performance-enum-size)
412412
{
413413
#ifdef _WIN32
414-
ErrorWouldBlock = WSAEWOULDBLOCK
414+
ErrorWouldBlock = WSAEWOULDBLOCK,
415+
ErrorAgain = WSAEWOULDBLOCK // Winsock has no EAGAIN; treat it as a would-block.
415416
#else
416-
ErrorWouldBlock = EWOULDBLOCK
417+
ErrorWouldBlock = EWOULDBLOCK,
418+
ErrorAgain = EAGAIN
417419
#endif
418420
};
419421

0 commit comments

Comments
 (0)