redisAsyncDisconnect closes the socket immediately without waiting for replies when RESP3 + SUBSCRIBE is used
Author: sthairnoCreated Dec 31, 2025Updated Dec 31, 2025
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:
https://github.com/redis/hiredis/blob/e834b57f05871c05f7f13e461b78d20907aa3a02/async.c#L454-L468
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)
onDisconnectSource: redis/hiredis