Repository: valkey-io/valkey
File: src/replication.c (C)

Code before:
        serverLog(LL_WARNING, "Unable to connect to PRIMARY: %s", connGetLastError(server.repl_transfer_s));
        connClose(server.repl_transfer_s);
        server.repl_transfer_s = NULL;
        return C_ERR;
    }


    server.repl_transfer_lastio = server.unixtime;
    server.repl_state = REPL_STATE_CONNECTING;
    serverLog(LL_NOTICE, "PRIMARY <-> REPLICA sync started");
    return C_OK;
}

/* In disk-based replication, replica will open a temp db file to store the RDB file.
 * Before entering the REPL_STATE_TRANSFER or after entering the REPL_STATE_TRANSFER,
 * if an error occurs, we need to clean up related resources, such as closing the tmp
 * file fd and deleting the temp file.
 *
 * Noted that repl_transfer_fd and repl_transfer_tmpfile should be set/unset together. */
void cleanupTransferResources(void) {
    if (server.repl_transfer_fd == -1) {
        serverAssert(server.repl_transfer_tmpfile == NULL);
        return;
    }

    serverAssert(server.repl_transfer_fd != -1 && server.repl_transfer_tmpfile != NULL);
    close(server.repl_transfer_fd);
    bg_unlink(server.repl_transfer_tmpfile);
    zfree(server.repl_transfer_tmpfile);
    server.repl_transfer_tmpfile = NULL;
    server.repl_transfer_fd = -1;
}

/* This function can be called when a non blocking connection is currently
 * in progress to undo it.
 * Never call this function directly, use cancelReplicationHandshake() instead.
 */
void undoConnectWithPrimary(void) {
    if (server.repl_transfer_s == NULL) return;

    connClose(server.repl_transfer_s);
    server.repl_transfer_s = NULL;
}

/* Abort the async download of the bulk dataset while SYNC-ing with primary.
 * Never call this function directly, use cancelReplicationHandshake() instead.
 */
void replicationAbortSyncTransfer(void) {
    undoConnectWithPrimary();
    cleanupTransferResources();
}

Review comment (suggestion):
We already do the first part of the compression in the if statment above
```suggestion
    serverAssert(server.repl_transfer_tmpfile != NULL);
```

Code after:
        serverLog(LL_WARNING, "Unable to connect to PRIMARY: %s", connGetLastError(server.repl_transfer_s));
        connClose(server.repl_transfer_s);
        server.repl_transfer_s = NULL;
        return C_ERR;
    }


    server.repl_transfer_lastio = server.unixtime;
    server.repl_state = REPL_STATE_CONNECTING;
    serverLog(LL_NOTICE, "PRIMARY <-> REPLICA sync started");
    return C_OK;
}

/* In disk-based replication, replica will open a temp db file to store the RDB file.
 * Before entering the REPL_STATE_TRANSFER or after entering the REPL_STATE_TRANSFER,
 * if an error occurs, we need to clean up related resources, such as closing the tmp
 * file fd and deleting the temp file.
 *
 * Noted that repl_transfer_fd and repl_transfer_tmpfile should be set/unset together. */
void cleanupTransferResources(void) {
    if (server.repl_transfer_fd == -1) {
        serverAssert(server.repl_transfer_tmpfile == NULL);
        return;
    }

    serverAssert(server.repl_transfer_tmpfile != NULL);
    close(server.repl_transfer_fd);
    bg_unlink(server.repl_transfer_tmpfile);
    zfree(server.repl_transfer_tmpfile);
    server.repl_transfer_tmpfile = NULL;
    server.repl_transfer_fd = -1;
}

/* This function can be called when a non blocking connection is currently
 * in progress to undo it.
 * Never call this function directly, use cancelReplicationHandshake() instead.
 */
void undoConnectWithPrimary(void) {
    if (server.repl_transfer_s == NULL) return;

    connClose(server.repl_transfer_s);
    server.repl_transfer_s = NULL;
}

/* Abort the async download of the bulk dataset while SYNC-ing with primary.
 * Never call this function directly, use cancelReplicationHandshake() instead.
 */
void replicationAbortSyncTransfer(void) {
    undoConnectWithPrimary();
    cleanupTransferResources();
}