From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 3C16171814; Tue, 23 Feb 2021 03:19:59 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 3C16171814 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1614039599; bh=pR8Rk1rRE1KkGvvpFlt7HO+943zdEg4/9aazJh7M+oE=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=ACR3LNou7xModz8EUcIK9VNURZP3awBWyaeHBRPVQZIUXf67UaRN4lgAlx+eN10de MpZVy2m3rfwaFnudWe/TuaZAqoudp6sFMsGJ7j5wCnoCJifomKM0I4o7fKVd96RdDX 9v/iLULfSjkjprsSzGMftv4MuKUtqdKS+oErFEuA= Received: from smtpng2.m.smailru.net (smtpng2.m.smailru.net [94.100.179.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id C638D6C1A5 for ; Tue, 23 Feb 2021 03:15:58 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C638D6C1A5 Received: by smtpng2.m.smailru.net with esmtpa (envelope-from ) id 1lELMs-0003CR-0f; Tue, 23 Feb 2021 03:15:58 +0300 To: tarantool-patches@dev.tarantool.org, olegrok@tarantool.org, yaroslav.dynnikov@tarantool.org Date: Tue, 23 Feb 2021 01:15:44 +0100 Message-Id: X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD975C3EC174F566922928A658C60196E87DF7449A63D990329182A05F538085040C5ECC4171824733D0E789F3D2B3BF57E7878BE0B1006DC41F5EF6DB74FCA4C38 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7BF6702EC5472AA0FEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637E16D8B060A9A23898638F802B75D45FF5571747095F342E8C7A0BC55FA0FE5FC425FEB8730D38403616FE2C9DCD5C0ED389980DD8DF98F6C389733CBF5DBD5E913377AFFFEAFD269176DF2183F8FC7C081F0255C1FF0F6138941B15DA834481FCF19DD082D7633A0EF3E4896CB9E6436389733CBF5DBD5E9D5E8D9A59859A8B6D827CA74CCEABFF3CC7F00164DA146DA6F5DAA56C3B73B23C77107234E2CFBA567F23339F89546C55F5C1EE8F4F765FC08F9A42B2210255C75ECD9A6C639B01BBD4B6F7A4D31EC0BC0CAF46E325F83A522CA9DD8327EE4930A3850AC1BE2E73579C543ECCDAE434EC4224003CC836476C0CAF46E325F83A50BF2EBBBDD9D6B0F347543BADC64E7283B503F486389A921A5CC5B56E945C8DA X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C4C7A0BC55FA0FE5FC425FEB8730D38403616FE2C9DCD5C0EDA7719782FDFA9641B1881A6453793CE9C32612AADDFBE061C61BE10805914D3804EBA3D8E7E5B87ABF8C51168CD8EBDB587F3D2152687E5CDC48ACC2A39D04F89CDFB48F4795C241BDAD6C7F3747799A X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D346E67BC984B8ABCB598EB23D7D26FC06D5C676F7318309BB723E6988089BF6547A92821EB96ACCF871D7E09C32AA3244C6C61FB11685F50477A27246B5D986BE31DD47778AE04E04DFACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj2drqE2xHc+FhEmt4Kh3oPw== X-Mailru-Sender: 689FA8AB762F73936BC43F508A0638226D55767C3B9D11806E7F61C54BD797CB3841015FED1DE5223CC9A89AB576DD93FB559BB5D741EB963CF37A108A312F5C27E8A8C3839CE0E267EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH vshard 06/11] util: introduce fiber_is_self_canceled() X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Vladislav Shpilevoy via Tarantool-patches Reply-To: Vladislav Shpilevoy Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Original fiber.testcancel() has an issue - it is not exception-safe. This makes it unusable for code which wants to do cleanup before cancellation. The patch introduces util.fiber_is_self_canceled() which checks if the current fiber is canceled but returns true/false instead of throwing an error. The patch is going to be used in the map-reduce patches where it will be necessary to check if the fiber is canceled. And if it is - perform cleanup and quit whatever the code was doing. Part of #147 --- test/unit/util.result | 28 ++++++++++++++++++++++++++++ test/unit/util.test.lua | 14 ++++++++++++++ vshard/util.lua | 8 ++++++++ 3 files changed, 50 insertions(+) diff --git a/test/unit/util.result b/test/unit/util.result index 679c087..c83e80c 100644 --- a/test/unit/util.result +++ b/test/unit/util.result @@ -266,3 +266,31 @@ assert(type(err) == 'table') --- - true ... +-- +-- Exception-safe fiber cancel check. +-- +self_is_canceled = util.fiber_is_self_canceled +--- +... +assert(not self_is_canceled()) +--- +- true +... +ok = nil +--- +... +_ = fiber.create(function() \ + local f = fiber.self() \ + pcall(f.cancel, f) \ + ok = self_is_canceled() \ +end) +--- +... +test_run:wait_cond(function() return ok ~= nil end) +--- +- true +... +assert(ok) +--- +- true +... diff --git a/test/unit/util.test.lua b/test/unit/util.test.lua index df3db6f..881feb4 100644 --- a/test/unit/util.test.lua +++ b/test/unit/util.test.lua @@ -107,3 +107,17 @@ _ = test_run:wait_cond(function() return ok or err end) assert(not ok) err.message assert(type(err) == 'table') + +-- +-- Exception-safe fiber cancel check. +-- +self_is_canceled = util.fiber_is_self_canceled +assert(not self_is_canceled()) +ok = nil +_ = fiber.create(function() \ + local f = fiber.self() \ + pcall(f.cancel, f) \ + ok = self_is_canceled() \ +end) +test_run:wait_cond(function() return ok ~= nil end) +assert(ok) diff --git a/vshard/util.lua b/vshard/util.lua index d78f3a5..30a1e6e 100644 --- a/vshard/util.lua +++ b/vshard/util.lua @@ -225,6 +225,13 @@ local function fiber_cond_wait(cond, timeout) return nil, lerror.make(err) end +-- +-- Exception-safe way to check if the current fiber is canceled. +-- +local function fiber_is_self_canceled() + return not pcall(fiber.testcancel) +end + return { tuple_extract_key = tuple_extract_key, reloadable_fiber_create = reloadable_fiber_create, @@ -235,4 +242,5 @@ return { table_copy_yield = table_copy_yield, table_minus_yield = table_minus_yield, fiber_cond_wait = fiber_cond_wait, + fiber_is_self_canceled = fiber_is_self_canceled, } -- 2.24.3 (Apple Git-128)