diff --git a/rapid/plugin/group_replication/libmysqlgcs/src/bindings/xcom/xcom/xcom_base.c b/rapid/plugin/group_replication/libmysqlgcs/src/bindings/xcom/xcom/xcom_base.c index a05b9f38508e..34124e940c99 100644 --- a/rapid/plugin/group_replication/libmysqlgcs/src/bindings/xcom/xcom/xcom_base.c +++ b/rapid/plugin/group_replication/libmysqlgcs/src/bindings/xcom/xcom/xcom_base.c @@ -4006,7 +4006,11 @@ int acceptor_learner_task(task_arg arg) common to both the sender_task, reply_handler_task, and the ac‐ ceptor_learner_task. */ + // Allow the previous server reference to be freed. + if (ep->srv) srv_unref(ep->srv); ep->srv = get_server(site, ep->p->from); + // Prevent the new server reference from being freed. + if (ep->srv) srv_ref(ep->srv); ep->p->refcnt = 1; /* Refcnt from other end is void here */ MAY_DBG(FN; NDBG(ep->rfd.fd, d); NDBG(task_now(), f); @@ -4120,6 +4124,8 @@ int acceptor_learner_task(task_arg arg) if (ep->buf) X_FREE(ep->buf); free(ep->in_buf); + // Allow the server reference to be freed. + if (ep->srv) srv_unref(ep->srv); TASK_END; } diff --git a/rapid/plugin/group_replication/libmysqlgcs/src/bindings/xcom/xcom/xcom_transport.c b/rapid/plugin/group_replication/libmysqlgcs/src/bindings/xcom/xcom/xcom_transport.c index a29b024c6a33..3e324ed65999 100644 --- a/rapid/plugin/group_replication/libmysqlgcs/src/bindings/xcom/xcom/xcom_transport.c +++ b/rapid/plugin/group_replication/libmysqlgcs/src/bindings/xcom/xcom/xcom_transport.c @@ -747,6 +747,12 @@ mksrv(char *srv, xcom_port port) s->reply_handler = task_new(reply_handler_task, void_arg(s), "reply_handler_task", XCOM_THREAD_DEBUG); } reset_srv_buf(&s->out_buf); + /* + Keep the server from being freed if the acceptor_learner_task calls + srv_unref on the server before the {local_,}server_task and + reply_handler_task begin. + */ + srv_ref(s); return s; } @@ -872,6 +878,9 @@ static void shut_srv(server *s) task_terminate(s->sender); if (s->reply_handler) task_terminate(s->reply_handler); + + // Allow the server to be freed. This unref pairs with the ref from mksrv. + srv_unref(s); }