How to cancel hiredis blocking operation

1k Views Asked by At

Having a blocking subscription running like below halts the program at redisGetReply (source: hiredis#pipelining)

void subscribe (std::string& key, Subscriber* subscriber)
{
    void* reply = redisCommand (redis, "SUBSCRIBE %s", key.c_str ());
    freeReplyObject (reply);
    while (redisGetReply (redis, &reply) == REDIS_OK)
    {
        subscriber -> notify ();
        freeReplyObject (reply);
    }
}

I thought by invoking redisFree (thru signal handling) the socket would be closed and redisGetReply returned, as mentioned at hiredis#cleaning-up, instead it throws a memory access violation.

1

There are 1 best solutions below

1
On BEST ANSWER

Okay, nevermind. I managed to simply close the file descriptor which is being used by hiredis by invoking...

close (redis -> fd);

... and redisGetReply returns properly.

Alternatively, one could send a QUIT command and the blocking subscription returns.