Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: tarantool-patches@dev.tarantool.org, kostja.osipov@gmail.com
Subject: [Tarantool-patches] [PATCH v2 1/3] fiber: unref fiber.storage via global Lua state
Date: Thu, 16 Jan 2020 22:54:21 +0100	[thread overview]
Message-ID: <cc66ca5fc1c0a20c5b44ff150c455b4499e9e9cc.1579211601.git.v.shpilevoy@tarantool.org> (raw)
In-Reply-To: <cover.1579211601.git.v.shpilevoy@tarantool.org>

Fiber.storage is a table, available from anywhere in the fiber. It
is destroyed after fiber function is finished. That provides a
reliable fiber-local storage, similar to thread-local in C/C++.

But there is a problem that the storage may be created via one
struct lua_State, and destroyed via another. Here is an example:

    function test_storage()
        fiber.self().storage.key = 100
    end
    box.schema.func.create('test_storage')
    _ = fiber.create(function()
        box.func.test_storage:call()
    end)

There are 3 struct lua_State:
    tarantool_L - global always alive state;
    L1 - Lua coroutine of the fiber, created by fiber.create();
    L2 - Lua coroutine created by that fiber to execute
         test_storage().

Fiber.storage is created on stack of L2 and referenced by global
LUA_REGISTRYINDEX. Then it is unreferenced from L1 when the fiber
is being destroyed.

That is generally ok as soon as the storage object is always in
LUA_REGISTRYINDEX, which is shared by all Lua states.

But soon during destruction of the fiber.storage there will be
only tarantool_L and the original L2. Original L2 may be already
deleted by the time the storage is being destroyed. So this patch
makes unref of the storage via reliable tarantool_L.

Needed for #4662
---
 src/lua/fiber.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/lua/fiber.c b/src/lua/fiber.c
index 53ebec9aa..a7b75f9bf 100644
--- a/src/lua/fiber.c
+++ b/src/lua/fiber.c
@@ -445,7 +445,7 @@ lua_fiber_run_f(MAYBE_UNUSED va_list ap)
 	/* Destroy local storage */
 	int storage_ref = f->storage.lua.ref;
 	if (storage_ref > 0)
-		luaL_unref(L, LUA_REGISTRYINDEX, storage_ref);
+		luaL_unref(tarantool_L, LUA_REGISTRYINDEX, storage_ref);
 	/*
 	 * If fiber is not joinable
 	 * We can unref child stack here,
-- 
2.21.1 (Apple Git-122.3)

  reply	other threads:[~2020-01-16 21:54 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-16 21:54 [Tarantool-patches] [PATCH v2 0/3] Fiber storage leak Vladislav Shpilevoy
2020-01-16 21:54 ` Vladislav Shpilevoy [this message]
2020-01-17  7:30   ` [Tarantool-patches] [PATCH v2 1/3] fiber: unref fiber.storage via global Lua state Konstantin Osipov
2020-01-16 21:54 ` [Tarantool-patches] [PATCH v2 2/3] fiber: destroy fiber.storage created by iproto Vladislav Shpilevoy
2020-01-16 22:00   ` Cyrill Gorcunov
2020-01-17  7:47     ` Konstantin Osipov
2020-01-17  8:06       ` Cyrill Gorcunov
2020-01-17  7:45   ` Konstantin Osipov
2020-01-19 17:32     ` Vladislav Shpilevoy
2020-01-20  7:22       ` Konstantin Osipov
2020-01-20 19:15         ` Georgy Kirichenko
2020-01-21 22:21         ` Vladislav Shpilevoy
2020-01-21 22:32           ` Konstantin Osipov
2020-01-16 21:54 ` [Tarantool-patches] [PATCH v2 3/3] box: remove dead code from box_process_call/eval() Vladislav Shpilevoy
2020-01-17  7:46   ` Konstantin Osipov
2020-01-17  7:47   ` Konstantin Osipov
2020-01-17 17:41   ` Georgy Kirichenko
2020-01-19 17:32     ` Vladislav Shpilevoy
2020-01-20 19:21       ` Georgy Kirichenko
2020-01-18 19:27 ` [Tarantool-patches] [PATCH v2 0/3] Fiber storage leak Igor Munkin
2020-02-15  1: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=cc66ca5fc1c0a20c5b44ff150c455b4499e9e9cc.1579211601.git.v.shpilevoy@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=kostja.osipov@gmail.com \
    --cc=tarantool-patches@dev.tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v2 1/3] fiber: unref fiber.storage via global Lua state' \
    /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