From 94d7d5ebcda2939ad94b419861032ca7b3440183 Mon Sep 17 00:00:00 2001 From: Binbin Date: Tue, 26 Nov 2024 00:00:47 +0800 Subject: [PATCH] Avoid double close on repl_transfer_fd (#1349) The code is ok before 2de544cfcc6d1aa7cf6d0c75a6116f7fc27b6fd6, but now we will set server.repl_transfer_fd right after dfd was initiated, and in here we have a double close error since dfd and server.repl_transfer_fd are the same fd. Also move the declaration of dfd/maxtries to a small scope to avoid the confusion since they are only used in this code. Signed-off-by: Binbin --- src/replication.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/replication.c b/src/replication.c index e49c76788..236b4a1f4 100644 --- a/src/replication.c +++ b/src/replication.c @@ -3332,7 +3332,6 @@ error: * establish a connection with the primary. */ void syncWithPrimary(connection *conn) { char tmpfile[256], *err = NULL; - int dfd = -1, maxtries = 5; int psync_result; /* If this event fired after the user turned the instance into a primary @@ -3601,6 +3600,7 @@ void syncWithPrimary(connection *conn) { /* Prepare a suitable temp file for bulk transfer */ if (!useDisklessLoad()) { + int dfd = -1, maxtries = 5; while (maxtries--) { snprintf(tmpfile, 256, "temp-%d.%ld.rdb", (int)server.unixtime, (long int)getpid()); dfd = open(tmpfile, O_CREAT | O_WRONLY | O_EXCL, 0644); @@ -3657,7 +3657,6 @@ no_response_error: /* Handle receiveSynchronousResponse() error when primary has /* Fall through to regular error handling */ error: - if (dfd != -1) close(dfd); connClose(conn); server.repl_transfer_s = NULL; if (server.repl_rdb_transfer_s) {