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

Add script termination option to 'Lua Scripts' floater #1152

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions indra/llcommon/llapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ void LLApp::setStatus(EAppStatus status)
statsd = LLSD::Integer(status);
}
LLEventPumps::instance().obtain("LLApp").post(llsd::map("status", statsd));
LLEventPumps::instance().obtain("LLLua").post(llsd::map("status", "close_all"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Starting to look at this made me realize that I want to write a mechanism to request termination of an arbitrary C++ coroutine, not just one running Lua.

Quick sketch: I want to add a new data structure indicating a terminate request to LLCoros, plus an LLEventStream. The LLCoros constructor will cause its "LLCoros" LLEventPump to listen on "LLApp" so that events posted to "LLApp" will be passed along on "LLCoros".

I'll make LLCoros::checkStop() check for a termination request for this specific coroutine, in addition to checking for general viewer shutdown.

There are a few different places where a coroutine listens on "LLApp" for viewer shutdown events. I'll change those to listen on "LLCoros" instead.

The point of the distinction is that then I can resurrect the commented-out LLCoros::kill() method to set a termination request for the subject coroutine. But in case the subject coroutine is waiting for an event, kill() will also post a heads-up event on "LLCoros" to cause each waiting coroutine to check for a termination request. I'd rather not post that event on the more generic "LLApp" event pump.

So what I think I'd like to do is to accept this PR (once you're done; I realize you're still refining it) and then recast it to use the termination mechanism described above. I'll probably remove this second post() call at that time.

Copy link
Contributor Author

@maxim-productengine maxim-productengine Apr 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest I missed the ability to terminate coroutine once or twice in the past when working on maint bugs. So, I'm for it.

I'm not planning any more changes for now for this PR. (there will be some changes related to script termination as you know, but I'll create separate pr)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry to say I overlooked an important point. I can indeed introduce a flag for the next time a given coroutine wakes up and calls LLCoros::checkStop(), but I can't cause that coroutine to wake up and check it. An LLEventStream listener runs on the coroutine calling post(). An exception thrown by such a listener will propagate through the post() call.

The Boost.Fiber library we use does not provide a way to force an exception onto a suspended fiber -- although tantalizingly, the Boost.Context library on which it's based provides a mechanism that could support that. At present, the best we can do is to wait for the subject coroutine to call checkStop().

}
}

Expand Down
8 changes: 5 additions & 3 deletions indra/llcommon/lualistener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@ std::ostream& operator<<(std::ostream& out, const LuaListener& self)

LuaListener::LuaListener(lua_State* L):
super(getUniqueKey()),
mCoroName(LLCoros::getName()),
mListener(new LLLeapListener(
"LuaListener",
[this](const std::string& pump, const LLSD& data)
{ return queueEvent(pump, data); })),
// Listen for shutdown events on the "LLApp" LLEventPump.
mShutdownConnection(
LLEventPumps::instance().obtain("LLApp").listen(
LLEventPumps::instance().obtain("LLLua").listen(
LLEventPump::inventName("LuaState"),
[this](const LLSD& status)
{
const auto& statsd = status["status"];
if (statsd.asString() != "running")
const auto& coro_name = status["coro"].asString();
const auto& statsd = status["status"].asString();
maxim-productengine marked this conversation as resolved.
Show resolved Hide resolved
if ((statsd == "close_all") || ((statsd == "close") && (coro_name == mCoroName)))
{
// If a Lua script is still blocked in getNext() during
// viewer shutdown, close the queue to wake up getNext().
Expand Down
2 changes: 2 additions & 0 deletions indra/llcommon/lualistener.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class LuaListener: public LLInstanceTracker<LuaListener, int>

std::unique_ptr<LLLeapListener> mListener;
LLTempBoundListener mShutdownConnection;

std::string mCoroName;
};

#endif /* ! defined(LL_LUALISTENER_H) */
6 changes: 6 additions & 0 deletions indra/newview/llfloaterluascripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ LLFloaterLUAScripts::LLFloaterLUAScripts(const LLSD &key)
{
gViewerWindow->getWindow()->openFolder(mTargetFolderPath);
});
mCommitCallbackRegistrar.add("Script.Terminate", [this](LLUICtrl*, const LLSD &userdata)
{
LLEventPumps::instance().obtain("LLLua").post(llsd::map("status", "close", "coro", mCoroName));
LLLUAmanager::terminateScript(mCoroName);
});
}


Expand Down Expand Up @@ -113,6 +118,7 @@ void LLFloaterLUAScripts::onScrollListRightClicked(LLUICtrl *ctrl, S32 x, S32 y)
if (menu)
{
mTargetFolderPath = std::filesystem::path((item->getColumn(1)->getValue().asString())).parent_path().string();
mCoroName = item->getValue().asString();
maxim-productengine marked this conversation as resolved.
Show resolved Hide resolved
menu->show(x, y);
LLMenuGL::showPopup(this, menu, x, y);
}
Expand Down
1 change: 1 addition & 0 deletions indra/newview/llfloaterluascripts.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class LLFloaterLUAScripts :
std::unique_ptr<LLTimer> mUpdateTimer;
LLScrollListCtrl* mScriptList;
std::string mTargetFolderPath;
std::string mCoroName;

LLHandle<LLContextMenu> mContextMenuHandle;
};
Expand Down
15 changes: 15 additions & 0 deletions indra/newview/llluamanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include <vector>

std::map<std::string, std::string> LLLUAmanager::sScriptNames;
std::set<std::string> LLLUAmanager::sTerminationList;

lua_function(sleep, "sleep(seconds): pause the running coroutine")
{
Expand Down Expand Up @@ -188,6 +189,20 @@ void LLLUAmanager::runScriptFile(const std::string &filename, script_result_fn r
// A script_finished_fn is used to initialize the LuaState.
// It will be called when the LuaState is destroyed.
LuaState L(finished_cb);

lua_callbacks(L)->interrupt = [](lua_State *L, int gc)
{
if (gc >= 0)
return;
maxim-productengine marked this conversation as resolved.
Show resolved Hide resolved

std::set<std::string> scripts = LLLUAmanager::getTerminationList();
std::string coro = LLCoros::getName();
if (scripts.find(coro) != scripts.end())
{
sTerminationList.erase(coro);
maxim-productengine marked this conversation as resolved.
Show resolved Hide resolved
lluau::error(L, "Script was terminated");
}
};
std::string text{std::istreambuf_iterator<char>(in_file), {}};
auto [count, result] = L.expr(filename, text);
if (result_cb)
Expand Down
5 changes: 4 additions & 1 deletion indra/newview/llluamanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,12 @@ class LLLUAmanager
static void runScriptOnLogin();

static const std::map<std::string, std::string> getScriptNames() { return sScriptNames; }
static std::set<std::string> getTerminationList() { return sTerminationList; }
static void terminateScript(std::string& coro_name) { sTerminationList.insert(coro_name); }

private:
static std::map<std::string, std::string> sScriptNames;
static std::map<std::string, std::string> sScriptNames;
static std::set<std::string> sTerminationList;
};

class LLRequireResolver
Expand Down
8 changes: 8 additions & 0 deletions indra/newview/skins/default/xui/en/menu_lua_scripts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@
<menu_item_call.on_click
function="Script.OpenFolder" />
</menu_item_call>
<menu_item_separator/>
<menu_item_call
label="Terminate script"
layout="topleft"
name="terminate">
<menu_item_call.on_click
function="Script.Terminate" />
</menu_item_call>
</context_menu>
Loading