Fix minor repldbfd leak in updateReplicasWaitingBgsave if fstat fails (#1226)

In the old code, if fstat fails, replica->repldbfd will hold the
fd and we are doing a free client. And in freeClient, we check and
close only if repl_state == REPLICA_STATE_SEND_BULK. So if fstat
fails, we will leak the fd.

We can also extend freeClient to handle REPLICA_STATE_WAIT_BGSAVE_END
as well, but here seems to be a more friendly (and safer) way.

Signed-off-by: Binbin <binloveplay1314@qq.com>
This commit is contained in:
Binbin 2024-10-27 15:23:00 +08:00 committed by GitHub
parent 4be09e434a
commit 5d2ff853a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1741,6 +1741,7 @@ void updateReplicasWaitingBgsave(int bgsaveerr, int type) {
client *replica = ln->value;
if (replica->repl_state == REPLICA_STATE_WAIT_BGSAVE_END) {
int repldbfd;
struct valkey_stat buf;
if (bgsaveerr != C_OK) {
@ -1790,17 +1791,26 @@ void updateReplicasWaitingBgsave(int bgsaveerr, int type) {
}
replica->repl_start_cmd_stream_on_ack = 1;
} else {
if ((replica->repldbfd = open(server.rdb_filename, O_RDONLY)) == -1 ||
valkey_fstat(replica->repldbfd, &buf) == -1) {
repldbfd = open(server.rdb_filename, O_RDONLY);
if (repldbfd == -1) {
freeClientAsync(replica);
serverLog(LL_WARNING, "SYNC failed. Can't open/stat DB after BGSAVE: %s", strerror(errno));
serverLog(LL_WARNING, "SYNC failed. Can't open DB after BGSAVE: %s", strerror(errno));
continue;
}
if (valkey_fstat(repldbfd, &buf) == -1) {
freeClientAsync(replica);
serverLog(LL_WARNING, "SYNC failed. Can't stat DB after BGSAVE: %s", strerror(errno));
close(repldbfd);
continue;
}
replica->repldbfd = repldbfd;
replica->repldboff = 0;
replica->repldbsize = buf.st_size;
replica->repl_state = REPLICA_STATE_SEND_BULK;
replica->replpreamble = sdscatprintf(sdsempty(), "$%lld\r\n", (unsigned long long)replica->repldbsize);
/* When repl_state changes to REPLICA_STATE_SEND_BULK, we will release
* the resources in freeClient. */
connSetWriteHandler(replica->conn, NULL);
if (connSetWriteHandler(replica->conn, sendBulkToReplica) == C_ERR) {
freeClientAsync(replica);