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,
	imun@tarantool.org
Subject: [Tarantool-patches] [PATCH 1/2] fiber: unref fiber.storage via global Lua state
Date: Sun,  8 Dec 2019 20:28:32 +0100	[thread overview]
Message-ID: <b58dce9ba1ef11c4eb0272450b66b1fce0558f67.1575833120.git.v.shpilevoy@tarantool.org> (raw)
In-Reply-To: <cover.1575833120.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 Lua state;
    L1 - stack of the fiber, created by fiber.create();
    L2 - stack 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.0 (Apple Git-122.2)

  reply	other threads:[~2019-12-08 19:28 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-08 19:28 [Tarantool-patches] [PATCH 0/2] Fiber storage leak Vladislav Shpilevoy
2019-12-08 19:28 ` Vladislav Shpilevoy [this message]
2019-12-11 23:57   ` [Tarantool-patches] [PATCH 1/2] fiber: unref fiber.storage via global Lua state Igor Munkin
2019-12-12  8:50     ` Konstantin Osipov
2019-12-12 23:38     ` Vladislav Shpilevoy
2019-12-13 10:44       ` Igor Munkin
2019-12-08 19:28 ` [Tarantool-patches] [PATCH 2/2] fiber: destroy fiber.storage created by iproto Vladislav Shpilevoy
2019-12-09  7:21   ` Konstantin Osipov
2019-12-09 23:31     ` Vladislav Shpilevoy
2019-12-10  8:21       ` Konstantin Osipov
2019-12-10  8:32   ` Konstantin Osipov
2019-12-10 22:59     ` Vladislav Shpilevoy
2019-12-11  7:08       ` Konstantin Osipov
2019-12-11 21:23         ` Vladislav Shpilevoy
2019-12-12  0:00           ` Igor Munkin
2019-12-12 23:37             ` Vladislav Shpilevoy
2019-12-13 13:35               ` Igor Munkin
2019-12-12  8:46           ` Konstantin Osipov
2019-12-13  0:02             ` Vladislav Shpilevoy
2019-12-13  7:58               ` Konstantin Osipov
2019-12-13 23:11                 ` Vladislav Shpilevoy
2019-12-14 12:26                   ` Konstantin Osipov
2019-12-14 12:30                     ` Konstantin Osipov
2019-12-14 12:33                       ` Konstantin Osipov
2019-12-14 16:49                         ` Vladislav Shpilevoy
2019-12-14 20:59                           ` 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=b58dce9ba1ef11c4eb0272450b66b1fce0558f67.1575833120.git.v.shpilevoy@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=imun@tarantool.org \
    --cc=kostja.osipov@gmail.com \
    --cc=tarantool-patches@dev.tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 1/2] 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