Skip to content

Commit

Permalink
Fix minor repldbfd leak in updateReplicasWaitingBgsave if fstat fails (
Browse files Browse the repository at this point in the history
…valkey-io#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 <[email protected]>
  • Loading branch information
enjoy-binbin authored Oct 27, 2024
1 parent 4be09e4 commit 5d2ff85
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/replication.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 5d2ff85

Please sign in to comment.