From 04e17d603bcb4de97686ac0203febe3ef40319c2 Mon Sep 17 00:00:00 2001
From: Georgy Moshkin <gmoshkin@picodata.io>
Date: Tue, 16 Aug 2022 19:05:30 +0300
Subject: [PATCH] fix: race condition in on_shutdown

Tarantool remote requests are handled in a IProto thread, and
on_shutdown triggers are handled in the TX thread.

During shutdown we send a request to the leader, which introduces a race
condition in case we are the leader. If iproto thread shuts down before
the request is sent, it will never be handled.

The fix is simple: in case we are the leader during shutdown, don't send
the request, instead handle the request directly in the TX thread.
---
 src/traft/failover.rs | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/traft/failover.rs b/src/traft/failover.rs
index 04386cb37a..a695cca5f6 100644
--- a/src/traft/failover.rs
+++ b/src/traft/failover.rs
@@ -39,6 +39,12 @@ pub fn on_shutdown() {
             node.wait_status();
             continue;
         });
+        if leader_id == raft_id {
+            if let Err(e) = node.handle_topology_request_and_wait(req.into()) {
+                crate::warn_or_panic!("failed to deactivate myself: {}", e);
+            }
+            break;
+        }
         let leader = Storage::peer_by_raft_id(leader_id).unwrap().unwrap();
         let wait_before_retry = Duration::from_millis(300);
         let now = Instant::now();
-- 
GitLab