Search code examples
c++blockinghiredis

How to cancel hiredis blocking operation


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.


Solution

  • 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.