Skip to content

Commit

Permalink
fix: drain the connection socket on a protocol error (#1422)
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Dunstall <andydunstall@hotmail.co.uk>
  • Loading branch information
andydunstall committed Jun 18, 2023
1 parent 69e6ad7 commit 7ec5bd9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/facade/dragonfly_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/facade/redis_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 7ec5bd9

Please sign in to comment.