From 890de52b5c323cb0259b01def4f123ed333a1e76 Mon Sep 17 00:00:00 2001 From: VivekSainiEQ Date: Tue, 2 Nov 2021 17:43:17 +0000 Subject: [PATCH] Check that cluster connection is valid before setting write handler Former-commit-id: 6a0a005ff2587c1c2647d3a077472b8bca38a074 --- src/cluster.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cluster.cpp b/src/cluster.cpp index 4f1cc7710..29b27ac44 100644 --- a/src/cluster.cpp +++ b/src/cluster.cpp @@ -2447,7 +2447,10 @@ void clusterSendMessage(clusterLink *link, unsigned char *msg, size_t msglen) { if (sdslen(link->sndbuf) == 0 && msglen != 0) { aePostFunction(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el, [link] { - connSetWriteHandlerWithBarrier(link->conn, clusterWriteHandler, 1); + /* The connection could be timed out before this posted function executes (thanks to TCP keepalive). + * So check that the connection is still there before setting the write handler, otherwise you segfault */ + if (link->conn != nullptr) + connSetWriteHandlerWithBarrier(link->conn, clusterWriteHandler, 1); }); }