From 9a1a9f09869d9dfcccb433954b4e75e3ca7d130d Mon Sep 17 00:00:00 2001 From: mechanik20051988 <mechanik20051988@tarantool.org> Date: Wed, 29 Sep 2021 18:22:19 +0300 Subject: [PATCH] lua: implement timeout for 'fiber:join' Implement ability to pass timeout to 'fiber:join' function. If timeout expired, join fails with 'timed out' error. Closes #6203 @TarantoolBot document Title: ability to set timeout for 'fiber:join' function was implemented Implement ability to pass timeout to 'fiber:join' function. If timeout expired, join fails with 'timed out' error. --- ...-6203-implement-fiber-join-with-timeout.md | 3 ++ src/lua/fiber.c | 11 +++- .../gh-6203-fiber-join-with-timeout.result | 51 +++++++++++++++++++ .../gh-6203-fiber-join-with-timeout.test.lua | 21 ++++++++ 4 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 changelogs/unreleased/gh-6203-implement-fiber-join-with-timeout.md create mode 100644 test/app/gh-6203-fiber-join-with-timeout.result create mode 100644 test/app/gh-6203-fiber-join-with-timeout.test.lua diff --git a/changelogs/unreleased/gh-6203-implement-fiber-join-with-timeout.md b/changelogs/unreleased/gh-6203-implement-fiber-join-with-timeout.md new file mode 100644 index 0000000000..0a45a780c9 --- /dev/null +++ b/changelogs/unreleased/gh-6203-implement-fiber-join-with-timeout.md @@ -0,0 +1,3 @@ +## feature/core + +* Implement timeout for 'fiber:join' in lua (gh-6203). \ No newline at end of file diff --git a/src/lua/fiber.c b/src/lua/fiber.c index 95634a4d01..c4059a9e44 100644 --- a/src/lua/fiber.c +++ b/src/lua/fiber.c @@ -809,13 +809,20 @@ lbox_fiber_join(struct lua_State *L) if (!(fiber->flags & FIBER_IS_JOINABLE)) luaL_error(L, "the fiber is not joinable"); - fiber_join(fiber); + double timeout = TIMEOUT_INFINITY; + if (!lua_isnoneornil(L, 2)) { + if (!lua_isnumber(L, 2) || + (timeout = lua_tonumber(L, 2)) < .0) { + luaL_error(L, "fiber:join(timeout): bad arguments"); + } + } + int rc = fiber_join_timeout(fiber, timeout); if (child_L != NULL) { coro_ref = lua_tointeger(child_L, -1); lua_pop(child_L, 1); } - if (fiber->f_ret != 0) { + if (rc != 0) { /* * After fiber_join the error of fiber being joined was moved to * current fiber diag so we have to get it from there. diff --git a/test/app/gh-6203-fiber-join-with-timeout.result b/test/app/gh-6203-fiber-join-with-timeout.result new file mode 100644 index 0000000000..e707043021 --- /dev/null +++ b/test/app/gh-6203-fiber-join-with-timeout.result @@ -0,0 +1,51 @@ +fiber = require('fiber') +--- +... +timeout = 10 +--- +... +function sleep(timeout) \ + channel:get() \ + fiber.sleep(timeout) \ +end +--- +... +channel = fiber.channel() +--- +... +f = fiber.create(sleep, timeout) +--- +... +f:set_joinable(true) +--- +... +channel:put(true) +--- +- true +... +f:join(0.1) -- Join failed because of timeout +--- +- false +- timed out +... +timeout = 0.2 +--- +... +f = fiber.create(sleep, timeout) +--- +... +f:set_joinable(true) +--- +... +channel:put(true) +--- +- true +... +f:join("xxx") --error: 'fiber:join(timeout): bad arguments' +--- +- error: 'fiber:join(timeout): bad arguments' +... +f:join(-1) --error: 'fiber:join(timeout): bad arguments' +--- +- error: 'fiber:join(timeout): bad arguments' +... diff --git a/test/app/gh-6203-fiber-join-with-timeout.test.lua b/test/app/gh-6203-fiber-join-with-timeout.test.lua new file mode 100644 index 0000000000..ad1e13d5f5 --- /dev/null +++ b/test/app/gh-6203-fiber-join-with-timeout.test.lua @@ -0,0 +1,21 @@ +fiber = require('fiber') + +timeout = 10 +function sleep(timeout) \ + channel:get() \ + fiber.sleep(timeout) \ +end + +channel = fiber.channel() +f = fiber.create(sleep, timeout) +f:set_joinable(true) +channel:put(true) +f:join(0.1) -- Join failed because of timeout + +timeout = 0.2 +f = fiber.create(sleep, timeout) +f:set_joinable(true) +channel:put(true) +f:join("xxx") --error: 'fiber:join(timeout): bad arguments' +f:join(-1) --error: 'fiber:join(timeout): bad arguments' + -- GitLab