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

Code before:
     * will skip directly to the read-from-socket part. */
    if (ret == C_OK) {
        serverAssert(server.repl_transfer_size >= 0);
        return;
    } else if (ret == C_RETRY) {
        serverAssert(server.repl_transfer_size == -1);
        return;
    } else if (ret == C_ERR) {
        serverLog(LL_WARNING, "Failed to read sync metadata");
        cancelReplicationHandshake(1);
        return;
    }

read_from_socket:
    if (server.repl_transfer_size == 0) {
        serverLog(LL_NOTICE, "PRIMARY <-> REPLICA sync: receiving streamed RDB from primary with EOF to parser");
    } else {
        serverLog(LL_NOTICE, "PRIMARY <-> REPLICA sync: receiving %lld bytes from primary to parser", (long long)server.repl_transfer_size);
    }

    replicaBeforeLoadPrimaryRDB(conn, 1);
    if (replicaLoadPrimaryRDBFromSocket(conn, buf, eofmark, &usemark, &rsi) == C_ERR) {
        serverLog(LL_WARNING, "Failed to load RDB");
        cancelReplicationHandshake(1);
        return;
    }
    replicaAfterLoadPrimaryRDB(conn, &rsi);
}

int tryReadBulkPayload(connection *conn, char *buf, int usemark, ssize_t *nread_out) {
    ssize_t readlen, nread;
    off_t left;

    if (usemark) {
        readlen = sizeof(buf[0]) * PROTO_IOBUF_LEN;
    } else {
        left = server.bio_repl_transfer_size - server.bio_repl_transfer_read;
        readlen = (left < (signed)(sizeof(buf[0]) * PROTO_IOBUF_LEN)) ? left : (signed)(sizeof(buf[0]) * PROTO_IOBUF_LEN);
    }

    nread = connRead(conn, buf, readlen);
    if (nread <= 0) {
        if (connGetState(conn) == CONN_STATE_CONNECTED) {
            /* equivalent to EAGAIN */
            memset(buf, 0, PROTO_IOBUF_LEN);
            return C_RETRY;
        }
        replicaBioSaveServerLog(LL_WARNING, "I/O error trying to sync with PRIMARY: %s",
                                (nread == -1) ? connGetLastError(conn) : "connection lost");
        return C_ERR;
    }

Review comment (suggestion):
You can check and confirm. `rdbLoad` might not be propagating back the `RDB_INCOMPATIBLE` code which you rely on below at line 2432

Code after:
                  "Failed trying to rename the temp DB into %s in "
                  "PRIMARY <-> REPLICA synchronization: %s",
                  server.rdb_filename, strerror(errno));
        if (old_rdb_fd != -1) close(old_rdb_fd);
        return C_ERR;
    }
    /* Close old rdb asynchronously. */
    if (old_rdb_fd != -1) bioCreateCloseJob(old_rdb_fd, 0, 0);

    /* Sync the directory to ensure rename is persisted */
    if (fsyncFileDir(server.rdb_filename) == -1) {
        serverLog(LL_WARNING,
                  "Failed trying to sync DB directory %s in "
                  "PRIMARY <-> REPLICA synchronization: %s",
                  server.rdb_filename, strerror(errno));
        return C_ERR;
    }

    /* We will soon start loading the RDB from disk, the replication history is changed,
     * we must discard the cached primary structure and force resync of sub-replicas. */
    replicationAttachToNewPrimary();

    /* We pass RDBFLAGS_EMPTY_DATA to call emptyData() after validating rdb compatibility
     * and before loading the data from the RDB */
    serverLog(LL_NOTICE, "PRIMARY <-> REPLICA sync: Loading DB in memory");
    int retval = rdbLoad(server.rdb_filename, rsi, RDBFLAGS_REPLICATION | RDBFLAGS_EMPTY_DATA);

    if (retval != RDB_OK) {
        serverLog(LL_WARNING, "Failed trying to load the PRIMARY synchronization "
                              "DB from disk, check server logs.");
        if (server.rdb_del_sync_files && allPersistenceDisabled()) {
            serverLog(LL_NOTICE, "Removing the RDB file obtained from "
                                 "the primary. This replica has persistence "
                                 "disabled");
            bg_unlink(server.rdb_filename);
        }

        /* If RDB failed compatibility check, we did not load the new data set or flush our old data. */
        if (retval == RDB_INCOMPATIBLE) {
            serverLog(LL_NOTICE, "PRIMARY <-> REPLICA sync: Skipping flush, no new data was loaded.");
        } else {
            /* If disk-based RDB loading fails, remove the half-loaded dataset. */
            serverLog(LL_NOTICE, "PRIMARY <-> REPLICA sync: Discarding the half-loaded data");
            emptyData(-1, empty_db_flags, replicationEmptyDbCallback);
        }


        /* Note that there's no point in restarting the AOF on sync failure,
         * it'll be restarted when sync succeeds or replica promoted. */
        return C_ERR;
    }