fix: drain the connection socket on a protocol error (#1422)

Signed-off-by: Andy Dunstall <andydunstall@hotmail.co.uk>
This commit is contained in:
Andy Dunstall 2023-06-18 06:43:56 +01:00 committed by GitHub
parent 69e6ad799a
commit 7ec5bd912c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View file

@ -505,8 +505,24 @@ void Connection::ConnectionFlow(FiberSocketBase* peer) {
DCHECK(memcache_parser_);
orig_builder->SendProtocolError("bad command line format");
}
error_code ec2 = peer->Shutdown(SHUT_RDWR);
// Shut down the servers side of the socket to send a FIN to the client
// then keep draining the socket (discarding any received data) until
// the client closes the connection.
//
// Otherwise the clients write could fail (or block), so they would never
// read the above protocol error (see issue #1327).
error_code ec2 = peer->Shutdown(SHUT_WR);
LOG_IF(WARNING, ec2) << "Could not shutdown socket " << ec2;
if (!ec2) {
while (true) {
// Discard any received data.
io_buf_.Clear();
if (!peer->Recv(io_buf_.AppendBuffer())) {
break;
}
}
}
FetchBuilderStats(stats_, orig_builder);
}

View file

@ -252,7 +252,7 @@ auto RedisParser::ConsumeArrayLen(Buffer str) -> Result {
return BAD_ARRAYLEN;
case OK:
if (len < -1 || len > kMaxArrayLen) {
VLOG_IF(1, len > kMaxArrayLen) << "Multi bulk len is too big " << len;
LOG_IF(WARNING, len > kMaxArrayLen) << "Multi bulk len is too big " << len;
return BAD_ARRAYLEN;
}