Skip to content

Commit 84d5411

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 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 capture the socket error immediately after send()/recv(), before LOG_TRACE can clobber errno / the Winsock last error. EWOULDBLOCK and EAGAIN have the same value on all supported platforms, so a single ErrorWouldBlock check is used (checking both is redundant and trips clang-tidy misc-redundant-expression). Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
1 parent 9d38caf commit 84d5411

2 files changed

Lines changed: 22 additions & 7 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] Move metrics storage test fixtures into anonymous namespace
1922
[#4286](https://github.com/open-telemetry/opentelemetry-cpp/pull/4286)
2023

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,15 +282,19 @@ 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 || recvError != SocketTools::Socket::ErrorWouldBlock)
293+
{
294+
// Orderly shutdown (received == 0) or a real error; a transient would-block is
295+
// retried on the next readable event.
288296
handleConnectionClosed(conn);
289-
return;
290297
}
291-
conn.receiveBuffer.append(buffer, buffer + received);
292-
293-
handleConnection(conn);
294298
}
295299

296300
void onSocketWritable(SocketTools::Socket socket) override
@@ -340,12 +344,20 @@ class HttpServer : private SocketTools::Reactor::SocketCallback
340344
}
341345

342346
int sent = conn.socket.send(conn.sendBuffer.data(), static_cast<int>(conn.sendBuffer.size()));
347+
int sendError = (sent < 0) ? conn.socket.error() : 0;
343348
LOG_TRACE("HttpServer: [%s] sent %d", conn.request.client.c_str(), sent);
344-
if (sent < 0 && conn.socket.error() != SocketTools::Socket::ErrorWouldBlock)
349+
if (sent < 0)
345350
{
351+
if (sendError == SocketTools::Socket::ErrorWouldBlock)
352+
{
353+
// Backpressure: keep the unsent data and wait for the socket to become writable.
354+
m_reactor.addSocket(conn.socket,
355+
SocketTools::Reactor::Writable | SocketTools::Reactor::Closed);
356+
}
357+
// Never erase the buffer on a failed send: a negative count would wipe it.
346358
return true;
347359
}
348-
conn.sendBuffer.erase(0, sent);
360+
conn.sendBuffer.erase(0, static_cast<size_t>(sent));
349361

350362
if (!conn.sendBuffer.empty())
351363
{

0 commit comments

Comments
 (0)