From 531b82df22d30f4b996351a86b29de8bb56c06bb Mon Sep 17 00:00:00 2001 From: Yi Deng <151997860+DengY11@users.noreply.github.com> Date: Fri, 27 Jun 2025 09:07:33 +0800 Subject: [PATCH] Fix replication lseek check (#14135) In `sendBulkToSlave`, the `lseek()` call used to position the RDB file descriptor before reading the next data chunk was not checked for errors. If the `lseek()` system call were to fail, the file descriptor would remain at an incorrect position. The subsequent `read()` would then fetch the wrong data, leading to a corrupted RDB stream being sent to the replica. This could cause the replication to fail or result in data inconsistency. This patch introduces a check for the `lseek()` return value. On failure, it logs a detailed warning and aborts the replication by freeing the client, mirroring the existing error handling for `read()` and `write()` calls within the same function. This improves the robustness of the RDB transfer process. --------- Co-authored-by: Yuan Wang --- src/replication.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/replication.c b/src/replication.c index ed4376748..247faa0b0 100644 --- a/src/replication.c +++ b/src/replication.c @@ -1574,7 +1574,12 @@ void sendBulkToSlave(connection *conn) { } /* If the preamble was already transferred, send the RDB bulk data. */ - lseek(slave->repldbfd,slave->repldboff,SEEK_SET); + if (lseek(slave->repldbfd,slave->repldboff,SEEK_SET) == -1) { + serverLog(LL_WARNING,"Failed to lseek the RDB file to offset %lld for replica %s: %s", + (long long)slave->repldboff, replicationGetSlaveName(slave), strerror(errno)); + freeClient(slave); + return; + } buflen = read(slave->repldbfd,buf,PROTO_IOBUF_LEN); if (buflen <= 0) { serverLog(LL_WARNING,"Read error sending DB to replica: %s",