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 <binloveplay1314@qq.com>
This commit is contained in:
Binbin 2024-11-26 00:00:47 +08:00 committed by Madelyn Olson
parent d6e9ff1f85
commit 94d7d5ebcd

View File

@ -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) {