From 4edb48aeea071c4ebc1ff02082b76586468d6327 Mon Sep 17 00:00:00 2001 From: Curve Date: Wed, 22 May 2024 02:53:54 +0200 Subject: [PATCH] fix(patchbay): timeout in cases where server does not respond (closes Vencord/Vesktop#616) --- CMakeLists.txt | 6 +++--- package.json | 2 +- private/message.hpp | 6 +++++- private/patchbay.impl.hpp | 1 + server/main.cpp | 21 +++++++++++++++++++++ src/patchbay.cpp | 2 -- src/patchbay.impl.cpp | 34 ++++++++++++++++++++++++++++++++-- 7 files changed, 63 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9bb37e2..4db6591 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.21) -project(venmic LANGUAGES CXX VERSION 3.4.4) +project(venmic LANGUAGES CXX VERSION 3.5.0) # -------------------------------------------------------------------------------------------------------- # Library options @@ -87,7 +87,7 @@ include("cmake/cpm.cmake") CPMFindPackage( NAME rohrkabel - VERSION 5.1 + VERSION 6.0 GIT_REPOSITORY "https://github.com/Curve/rohrkabel" ) @@ -111,7 +111,7 @@ CPMFindPackage( CPMFindPackage( NAME glaze - VERSION 2.6.1 + VERSION 2.6.5 GIT_REPOSITORY "https://github.com/stephenberry/glaze" ) diff --git a/package.json b/package.json index deb0a8d..77a2294 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "private": false, "license": "MPL-2.0", "author": "Curve (https://github.com/Curve)", - "version": "3.4.4", + "version": "3.5.0", "main": "./lib/index.js", "types": "./lib/module.d.ts", "scripts": { diff --git a/private/message.hpp b/private/message.hpp index 77bd212..2491b6d 100644 --- a/private/message.hpp +++ b/private/message.hpp @@ -24,11 +24,15 @@ namespace vencord { }; + struct abort + { + }; + struct ready { bool success{true}; }; - using pw_recipe = pw::recipe; + using pw_recipe = pw::recipe; using cr_recipe = cr::recipe, ready>; } // namespace vencord diff --git a/private/patchbay.impl.hpp b/private/patchbay.impl.hpp index 6105c6d..8d33322 100644 --- a/private/patchbay.impl.hpp +++ b/private/patchbay.impl.hpp @@ -46,6 +46,7 @@ namespace vencord std::jthread thread; public: + std::stop_source update_source; std::unique_ptr sender; std::unique_ptr receiver; diff --git a/server/main.cpp b/server/main.cpp index 8c1e870..da92155 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -1,4 +1,6 @@ #include + +#include #include #include @@ -49,6 +51,25 @@ int main(int argc, char **args) httplib::Server server; + server.set_exception_handler( + [&](const auto &, auto &, auto exception) + { + try + { + std::rethrow_exception(exception); + } + catch (const std::exception &ex) + { + logger::get()->error("Encountered error: {}", ex.what()); + } + catch (...) + { + logger::get()->error("Encountered error: "); + } + + server.stop(); + }); + server.Post("/list", [](const auto &req, auto &response) { diff --git a/src/patchbay.cpp b/src/patchbay.cpp index 4018a0b..c594fed 100644 --- a/src/patchbay.cpp +++ b/src/patchbay.cpp @@ -15,13 +15,11 @@ namespace vencord void patchbay::link(link_options options) { - logger::get()->trace(R"([patchbay] (link) request: "{}")", glz::write_json(options)); m_impl->sender->send(std::move(options)); } void patchbay::unlink() { - logger::get()->trace("[patchbay] unlink requested"); m_impl->sender->send(unset_target{}); } diff --git a/src/patchbay.impl.cpp b/src/patchbay.impl.cpp index 79738d9..9c763f5 100644 --- a/src/patchbay.impl.cpp +++ b/src/patchbay.impl.cpp @@ -40,7 +40,15 @@ namespace vencord thread = std::jthread{thread_start, std::move(pw_receiver), cr_sender}; - if (receiver->recv_as().success) + auto response = receiver->recv_timeout_as(std::chrono::seconds(1)); + + if (!response.has_value()) + { + sender->send(abort{}); + response = receiver->recv_as(); + } + + if (response->success) { logger::get()->trace("[patchbay] (init) pw_receiver is ready"); return; @@ -589,6 +597,15 @@ namespace vencord core->context()->loop()->quit(); } + template <> + void patchbay::impl::receive([[maybe_unused]] cr_recipe::sender &, [[maybe_unused]] abort &) + { + should_exit = true; + + update_source.request_stop(); + core->context()->loop()->quit(); + } + void patchbay::impl::start(pw_recipe::receiver receiver, cr_recipe::sender sender) { auto loop = pw::main_loop::create(); @@ -611,16 +628,29 @@ namespace vencord receive(sender, message); }); + auto future = core->update(); + update_source = future.stop_source(); + + auto success = future.get(); + + if (!success.value_or(false)) + { + sender.send(ready{false}); + return; + } + auto listener = registry->listen(); listener.on([this](std::uint32_t id) { del_global(id); }); listener.on([this](auto global) { add_global(global); }); - sender.send(ready{}); + sender.send(ready{true}); while (!should_exit) { loop->run(); } + + logger::get()->trace("[patchbay] (main_loop) finished"); } } // namespace vencord