Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mitigations for deadlock behavior on Linux #5186

Merged
merged 3 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions library/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,17 @@ bool is_builtin(color_ostream &con, const std::string &command) {
}

void get_commands(color_ostream &con, std::vector<std::string> &commands) {
#ifdef LINUX_BUILD
CoreSuspender suspend;
#else
ConditionalCoreSuspender suspend{};

if (!suspend) {
con.printerr("Cannot acquire core lock in helpdb.get_commands\n");
commands.clear();
return;
}
#endif

auto L = DFHack::Core::getInstance().getLuaState();
Lua::StackUnwinder top(L);
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -686,7 +705,17 @@ void ls_helper(color_ostream &con, const std::vector<std::string> &params) {
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);

Expand All @@ -706,7 +735,7 @@ void ls_helper(color_ostream &con, const std::vector<std::string> &params) {
}
}

command_result Core::runCommand(color_ostream &con, const std::string &first_, std::vector<std::string> &parts)
command_result Core::runCommand(color_ostream &con, const std::string &first_, std::vector<std::string> &parts, bool no_autocomplete)
{
std::string first = first_;
CommandDepthCounter counter;
Expand Down Expand Up @@ -1273,7 +1302,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());
Expand Down
2 changes: 1 addition & 1 deletion library/LuaTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion library/RemoteTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion library/include/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <std::string> &parameters);
command_result runCommand(color_ostream &out, const std::string &command, std::vector <std::string> &parameters, 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);

Expand Down
Loading