Merge pull request #31712 from Avogar/fix-write-after-finalize

Fix possible Logical error: Cannot write to finalized buffer
This commit is contained in:
Kruglov Pavel 2021-11-25 13:43:14 +03:00 committed by GitHub
commit 5f17109a84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -929,6 +929,15 @@ void HTTPHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse
request_credentials.reset(); // ...so that the next requests on the connection have to always start afresh in case of exceptions.
});
/// Check if exception was thrown in used_output.finalize().
/// In this case used_output can be in invalid state and we
/// cannot write in it anymore. So, just log this exception.
if (used_output.isFinalized())
{
tryLogCurrentException(log, "Cannot flush data to client");
return;
}
tryLogCurrentException(log);
/** If exception is received from remote server, then stack trace is embedded in message.

View File

@ -60,13 +60,19 @@ private:
/// Points to 'out' or to CompressedWriteBuffer(*out) or to CascadeWriteBuffer.
std::shared_ptr<WriteBuffer> out_maybe_delayed_and_compressed;
bool finalized = false;
inline bool hasDelayed() const
{
return out_maybe_delayed_and_compressed != out_maybe_compressed;
}
inline void finalize() const
inline void finalize()
{
if (finalized)
return;
finalized = true;
if (out_maybe_delayed_and_compressed)
out_maybe_delayed_and_compressed->finalize();
if (out_maybe_compressed)
@ -74,6 +80,11 @@ private:
if (out)
out->finalize();
}
inline bool isFinalized() const
{
return finalized;
}
};
IServer & server;