[Tarantool-patches] [PATCH vshard 06/11] util: introduce fiber_is_self_canceled()

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Tue Feb 23 03:15:44 MSK 2021


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)



More information about the Tarantool-patches mailing list