Skip to content

redisAsyncDisconnect closes the socket immediately without waiting for replies when RESP3 + SUBSCRIBE is used #1320

@sthairno

Description

@sthairno

Hello,
I noticed an issue when using HELLO 3 and SUBSCRIBE on the same async connection. After SUBSCRIBE is executed, calling redisAsyncDisconnect() seems to close the socket without waiting for replies to the commands sent just before the disconnect.
While reading the code, it looks like the condition that decides whether to call __redisAsyncDisconnect does not consider ac->sub.replies.head, so pending reply processes might be ignored during disconnect:

hiredis/async.c

Lines 454 to 468 in e834b57

/* Tries to do a clean disconnect from Redis, meaning it stops new commands
* from being issued, but tries to flush the output buffer and execute
* callbacks for all remaining replies. When this function is called from a
* callback, there might be more replies and we can safely defer disconnecting
* to redisProcessCallbacks(). Otherwise, we can only disconnect immediately
* when there are no pending callbacks. */
void redisAsyncDisconnect(redisAsyncContext *ac) {
redisContext *c = &(ac->c);
c->flags |= REDIS_DISCONNECTING;
/** unset the auto-free flag here, because disconnect undoes this */
c->flags &= ~REDIS_NO_AUTO_FREE;
if (!(c->flags & REDIS_IN_CALLBACK) && ac->replies.head == NULL)
__redisAsyncDisconnect(ac);
}

Reproduction code

#include <hiredis/hiredis.h>
#include <hiredis/adapters/poll.h>

#include <stdio.h>
#include <string.h>

static int connected = 0;
static int disconnected = 0;
static int hello = 0;
static int subscribe = 0;
static int ping = 0;

static void onConnect(redisAsyncContext*, int) {
	connected = 1;
	printf("onConnect\n");
}

static void onDisconnect(const redisAsyncContext*, int) {
	disconnected = 1;
	printf("onDisconnect\n");
}

static void onHello(redisAsyncContext*, void* reply, void*) {
	hello = 1;
	printf("onHello: %p\n", reply);
}

static void onSubscribe(redisAsyncContext*, void* reply, void*) {
	subscribe = 1;
	printf("onSubscribe: %p\n", reply);
}

static void onPing(redisAsyncContext*, void* reply, void*) {
	printf("onPing: %p\n", reply);
}

int main(int argc, char** argv) {
	const int sub = (argc >= 2) && (strcmp(argv[1], "sub") == 0);

	printf("[redisAsyncConnect]\n");
	redisAsyncContext* ac = redisAsyncConnect("127.0.0.1", 6379);

	redisPollAttach(ac);
	redisAsyncSetConnectCallback(ac, onConnect);
	redisAsyncSetDisconnectCallback(ac, onDisconnect);
	while(!connected) { redisPollTick(ac, 0.01); }

	printf("[redisAsyncCommand: HELLO 3]\n");
	redisAsyncCommand(ac, onHello, NULL, "HELLO 3");
	while(!hello) { redisPollTick(ac, 0.01); }

	if (sub) {
		printf("[redisAsyncCommand: SUBSCRIBE foo]\n");
		redisAsyncCommand(ac, onSubscribe, NULL, "SUBSCRIBE foo"); 
		while(!subscribe) { redisPollTick(ac, 0.01); }
	}

	printf("[redisAsyncCommand: PING]\n");
	redisAsyncCommand(ac, onPing, NULL, "PING");

	printf("[redisAsyncDisconnect]\n");
	redisAsyncDisconnect(ac);

	while(!disconnected) { redisPollTick(ac, 0.01); }

	return 0;
}

Outputs

$ ./disconnect
[redisAsyncConnect]
onConnect
[redisAsyncCommand: HELLO 3]
onHello: 0x557c771b10c0
[redisAsyncCommand: PING]
[redisAsyncDisconnect]
onPing: 0x557c771b10c0
onDisconnect
$ ./disconnect sub
[redisAsyncConnect]
onConnect
[redisAsyncCommand: HELLO 3]
onHello: 0x559d83b830c0
[redisAsyncCommand: SUBSCRIBE foo]
onSubscribe: 0x559d83b830c0
[redisAsyncCommand: PING]
[redisAsyncDisconnect]
onPing: (nil)
onSubscribe: (nil)
onDisconnect

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions