From 9643fdc77323eb55a34ad8fefa7d4fa37b97ee87 Mon Sep 17 00:00:00 2001
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
Date: Mon, 2 Dec 2019 23:40:17 +0100
Subject: [PATCH] test: fix flaky box/gh-4627-session-use-after-free

The problem was in that the test uses the global trigger
box.session.on_disconnect() to set a global variable by one
connection. But test-run can do multiple connects/reconnects to
the same instance. That led to multiple invocations of
box.session.on_disconnect(), which could override the global
variable in unexpected ways and moments.

The patch makes only one session execute that trigger.

Probably related to https://github.com/tarantool/test-run/issues/46
Follow up #4627

Reviewed-by: Alexander Turenko <alexander.turenko@tarantool.org>
---
 .../box/gh-4627-session-use-after-free.result | 29 +++++++++++++++++--
 .../gh-4627-session-use-after-free.test.lua   | 22 +++++++++++++-
 2 files changed, 48 insertions(+), 3 deletions(-)

diff --git a/test/box/gh-4627-session-use-after-free.result b/test/box/gh-4627-session-use-after-free.result
index 5e5c154b92..29144b0983 100644
--- a/test/box/gh-4627-session-use-after-free.result
+++ b/test/box/gh-4627-session-use-after-free.result
@@ -20,6 +20,22 @@ fiber = require('fiber')
  | ---
  | ...
 
+box.schema.user.grant('guest', 'execute', 'universe')
+ | ---
+ | ...
+
+-- This is a workaround for flakiness of the test
+-- appearing when test-run does reconnects to the
+-- instance and therefore creates multiple
+-- sessions. By setting a flag for only one
+-- session, others won't interfere in
+-- session.on_disconnect().
+function enable_on_disconnect()                 \
+    box.session.storage.is_enabled = true       \
+end
+ | ---
+ | ...
+
 sid_before_yield = nil
  | ---
  | ...
@@ -27,6 +43,9 @@ sid_after_yield = nil
  | ---
  | ...
 func = box.session.on_disconnect(function()     \
+    if not box.session.storage.is_enabled then  \
+        return                                  \
+    end                                         \
     sid_before_yield = box.session.id()         \
     fiber.yield()                               \
     sid_after_yield = box.session.id()          \
@@ -37,9 +56,11 @@ end)
 connection = net_box.connect(box.cfg.listen)
  | ---
  | ...
-connection:ping()
+-- Connections, not related to this one, won't
+-- call this function, and therefore won't do
+-- anything in session.on_disconnect() trigger.
+connection:call('enable_on_disconnect')
  | ---
- | - true
  | ...
 connection:close()
  | ---
@@ -58,3 +79,7 @@ sid_after_yield == sid_before_yield and sid_after_yield ~= 0 or \
 box.session.on_disconnect(nil, func)
  | ---
  | ...
+
+box.schema.user.revoke('guest', 'execute', 'universe')
+ | ---
+ | ...
diff --git a/test/box/gh-4627-session-use-after-free.test.lua b/test/box/gh-4627-session-use-after-free.test.lua
index 70624a96a4..18d56e2035 100644
--- a/test/box/gh-4627-session-use-after-free.test.lua
+++ b/test/box/gh-4627-session-use-after-free.test.lua
@@ -15,16 +15,34 @@
 net_box = require('net.box')
 fiber = require('fiber')
 
+box.schema.user.grant('guest', 'execute', 'universe')
+
+-- This is a workaround for flakiness of the test
+-- appearing when test-run does reconnects to the
+-- instance and therefore creates multiple
+-- sessions. By setting a flag for only one
+-- session, others won't interfere in
+-- session.on_disconnect().
+function enable_on_disconnect()                 \
+    box.session.storage.is_enabled = true       \
+end
+
 sid_before_yield = nil
 sid_after_yield = nil
 func = box.session.on_disconnect(function()     \
+    if not box.session.storage.is_enabled then  \
+        return                                  \
+    end                                         \
     sid_before_yield = box.session.id()         \
     fiber.yield()                               \
     sid_after_yield = box.session.id()          \
 end)
 
 connection = net_box.connect(box.cfg.listen)
-connection:ping()
+-- Connections, not related to this one, won't
+-- call this function, and therefore won't do
+-- anything in session.on_disconnect() trigger.
+connection:call('enable_on_disconnect')
 connection:close()
 
 while not sid_after_yield do fiber.yield() end
@@ -33,3 +51,5 @@ sid_after_yield == sid_before_yield and sid_after_yield ~= 0 or \
     {sid_after_yield, sid_before_yield}
 
 box.session.on_disconnect(nil, func)
+
+box.schema.user.revoke('guest', 'execute', 'universe')
-- 
GitLab