From 6d67629e75b406894b003e228e77a12ed21a6d48 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Tue, 10 Nov 2026 14:03:41 +0100 Subject: [PATCH 1/3] Diagnose stalled frontend lifecycle tests (#520) --- test/cmake/CheckLifecycleWatchdog.cmake | 4 ++ test/web/CMakeLists.txt | 3 + test/web/FrontendLifecycle.cpp | 75 ++++++++++++++++++++++++- test/web/content-policy.html | 2 + test/web/policy-escaped.html | 1 + test/web/view-association.html | 4 ++ 6 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 test/cmake/CheckLifecycleWatchdog.cmake diff --git a/test/cmake/CheckLifecycleWatchdog.cmake b/test/cmake/CheckLifecycleWatchdog.cmake new file mode 100644 index 00000000..342c4d07 --- /dev/null +++ b/test/cmake/CheckLifecycleWatchdog.cmake @@ -0,0 +1,4 @@ +execute_process(COMMAND "${PROBE}" watchdog-probe RESULT_VARIABLE result ERROR_VARIABLE error TIMEOUT 7) +if(NOT result STREQUAL "1" OR NOT error MATCHES "Lifecycle timeout at stage: association: waiting for second WebSocket") + message(FATAL_ERROR "Watchdog did not report the stalled stage and fail: result=${result}, stderr=${error}") +endif() diff --git a/test/web/CMakeLists.txt b/test/web/CMakeLists.txt index 7f3694ca..f4c8c4ba 100644 --- a/test/web/CMakeLists.txt +++ b/test/web/CMakeLists.txt @@ -20,6 +20,9 @@ if(${WEBFRONT_FRONTEND_${WEBFRONT_WEBTEST_FRONTEND}_OPTION} target_compile_definitions(frontend_lifecycle PRIVATE WEBFRONT_TEST_FRONTEND=${WEBFRONT_WEBTEST_FRONTEND_TYPE}) webfront_setup_frontends_for_target(frontend_lifecycle) add_dependencies(webtest frontend_lifecycle) + add_test(NAME FrontendLifecycleWatchdog COMMAND "${CMAKE_COMMAND}" + "-DPROBE=$" -P "${PROJECT_SOURCE_DIR}/test/cmake/CheckLifecycleWatchdog.cmake") + set_tests_properties(FrontendLifecycleWatchdog PROPERTIES TIMEOUT 10 LABELS fast) endif() # Manual validation of the interface-thread executor and the native window (#450) with a real browser, window and native diff --git a/test/web/FrontendLifecycle.cpp b/test/web/FrontendLifecycle.cpp index 5e44da83..39b9ffef 100644 --- a/test/web/FrontendLifecycle.cpp +++ b/test/web/FrontendLifecycle.cpp @@ -4,6 +4,8 @@ #include #include +#include +#include #include #include #include @@ -26,6 +28,52 @@ using TestFS = webfront::fs::Multi>; static_assert(Frontend::action == webfront::frontend::Action::closeServerAfterOpen); +// A stuck UI loop or a page which never settles must fail before CTest's outer timeout. The watchdog +// uses process termination because the frontend may be stuck on its owning thread and cannot be unwound safely. +std::atomic lifecycleStage = 0; +constexpr const char* stageName(int stage) { + switch (stage) { + case 1: return "bridge connected"; + case 2: return "association: checking origins"; + case 3: return "association: waiting for second WebSocket"; + case 4: return "association: waiting for second connection registration"; + case 5: return "policy: bridge ready, opening popup"; + case 6: return "policy: waiting for navigation or cancellation"; + case 7: return "policy: destination bridge ready"; + case 8: return "page reported completion, waiting for window close"; + case 9: return "window closed, checking results or shutting down"; + default: return "starting frontend or waiting for first bridge connection"; + } +} + +class LifecycleWatchdog { + std::mutex mutex; + std::condition_variable changed; + bool finished = false; + std::thread worker; +public: + explicit LifecycleWatchdog(std::chrono::seconds timeout) : worker([this, timeout] { + std::unique_lock lock{mutex}; + if (!changed.wait_for(lock, timeout, [this] { return finished; })) { + std::cerr << "Lifecycle timeout at stage: " << stageName(lifecycleStage.load()) << std::endl; + std::_Exit(EXIT_FAILURE); + } + }) { } + ~LifecycleWatchdog() { + { + std::lock_guard lock{mutex}; + finished = true; + } + changed.notify_one(); + worker.join(); + } +}; + +template +void registerLifecycleStage(Application& app) { + app.cppFunction("lifecycleStage", [](std::int32_t stage) { lifecycleStage = stage; }); +} + int show(std::string_view page) { App app(webfront::WebFrontConfig{.documentRoot = std::filesystem::current_path()}); std::atomic connected = false; @@ -46,8 +94,11 @@ int checkViewAssociation() { std::vector embedded; std::vector local; std::atomic originsChecked = false; + registerLifecycleStage(app); app.onUIStarted([&](App::UI ui) { std::scoped_lock lock{mutex}; + if (embedded.empty()) + lifecycleStage = 1; embedded.push_back(ui.isEmbeddedView()); local.push_back(ui.isLocalClient()); }); @@ -60,6 +111,7 @@ int checkViewAssociation() { }); app.cppFunction("originsChecked", [&] { originsChecked = true; }); app.show("view-association.html"); + lifecycleStage = 9; if (!originsChecked) throw std::runtime_error("The view association script did not match the browser's serialized IPv6 origins"); std::scoped_lock lock{mutex}; @@ -442,19 +494,28 @@ int checkContentPolicy(bool allowNavigation) { policy.navigationAllowedOrigins = {"http://localhost:9002"}; using PolicyApp = webfront::BasicWFWithFrontend; PolicyApp app(webfront::WebFrontConfig{.documentRoot = std::filesystem::current_path(), .contentPolicy = policy}); + registerLifecycleStage(app); + app.onUIStarted([](PolicyApp::UI) { + int initial = 0; + lifecycleStage.compare_exchange_strong(initial, 1); + }); std::atomic completed = false; std::atomic escaped = false; std::atomic navigated = false; app.cppFunction("policyCompleted", [&] { + lifecycleStage = 8; completed = true; }); app.cppFunction("policyEscaped", [&] { + lifecycleStage = 8; escaped = true; }); app.cppFunction("policyNavigated", [&] { + lifecycleStage = 8; navigated = true; }); app.show("content-policy.html"); + lifecycleStage = 9; if (escaped) throw std::runtime_error("Frontend allowed an unmanaged popup"); if (completed == allowNavigation) @@ -511,8 +572,20 @@ int run(std::string_view mode) { int main(int argc, char** argv) { try { - // CEF subprocesses have their own argument list and must dispatch before mode parsing. + if (argc == 2 && std::string_view(argv[1]) == "watchdog-probe") { + LifecycleWatchdog watchdog(std::chrono::seconds(1)); + lifecycleStage = 3; + std::this_thread::sleep_for(std::chrono::seconds(5)); + return 0; + } +#ifdef WEBFRONT_EMBED_CEF + // CEF subprocesses must dispatch before starting any test threads. + webfront::detail::ensureFrontendInitialized(); +#endif + LifecycleWatchdog watchdog(std::chrono::seconds(45)); +#ifndef WEBFRONT_EMBED_CEF webfront::detail::ensureFrontendInitialized(); +#endif return argc == 2 ? run(argv[1]) : 1; } catch (const std::exception& error) { std::cerr << error.what() << '\n'; diff --git a/test/web/content-policy.html b/test/web/content-policy.html index 3d3ed8d0..1b765ca7 100644 --- a/test/web/content-policy.html +++ b/test/web/content-policy.html @@ -2,6 +2,7 @@ diff --git a/test/web/policy-escaped.html b/test/web/policy-escaped.html index d190ebee..101d672b 100644 --- a/test/web/policy-escaped.html +++ b/test/web/policy-escaped.html @@ -3,6 +3,7 @@ diff --git a/test/web/policy-escaped.html b/test/web/policy-escaped.html index 101d672b..c2546ba8 100644 --- a/test/web/policy-escaped.html +++ b/test/web/policy-escaped.html @@ -1,9 +1,12 @@ diff --git a/test/web/lifecycle-stage-probe.html b/test/web/lifecycle-stage-probe.html new file mode 100644 index 00000000..dcee793e --- /dev/null +++ b/test/web/lifecycle-stage-probe.html @@ -0,0 +1,14 @@ + + + diff --git a/test/web/policy-escaped.html b/test/web/policy-escaped.html index c2546ba8..c96c5537 100644 --- a/test/web/policy-escaped.html +++ b/test/web/policy-escaped.html @@ -3,10 +3,12 @@