From bda53c526f28d8602894422821ca70a77abc1ea2 Mon Sep 17 00:00:00 2001 From: Roman Tsisyk <roman@tsisyk.com> Date: Mon, 7 Oct 2013 18:46:57 +0400 Subject: [PATCH] Fix #53: "Race condition running init.lua" --- src/lua/init.cc | 20 ++++++++++++++++++++ test/box/test_init.lua | 8 ++++++++ 2 files changed, 28 insertions(+) diff --git a/src/lua/init.cc b/src/lua/init.cc index 423067d478..fb4961d37a 100644 --- a/src/lua/init.cc +++ b/src/lua/init.cc @@ -1513,6 +1513,13 @@ load_init_script(va_list ap) { struct lua_State *L = va_arg(ap, struct lua_State *); + /* + * Return control to tarantool_lua_load_init_script. + * tarantool_lua_load_init_script when will start an auxiliary event + * loop and re-schedule this fiber. + */ + fiber_sleep(0.0); + char path[PATH_MAX + 1]; snprintf(path, PATH_MAX, "%s/%s", cfg.script_dir, TARANTOOL_LUA_INIT_SCRIPT); @@ -1530,6 +1537,12 @@ load_init_script(va_list ap) * The file doesn't exist. It's OK, tarantool may * have no init file. */ + + /* + * Lua script finished. Stop the auxiliary event loop and + * return control back to tarantool_lua_load_init_script. + */ + ev_break(EVBREAK_ALL); } /** @@ -1573,6 +1586,13 @@ tarantool_lua_load_init_script(struct lua_State *L) struct fiber *loader = fiber_new(TARANTOOL_LUA_INIT_SCRIPT, load_init_script); fiber_call(loader, L); + + /* + * Run an auxiliary event loop to re-schedule load_init_script fiber. + * When this fiber finishes, it will call ev_break to stop the loop. + */ + ev_run(0); + /* Outside the startup file require() or ffi are not * allowed. */ diff --git a/test/box/test_init.lua b/test/box/test_init.lua index 61b0ed0870..c56b969341 100644 --- a/test/box/test_init.lua +++ b/test/box/test_init.lua @@ -38,3 +38,11 @@ box.fiber.resume(fiber) -- box.insert(0, 2, 4, 8, 16) + +-- +-- A test case for https://github.com/tarantool/tarantool/issues/53 +-- + +assert (require ~= nil) +box.fiber.sleep(0.0) +assert (require ~= nil) -- GitLab