Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: tarantool-patches@freelists.org
Cc: kostja@tarantool.org
Subject: [tarantool-patches] [PATCH 1/4] swim: fix a leak when a trigger is installed
Date: Fri, 28 Jun 2019 01:25:43 +0200	[thread overview]
Message-ID: <8570762db79e2c187cf5e69e28d780d2719c0a71.1561677891.git.v.shpilevoy@tarantool.org> (raw)
In-Reply-To: <cover.1561677891.git.v.shpilevoy@tarantool.org>

SWIM wraps user triggers to prepare arguments. The wrapper
function kept a reference to SWIM object, and prevented its
automatic deletion at GC.

The patch makes this reference weak.

Follow up #4250
---
 src/lua/swim.lua        | 13 ++++++++++---
 test/swim/swim.result   | 21 +++++++++++++++++++++
 test/swim/swim.test.lua | 10 ++++++++++
 3 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/src/lua/swim.lua b/src/lua/swim.lua
index b6d826ca0..bae6c0da6 100644
--- a/src/lua/swim.lua
+++ b/src/lua/swim.lua
@@ -786,10 +786,17 @@ local swim_member_event_mt = {
 -- @return A function to set as a trigger.
 --
 local function swim_on_member_event_new(s, callback, ctx)
+    -- Do not keep a hard reference to a SWIM instance. Otherwise
+    -- it is a cyclic reference, and both the instance and the
+    -- trigger will never be GC-ed.
+    s = setmetatable({s}, {__mode = 'v'})
     return function(member_ptr, event_mask)
-        local m = swim_wrap_member(s, member_ptr)
-        local event = setmetatable({event_mask}, swim_member_event_mt)
-        return callback(m, event, ctx)
+        local s = s[1]
+        if s then
+            local m = swim_wrap_member(s, member_ptr)
+            local event = setmetatable({event_mask}, swim_member_event_mt)
+            return callback(m, event, ctx)
+        end
     end
 end
 
diff --git a/test/swim/swim.result b/test/swim/swim.result
index 05a108614..6004971d0 100644
--- a/test/swim/swim.result
+++ b/test/swim/swim.result
@@ -1574,6 +1574,27 @@ s1:delete()
 s2:delete()
 ---
 ...
+--
+-- Check that Lua triggers don't keep a reference of SWIM instance
+-- preventing its GC.
+--
+s = swim.new({uri = 0, uuid = uuid(1)})
+---
+...
+_ = s:on_member_event(function() end)
+---
+...
+s = setmetatable({s}, {__mode = 'v'})
+---
+...
+collectgarbage('collect')
+---
+- 0
+...
+s
+---
+- []
+...
 test_run:cmd("clear filter")
 ---
 - true
diff --git a/test/swim/swim.test.lua b/test/swim/swim.test.lua
index b4e4adafc..f1139087c 100644
--- a/test/swim/swim.test.lua
+++ b/test/swim/swim.test.lua
@@ -535,4 +535,14 @@ s1:cfg({generation = 5})
 s1:delete()
 s2:delete()
 
+--
+-- Check that Lua triggers don't keep a reference of SWIM instance
+-- preventing its GC.
+--
+s = swim.new({uri = 0, uuid = uuid(1)})
+_ = s:on_member_event(function() end)
+s = setmetatable({s}, {__mode = 'v'})
+collectgarbage('collect')
+s
+
 test_run:cmd("clear filter")
-- 
2.20.1 (Apple Git-117)

  reply	other threads:[~2019-06-27 23:24 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-27 23:25 [tarantool-patches] [PATCH 0/4] SWIM amendments Vladislav Shpilevoy
2019-06-27 23:25 ` Vladislav Shpilevoy [this message]
2019-06-27 23:25 ` [tarantool-patches] [PATCH 2/4] swim: fix a dangerous yield in ffi.gc Vladislav Shpilevoy
2019-06-27 23:25 ` [tarantool-patches] [PATCH 3/4] swim: fix inability to set generation only Vladislav Shpilevoy
2019-06-27 23:25 ` [tarantool-patches] [PATCH 4/4] swim: default generation is timestamp Vladislav Shpilevoy
2019-06-28 11:25 ` [tarantool-patches] Re: [PATCH 0/4] SWIM amendments Konstantin Osipov
2019-06-28 21:02   ` Vladislav Shpilevoy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8570762db79e2c187cf5e69e28d780d2719c0a71.1561677891.git.v.shpilevoy@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [tarantool-patches] [PATCH 1/4] swim: fix a leak when a trigger is installed' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox