Skip to content

[BUG] Embedded HTTP server (ext/http) drops the pending response when a nonblocking send would block #4281

Closed as not planned

Description

@thc1006

What

HttpServer::sendMore() in ext/include/opentelemetry/ext/http/server/http_server.h mishandles a nonblocking send() that returns -1 with EWOULDBLOCK/EAGAIN:

int sent = conn.socket.send(...);
LOG_TRACE(..., sent);
if (sent < 0 && conn.socket.error() != SocketTools::Socket::ErrorWouldBlock)
{
  return true;
}
conn.sendBuffer.erase(0, sent);   // sent == -1 -> erase(0, (size_t)-1) wipes the whole buffer

When the socket is not writable (backpressure), send() returns -1 with EWOULDBLOCK. The early return is skipped, because its condition requires the error to not be would-block, so control falls through to erase(0, sent) with sent == -1. std::string::erase(pos, count) takes count as size_type, so -1 becomes SIZE_MAX and the entire sendBuffer is erased. The unsent part of the HTTP response is silently lost, and because the buffer is now empty the connection is treated as if the response was fully sent.

Related: error code read after logging

conn.socket.error() (which reads errno on POSIX / WSAGetLastError() on Windows) is evaluated after LOG_TRACE, which may call printf and clobber the error code. WSAGetLastError() in particular is documented to be read immediately after the failing socket call. The error should be captured right after send().

Impact

Truncated or lost HTTP responses under write backpressure in the embedded server. It is a mock server ("out of scope: performance"), so this mostly affects larger responses on slow or full sockets, but it is a correctness bug and this header is installed with opentelemetry_ext.

Suggested fix

Capture the error before logging, never erase on sent < 0, and re-register for writability on a genuine would-block:

int sent      = conn.socket.send(...);
int sendError = (sent < 0) ? conn.socket.error() : 0;
LOG_TRACE(..., sent);
if (sent < 0)
{
  if (sendError == ErrorWouldBlock || sendError == ErrorAgain)
  {
    m_reactor.addSocket(conn.socket, Writable | Closed);  // keep the buffer, wait for writability
  }
  return true;
}
conn.sendBuffer.erase(0, static_cast<size_t>(sent));

EAGAIN and EWOULDBLOCK may be distinct values on some POSIX systems, and Socket currently only defines ErrorWouldBlock, so an ErrorAgain alias is also needed.

Found while reviewing #4280. A PR will follow.

Activity

  1. added
    needs-triage
    on Jul 23, 2026
  2. added 6 commits that reference this issue on Jul 23, 2026
    d294ce8
    3af9f11
    171b8e0
    84d5411
    b48b268
    817f73e
  3. thc1006 commented on Aug 3, 2026

    @thc1006
    MemberAuthor

    Closing after @dbarker set the scope for these files on #4287. http_server.h and socket_tools.h are internal, supporting the tests and a single example, and the bar for them is being correct enough for that rather than production hardening. #4283, which would have fixed this, is closed for the same reason. Easy to reopen if it ever shows up in the tests or the example.

  4. added
    triage/not-a-bug
    and removed
    needs-triage
    on Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage/not-a-bug

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions