Skip to content

Explicitly drain the leftover bytes in buffer for the POST request in HTTPHander#81461

Closed
nikitamikhaylov wants to merge 2 commits intomasterfrom
drain-the-buffer
Closed

Explicitly drain the leftover bytes in buffer for the POST request in HTTPHander#81461
nikitamikhaylov wants to merge 2 commits intomasterfrom
drain-the-buffer

Conversation

@nikitamikhaylov
Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Previously, the error Invalid HTTP version string: /?query=I might have occurred while inserting some data over HTTP. This happened because ClickHouse didn't fully consume the input bytes (\r\n0\r\n\r\n) and reused the connection for the subsequent requests. This behaviour is now fixed.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh bot commented Jun 6, 2025

Workflow [PR], commit [d73fe9d]

@clickhouse-gh clickhouse-gh bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 6, 2025
@nikitamikhaylov
Copy link
Copy Markdown
Member Author

Kudos to @vdimir for writing the test!

@nikitamikhaylov nikitamikhaylov added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Jun 6, 2025
{
String remaining;
readStringUntilEOF(remaining, *in_post_maybe_compressed);
auto hex_string = hexString(remaining.data(), remaining.size());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to shorten this to a substring, otherwise it can print a lot e.g. if the query is cancelled with memory limit exceeded.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More generally, maybe it should only print the data if the query was successful and having data still in the buffer is unexpected?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, if query fails executeQuery above throws exception and we don't get here.

/// 3. Make HTTPChunkedReadBuffer eagerly drain the final \r\n0\r\n\r\n before returning the final bytes of data,
// but that seems very inconvenient to implement because of how zero-copying is done in HTTPChunkedReadBuffer::nextImpl()
/// You can find an option (2) implemented below.
if (in_post_maybe_compressed->available())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

available() is data that happens to be already in the buffer. Don't we need to actually read from socket too (including blocking reads), if the expected final bytes weren't received yet? E.g. if the sender sleeps for a second after sending all the data but before sending the final \r\n0\r\n\r\n.

I think we can make HTTPServerRequest constructor set a flag saying whether the buffer needs to be drained (in the getChunkedTransferEncoding() and hasContentLength() cases, but not in the POST-of-unknown-size case since we wouldn't know how much to drain). Then here we'd do readStringUntilEOF if that flag is set.

Copy link
Copy Markdown
Member

@al13n321 al13n321 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should do #81561 instead.

@nikitamikhaylov
Copy link
Copy Markdown
Member Author

@al13n321 Feel free to close this one, but don't forget to write a test / use the one from the current PR.

@CheSema CheSema mentioned this pull request Jun 10, 2025
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants