From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 539283248C for ; Thu, 27 Jun 2019 19:24:58 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id d41BWJh9ygVG for ; Thu, 27 Jun 2019 19:24:58 -0400 (EDT) Received: from smtp46.i.mail.ru (smtp46.i.mail.ru [94.100.177.106]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 936E63209D for ; Thu, 27 Jun 2019 19:24:57 -0400 (EDT) From: Vladislav Shpilevoy Subject: [tarantool-patches] [PATCH 1/4] swim: fix a leak when a trigger is installed Date: Fri, 28 Jun 2019 01:25:43 +0200 Message-Id: <8570762db79e2c187cf5e69e28d780d2719c0a71.1561677891.git.v.shpilevoy@tarantool.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-Help: List-Unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-Subscribe: List-Owner: List-post: List-Archive: To: tarantool-patches@freelists.org Cc: kostja@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)