diff --git a/lib/remote/consolehandler.cpp b/lib/remote/consolehandler.cpp index e432abe8cc8..70912c52f56 100644 --- a/lib/remote/consolehandler.cpp +++ b/lib/remote/consolehandler.cpp @@ -31,6 +31,12 @@ static void ScriptFrameCleanupHandler() std::vector cleanup_keys; for (auto& kv : l_ApiScriptFrames) { + std::unique_lock frameLock(kv.second->Mutex, std::try_to_lock); + if (!frameLock) { + // If the frame is locked, it's in use, don't expire it this time. + continue; + } + if (kv.second->Seen < Utility::GetTime() - 1800) cleanup_keys.push_back(kv.first); } @@ -126,6 +132,13 @@ bool ConsoleHandler::ExecuteScriptHelper(boost::beast::http::requestMutex, std::try_to_lock); + if (!frameLock) { + HttpUtility::SendJsonError(response, params, 409, "Session is currently in use by another request."); + return true; + } + lsf->Seen = Utility::GetTime(); if (!lsf->Locals) @@ -201,6 +214,13 @@ bool ConsoleHandler::AutocompleteScriptHelper(boost::beast::http::requestMutex, std::try_to_lock); + if (!frameLock) { + HttpUtility::SendJsonError(response, params, 409, "Session is currently in use by another request."); + return true; + } + lsf->Seen = Utility::GetTime(); if (!lsf->Locals) diff --git a/lib/remote/consolehandler.hpp b/lib/remote/consolehandler.hpp index ba93d000177..93a197c0301 100644 --- a/lib/remote/consolehandler.hpp +++ b/lib/remote/consolehandler.hpp @@ -5,12 +5,14 @@ #include "remote/httphandler.hpp" #include "base/scriptframe.hpp" +#include namespace icinga { struct ApiScriptFrame { + std::mutex Mutex; double Seen{0}; int NextLine{1}; std::map Lines;