1

Logs:

OnWrite(): bytes_transferred=343
OnRead(): bytes_transferred=0
OnRead(): Recv: 
HTTP/1.1 200 OK
fail(): OnRead: short read

I am getting short read errors while performing beast::http::async_read operation.

Basically, first I perform async_write operation to send data to server. Then to read that data, I am performing async_read operation.

As the logs suggest, no bytes are read in async_read, resulting in short read error. But I wanted to understand why could it happen?

In async_read,

  • Buffer used: beast::flat_buffer
  • response message used: beast::http::response, I am also clearing the response message in async_read callback.

1 Answers1

1

Without seeing actual code, it's hard to diagnose an issue. Here's some general information that can help you understand the cause and potentially fix the issue.

short_read doesn't happen if there's not enough buffer, it happens when the data received doesn't satisfy the expected amount.

Short reads are normal, see Streams, Short Reads and Short Writes.

So use the appropriate read primitives to handle expected short reads:

Data may be read from or written to a connected TCP socket using the receive(), async_receive(), send() or async_send() member functions. However, as these could result in short writes or reads, an application will typically use the following operations instead: read(), async_read(), write() and async_write().

The usual place where short reads crop up as an error condition is in SSL streams. OpenSSL generates the error condition¹ as stream_errors::stream_truncated:

  • stream_truncated

    The underlying stream closed before the ssl stream gracefully shut down.

It can naturally occur when the peer doesn't negotiate a proper shutdown.

SSLv2 doesn't support a protocol-level shutdown, so an eof will be passed through as eof instead.

Relevant version history

Previous releases fixed bugs related to possible short reads:

Fixed an ssl::stream<> bug that may result in spurious 'short read' errors.

¹ For <v1.1 via SSL_R_SORT_READ

See also

Existing posts on this site, e.g. Short read error-Boost asio synchoronous https call

sehe
  • 374,641
  • 47
  • 450
  • 633