From 22aa7e3453c6eacf88bf0b19c98e68bc674eb423 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Thu, 9 Jan 2025 20:34:20 -0800 Subject: [PATCH 1/3] don't check for lock when interrupting lua --- library/LuaTools.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/LuaTools.cpp b/library/LuaTools.cpp index 24b540d242..b118fbdfa8 100644 --- a/library/LuaTools.cpp +++ b/library/LuaTools.cpp @@ -521,7 +521,7 @@ static void interrupt_hook (lua_State *L, lua_Debug *ar) bool DFHack::Lua::Interrupt (bool force) { - lua_State *L = DFHack::Core::getInstance().getLuaState(); + lua_State *L = DFHack::Core::getInstance().getLuaState(true); if (L->hook != interrupt_hook && !force) return false; if (force) From e464afc2aa424f9f6eae6d456ab0296c7e30a81c Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Thu, 9 Jan 2025 20:35:50 -0800 Subject: [PATCH 2/3] turn off try_autocomplete for remote connections and add explanatory comment --- library/Core.cpp | 4 ++-- library/RemoteTools.cpp | 4 +++- library/include/Core.h | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/library/Core.cpp b/library/Core.cpp index d9910c41c8..e41f6f45e7 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -706,7 +706,7 @@ void ls_helper(color_ostream &con, const std::vector ¶ms) { } } -command_result Core::runCommand(color_ostream &con, const std::string &first_, std::vector &parts) +command_result Core::runCommand(color_ostream &con, const std::string &first_, std::vector &parts, bool no_autocomplete) { std::string first = first_; CommandDepthCounter counter; @@ -1273,7 +1273,7 @@ command_result Core::runCommand(color_ostream &con, const std::string &first_, s } if ( lua ) res = runLuaScript(con, first, parts); - else if ( try_autocomplete(con, first, completed) ) + else if (!no_autocomplete && try_autocomplete(con, first, completed)) res = CR_NOT_IMPLEMENTED; else con.printerr("%s is not a recognized command.\n", first.c_str()); diff --git a/library/RemoteTools.cpp b/library/RemoteTools.cpp index d8d5eb61e1..bd0d230cbd 100644 --- a/library/RemoteTools.cpp +++ b/library/RemoteTools.cpp @@ -721,7 +721,9 @@ command_result CoreService::RunCommand(color_ostream &stream, for (int i = 0; i < in->arguments_size(); i++) args.push_back(in->arguments(i)); - return Core::getInstance().runCommand(stream, cmd, args); + // disable try_autocomplete in runCommand so misspellings of kill-lua won't cause deadlocks + // this remote server connection could be the last chance for recovering from stuck Lua scripts + return Core::getInstance().runCommand(stream, cmd, args, true); } command_result CoreService::CoreSuspend(color_ostream &stream, const EmptyMessage*, IntMessage *cnt) diff --git a/library/include/Core.h b/library/include/Core.h index f7da6fcfb3..7e3c825bce 100644 --- a/library/include/Core.h +++ b/library/include/Core.h @@ -171,7 +171,7 @@ namespace DFHack /// returns a named pointer. void *GetData(std::string key); - command_result runCommand(color_ostream &out, const std::string &command, std::vector ¶meters); + command_result runCommand(color_ostream &out, const std::string &command, std::vector ¶meters, bool no_autocomplete = false); command_result runCommand(color_ostream &out, const std::string &command); bool loadScriptFile(color_ostream &out, std::string fname, bool silent = false); From 20cd7927850c43c65e272317e5ed6085e18e60a4 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Thu, 9 Jan 2025 20:38:13 -0800 Subject: [PATCH 3/3] disable ConditionalCoreSuspender on Linux until we can figure out why it doesn't behave properly --- library/Core.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/library/Core.cpp b/library/Core.cpp index e41f6f45e7..8538dd65e9 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -419,6 +419,9 @@ bool is_builtin(color_ostream &con, const std::string &command) { } void get_commands(color_ostream &con, std::vector &commands) { +#ifdef LINUX_BUILD + CoreSuspender suspend; +#else ConditionalCoreSuspender suspend{}; if (!suspend) { @@ -426,6 +429,7 @@ void get_commands(color_ostream &con, std::vector &commands) { commands.clear(); return; } +#endif auto L = DFHack::Core::getInstance().getLuaState(); Lua::StackUnwinder top(L); @@ -626,12 +630,17 @@ static std::string sc_event_name (state_change_event id) { } void help_helper(color_ostream &con, const std::string &entry_name) { +#ifdef LINUX_BUILD + CoreSuspender suspend; +#else ConditionalCoreSuspender suspend{}; if (!suspend) { con.printerr("Failed Lua call to helpdb.help (could not acquire core lock).\n"); return; } +#endif + auto L = DFHack::Core::getInstance().getLuaState(); Lua::StackUnwinder top(L); @@ -649,7 +658,17 @@ void help_helper(color_ostream &con, const std::string &entry_name) { } void tags_helper(color_ostream &con, const std::string &tag) { +#ifdef LINUX_BUILD CoreSuspender suspend; +#else + ConditionalCoreSuspender suspend{}; + + if (!suspend) { + con.printerr("Failed Lua call to helpdb.help (could not acquire core lock).\n"); + return; + } +#endif + auto L = DFHack::Core::getInstance().getLuaState(); Lua::StackUnwinder top(L); @@ -686,7 +705,17 @@ void ls_helper(color_ostream &con, const std::vector ¶ms) { filter.push_back(str); } +#ifdef LINUX_BUILD CoreSuspender suspend; +#else + ConditionalCoreSuspender suspend{}; + + if (!suspend) { + con.printerr("Failed Lua call to helpdb.help (could not acquire core lock).\n"); + return; + } +#endif + auto L = DFHack::Core::getInstance().getLuaState(); Lua::StackUnwinder top(L);