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

Code before:
    c->flag.reply_skip = 0;
    if (c->flag.reply_skip_next) {
        c->flag.reply_skip = 1;
        c->flag.reply_skip_next = 0;
    }
}

/* Initializes the shared query buffer to a new sds with the default capacity.
 * Need to ensure the initlen is not less than readlen in readToQueryBuf. */
void initSharedQueryBuf(void) {
    thread_shared_qb = sdsnewlen(NULL, PROTO_IOBUF_LEN);
    sdsclear(thread_shared_qb);
}

void freeSharedQueryBuf(void) {
    sdsfree(thread_shared_qb);
    thread_shared_qb = NULL;
}

/* This function is used when we want to re-enter the event loop but there
 * is the risk that the client we are dealing with will be freed in some
 * way. This happens for instance in:
 *
 * * DEBUG RELOAD and similar.
 * * When a Lua script is in -BUSY state.
 * * A cluster replica doing CLUSTER SETSLOT and doing migration.
 *
 * So the function will protect the client by doing two things:
 *
 * 1) It removes the file events. This way it is not possible that an
 *    error is signaled on the socket, freeing the client.
 * 2) Moreover it makes sure that if the client is freed in a different code
 *    path, it is not really released, but only marked for later release. */
void protectClient(client *c) {
    c->flag.protected = 1;
    if (c->conn) {
        connSetReadHandler(c->conn, NULL);
        connSetWriteHandler(c->conn, NULL);
    }
}

/* This will undo the client protection done by protectClient() */
void unprotectClient(client *c) {
    if (c->flag.protected) {
        c->flag.protected = 0;
        if (c->conn) {
            connSetReadHandler(c->conn, readQueryFromClient);
            if (clientHasPendingReplies(c)) putClientInPendingWriteQueue(c);
        }
    }
}

Review comment (suggestion):
```suggestion
 * * A cluster replica executing CLUSTER SETSLOT during slot migration.
```

Code after:
    c->flag.reply_skip = 0;
    if (c->flag.reply_skip_next) {
        c->flag.reply_skip = 1;
        c->flag.reply_skip_next = 0;
    }
}

/* Initializes the shared query buffer to a new sds with the default capacity.
 * Need to ensure the initlen is not less than readlen in readToQueryBuf. */
void initSharedQueryBuf(void) {
    thread_shared_qb = sdsnewlen(NULL, PROTO_IOBUF_LEN);
    sdsclear(thread_shared_qb);
}

void freeSharedQueryBuf(void) {
    sdsfree(thread_shared_qb);
    thread_shared_qb = NULL;
}

/* This function is used when we want to re-enter the event loop but there
 * is the risk that the client we are dealing with will be freed in some
 * way. This happens for instance in:
 *
 * * DEBUG RELOAD and similar.
 * * When a Lua script is in -BUSY state.
 * * A cluster replica executing CLUSTER SETSLOT during slot migration.
 *
 * So the function will protect the client by doing two things:
 *
 * 1) It removes the file events. This way it is not possible that an
 *    error is signaled on the socket, freeing the client.
 * 2) Moreover it makes sure that if the client is freed in a different code
 *    path, it is not really released, but only marked for later release. */
void protectClient(client *c) {
    c->flag.protected = 1;
    if (c->conn) {
        connSetReadHandler(c->conn, NULL);
        connSetWriteHandler(c->conn, NULL);
    }
}

/* This will undo the client protection done by protectClient() */
void unprotectClient(client *c) {
    if (c->flag.protected) {
        c->flag.protected = 0;
        if (c->conn) {
            connSetReadHandler(c->conn, readQueryFromClient);
            if (clientHasPendingReplies(c)) putClientInPendingWriteQueue(c);
        }
    }
}