From 9fe49c4c577f025ae7395677f042ec3345a0fcfd Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:30:57 +0100 Subject: [PATCH 1/3] Saturate the automation startup deadline (#544) A startupTimeout of seconds::max() overflowed the clock's tick count when added to the launch time, which is undefined behavior and gave a deadline already past: the session closed at once. startupDeadline saturates at time_point::max(). --- include/webfront/automation/Enable.hpp | 13 ++++++++++++- test/unit/AutomationDiscoveryTests.cpp | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/include/webfront/automation/Enable.hpp b/include/webfront/automation/Enable.hpp index 4eb83a89..bd6c0c07 100644 --- a/include/webfront/automation/Enable.hpp +++ b/include/webfront/automation/Enable.hpp @@ -31,6 +31,17 @@ #include namespace webfront::automation { +/// The end of the startup window, saturated: a very large timeout (`seconds::max()` to disable the delay) must not overflow +/// the clock's tick count, which is undefined behavior and would yield a deadline already past. +[[nodiscard]] constexpr std::chrono::steady_clock::time_point startupDeadline(std::chrono::steady_clock::time_point launched, + std::chrono::seconds startup) noexcept { + using Clock = std::chrono::steady_clock; + const auto remaining = Clock::time_point::max() - launched; + if (startup >= std::chrono::duration_cast(remaining)) + return Clock::time_point::max(); + return launched + startup; +} + struct AutomationOptions { /// Granted operations (`check`, `build`, `test`, `preview`, `inspect`, `interact`, and the setup grants). None /// leaves only session discovery. @@ -87,7 +98,7 @@ class Automation { // A renderer in a loop never runs that request: after the grace the window is declared unresponsive. supervisor = std::jthread([this](std::stop_token stop) { while (!stop.stop_requested()) { - if (session->expire(Clock::now()) || session->expireStartup(launched + startup, Clock::now())) + if (session->expire(Clock::now()) || session->expireStartup(startupDeadline(launched, startup), Clock::now())) session->close("Native automation deadline exceeded"); if (session->isClosed()) { // The grace starts when the window is asked to close, so a closeBrowser() that blocks does not extend it. diff --git a/test/unit/AutomationDiscoveryTests.cpp b/test/unit/AutomationDiscoveryTests.cpp index bcd3bda0..918adb86 100644 --- a/test/unit/AutomationDiscoveryTests.cpp +++ b/test/unit/AutomationDiscoveryTests.cpp @@ -518,3 +518,19 @@ const Scenario defaultAutomation{"The default automation clock supervises a rend require(automation->controller()->isClosed()); require(discovery::list(root.path() / "sessions").empty()); }}; + +const Scenario startupDeadlineSaturates{"A startup timeout too large for the clock never expires", [] { + using namespace std::chrono; + const auto launched = steady_clock::now(); + given("an ordinary timeout", [&] { + then("the deadline is the launch plus the timeout", [&] { + check(startupDeadline(launched, seconds{30}) == launched + seconds{30}); + }); + }); + given("the largest representable timeout", [&] { + then("the deadline saturates instead of overflowing into the past", [&] { + check(startupDeadline(launched, seconds::max()) == steady_clock::time_point::max()); + check(startupDeadline(launched, seconds::max()) > launched); + }); + }); +}}; From fcc1604a3afb80fec9ebe80a25032fdfe9cfd083 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Sun, 15 Nov 2026 23:22:59 +0100 Subject: [PATCH 2/3] Saturate every automation delay and validate its options (#544) startupDeadline was a point fix: the close grace still added an unchecked duration to the clock, and the helper subtracted from time_point::max() (signed overflow for a negative clock origin). - deadlineAfter compares in whole seconds, which cannot overflow for any origin, and is used for both the startup window and the close grace - a non-positive startupTimeout and a negative closeGrace are refused - tests cover negative origins, zero/negative delays and the supervisor with seconds::max() / milliseconds::max() --- include/webfront/automation/Enable.hpp | 38 +++++++++---- test/unit/AutomationDiscoveryTests.cpp | 75 +++++++++++++++++++++++--- 2 files changed, 96 insertions(+), 17 deletions(-) diff --git a/include/webfront/automation/Enable.hpp b/include/webfront/automation/Enable.hpp index bd6c0c07..148c2b63 100644 --- a/include/webfront/automation/Enable.hpp +++ b/include/webfront/automation/Enable.hpp @@ -31,15 +31,27 @@ #include namespace webfront::automation { -/// The end of the startup window, saturated: a very large timeout (`seconds::max()` to disable the delay) must not overflow -/// the clock's tick count, which is undefined behavior and would yield a deadline already past. +/// `from + after`, saturated: a very large duration (`seconds::max()` to disable a delay) must not overflow the clock's tick +/// count, which is undefined behavior and would yield a deadline already past. The comparison is made in whole seconds, which +/// cannot overflow for any clock origin (negative included), and keeps a one-second margin for the truncation, so the sum is +/// only computed when it is representable. A zero or negative duration returns `from`. +template +[[nodiscard]] constexpr std::chrono::steady_clock::time_point deadlineAfter(std::chrono::steady_clock::time_point from, Duration after) noexcept { + using Clock = std::chrono::steady_clock; + using std::chrono::duration_cast; + using std::chrono::seconds; + if (after <= Duration::zero()) + return from; + const auto room = duration_cast(Clock::time_point::max().time_since_epoch()) - duration_cast(from.time_since_epoch()); + if (duration_cast(after) + seconds{1} >= room) + return Clock::time_point::max(); + return from + after; +} + +/// The end of the startup window, saturated (see `deadlineAfter`). [[nodiscard]] constexpr std::chrono::steady_clock::time_point startupDeadline(std::chrono::steady_clock::time_point launched, std::chrono::seconds startup) noexcept { - using Clock = std::chrono::steady_clock; - const auto remaining = Clock::time_point::max() - launched; - if (startup >= std::chrono::duration_cast(remaining)) - return Clock::time_point::max(); - return launched + startup; + return deadlineAfter(launched, startup); } struct AutomationOptions { @@ -57,10 +69,12 @@ struct AutomationOptions { /// Per-operation native deadline. std::chrono::seconds timeout{120}; /// How long the window may take to claim the session, counted from the activation: a renderer blocked before its first - /// poll has no operation for `timeout` to expire. Past it the session is revoked like a deadline. + /// poll has no operation for `timeout` to expire. Past it the session is revoked like a deadline. Must be positive; + /// `seconds::max()` disables the delay. std::chrono::seconds startupTimeout{120}; /// After a deadline revokes the session, how long the window may take to close on its own. `window.close()` cannot run /// in a renderer stuck in a loop, so past this grace the endpoint and record are removed and `onUnresponsive` is called. + /// Must not be negative; `milliseconds::max()` waits for the window without limit. std::chrono::milliseconds closeGrace{5000}; /// What to do with a window that did not close. By default the process ends with status 72 (as `webfront-agent` /// does): `show()` cannot be interrupted from another thread. Set it to close the application another way. @@ -77,7 +91,7 @@ class Automation { "Automation's Clock must return std::chrono::steady_clock::time_point"); public: - /// @throws std::invalid_argument for an unknown grant, an empty `listen` path, or a `listen` directory inside the project + /// @throws std::invalid_argument for an unknown grant, a non-positive `startupTimeout`, a negative `closeGrace`, an empty `listen` path, or a `listen` directory inside the project /// @throws std::runtime_error (including std::filesystem::filesystem_error) when the endpoint or the discovery directory cannot be /// created or is refused (an existing one that other users can read). Nothing stays registered in the application. Automation(App& app, AutomationOptions options) @@ -102,7 +116,7 @@ class Automation { session->close("Native automation deadline exceeded"); if (session->isClosed()) { // The grace starts when the window is asked to close, so a closeBrowser() that blocks does not extend it. - const auto deadline = Clock::now() + grace; + const auto deadline = deadlineAfter(Clock::now(), grace); binding.closeBrowser(); // Whatever revoked the session (a deadline, a signal, an attached client's --shutdown, the page's own failure // report, a lost WebSocket), the window was only asked to close: a stuck renderer ignores it, and a lost @@ -183,6 +197,10 @@ class Automation { static std::shared_ptr make(App& app, const AutomationOptions& options) { if (options.listen.empty()) throw std::invalid_argument("Automation needs a new private `listen` directory for its endpoint"); + if (options.startupTimeout <= std::chrono::seconds::zero()) + throw std::invalid_argument("Automation startupTimeout must be positive"); + if (options.closeGrace < std::chrono::milliseconds::zero()) + throw std::invalid_argument("Automation closeGrace must not be negative"); return std::make_shared(SessionOptions{.project = app.projectDirectory(), .allow = options.grants, .timeout = options.timeout, .callbacks = options.callbacks}); } static discovery::Entry record(const Session& session, const std::filesystem::path& endpoint) { diff --git a/test/unit/AutomationDiscoveryTests.cpp b/test/unit/AutomationDiscoveryTests.cpp index 918adb86..e1ac0500 100644 --- a/test/unit/AutomationDiscoveryTests.cpp +++ b/test/unit/AutomationDiscoveryTests.cpp @@ -519,18 +519,79 @@ const Scenario defaultAutomation{"The default automation clock supervises a rend require(discovery::list(root.path() / "sessions").empty()); }}; -const Scenario startupDeadlineSaturates{"A startup timeout too large for the clock never expires", [] { +const Scenario startupDeadlineSaturates{"A delay too large for the clock never expires, whatever the clock origin", [] { using namespace std::chrono; - const auto launched = steady_clock::now(); + const auto now = steady_clock::now(); + const auto negative = steady_clock::time_point{steady_clock::duration{-1'000'000'000'000}}; + const auto limit = steady_clock::time_point::max(); given("an ordinary timeout", [&] { - then("the deadline is the launch plus the timeout", [&] { - check(startupDeadline(launched, seconds{30}) == launched + seconds{30}); + then("the deadline is the origin plus the timeout, for a positive or a negative origin", [&] { + check(startupDeadline(now, seconds{30}) == now + seconds{30}); + check(startupDeadline(negative, seconds{30}) == negative + seconds{30}); + check(deadlineAfter(now, milliseconds{100}) == now + milliseconds{100}); }); }); - given("the largest representable timeout", [&] { + given("a zero or negative delay", [&] { + then("the deadline is the origin itself", [&] { + check(startupDeadline(negative, seconds{0}) == negative); + check(startupDeadline(now, seconds{-5}) == now); + check(deadlineAfter(negative, milliseconds{-1}) == negative); + }); + }); + given("the largest representable delay", [&] { then("the deadline saturates instead of overflowing into the past", [&] { - check(startupDeadline(launched, seconds::max()) == steady_clock::time_point::max()); - check(startupDeadline(launched, seconds::max()) > launched); + check(startupDeadline(now, seconds::max()) == limit); + check(startupDeadline(negative, seconds::max()) == limit); + check(startupDeadline(steady_clock::time_point::min(), seconds::max()) == limit); + check(deadlineAfter(now, milliseconds::max()) == limit); + check(deadlineAfter(negative, milliseconds::max()) == limit); + }); + }); +}}; + +const Scenario unboundedDelaysKeepTheSession{"Unbounded startup and grace options neither revoke the session nor end the window", [] { + SupervisorClock::reset(); + webfront::test::TemporaryDirectory root; + std::filesystem::create_directory(root.path() / "project"); + ConsumerApp app{.project = root.path() / "project"}; + std::atomic unresponsive{0}; + const auto options = [&](std::chrono::seconds startup, std::chrono::milliseconds grace) { + return AutomationOptions{.grants = {"check"}, + .listen = root.path() / "endpoint", + .discovery = root.path() / "sessions", + .startupTimeout = startup, + .closeGrace = grace, + .onUnresponsive = + [&] { + ++unresponsive; + }, + .announce = false}; + }; + given("a startup timeout of seconds::max()", [&] { + Automation automation{app, options(std::chrono::seconds::max(), std::chrono::milliseconds{100})}; + then("a window that never claims the session is not revoked", [&] { + advanceSupervisor(std::chrono::hours{24}); + require(!automation.controller()->isClosed()); + require(unresponsive.load() == 0); + }); + }); + given("a close grace of milliseconds::max()", [&] { + Automation automation{app, options(std::chrono::seconds{120}, std::chrono::milliseconds::max())}; + automation.controller()->close("Developer window disconnected"); + awaitCloseGrace(&automation); + then("the window is never declared unresponsive", [&] { + advanceSupervisor(std::chrono::hours{24}); + require(unresponsive.load() == 0); + }); + }); + given("a non-positive startup timeout or a negative grace", [&] { + then("the activation is refused before anything is registered", [&] { + requireThrows([&] { + Automation{app, options(std::chrono::seconds{0}, std::chrono::milliseconds{100})}; + }); + requireThrows([&] { + Automation{app, options(std::chrono::seconds{120}, std::chrono::milliseconds{-1})}; + }); }); }); }}; From c7cf2e2effbdca38460bf275cf11c9422bcb0c63 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Sun, 15 Nov 2026 23:26:07 +0100 Subject: [PATCH 3/3] Make the saturated deadline free of any overflow (#544) The margin `seconds{1}` added to a `seconds::max()` delay overflowed in the check meant to saturate it, and `from + after` converted `after` to the clock's ticks even when only the deadline, not the duration, is representable (origin near time_point::min()). The room is compared without incrementing the delay, and the sum is made in two steps when the delay exceeds the tick range. The new test covers the negative origin with a delay beyond the tick range. --- include/webfront/automation/Enable.hpp | 21 +++++++++++++++------ test/unit/AutomationDiscoveryTests.cpp | 10 ++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/include/webfront/automation/Enable.hpp b/include/webfront/automation/Enable.hpp index 148c2b63..b5118676 100644 --- a/include/webfront/automation/Enable.hpp +++ b/include/webfront/automation/Enable.hpp @@ -32,9 +32,10 @@ namespace webfront::automation { /// `from + after`, saturated: a very large duration (`seconds::max()` to disable a delay) must not overflow the clock's tick -/// count, which is undefined behavior and would yield a deadline already past. The comparison is made in whole seconds, which -/// cannot overflow for any clock origin (negative included), and keeps a one-second margin for the truncation, so the sum is -/// only computed when it is representable. A zero or negative duration returns `from`. +/// count, which is undefined behavior and would yield a deadline already past. Nothing is computed that can overflow: the room +/// left before `time_point::max()` is measured in whole seconds (never more than twice the clock's range, whatever the origin, +/// negative included), `after` is compared to it without being incremented, and the sum is made in two steps when `after` +/// alone exceeds the clock's tick range while the deadline stays representable. A zero or negative duration returns `from`. template [[nodiscard]] constexpr std::chrono::steady_clock::time_point deadlineAfter(std::chrono::steady_clock::time_point from, Duration after) noexcept { using Clock = std::chrono::steady_clock; @@ -42,10 +43,18 @@ template using std::chrono::seconds; if (after <= Duration::zero()) return from; - const auto room = duration_cast(Clock::time_point::max().time_since_epoch()) - duration_cast(from.time_since_epoch()); - if (duration_cast(after) + seconds{1} >= room) + const auto capacity = duration_cast(Clock::time_point::max().time_since_epoch()); + const auto room = capacity - duration_cast(from.time_since_epoch()); + const auto whole = duration_cast(after); + // One second of margin for the truncations; `room - 1` cannot overflow, `room` being at least 1. + if (whole >= room - seconds{1}) return Clock::time_point::max(); - return from + after; + if (whole < capacity) + return from + after; // `after` converts to ticks: it is below the clock's range + // `after` exceeds the range of the ticks but the deadline does not: `from` is negative, so a first step of the largest + // whole number of `Duration` the clock can hold stays representable, then the rest. + const auto step = duration_cast(Clock::duration::max()); + return from + duration_cast(step) + (after - step); } /// The end of the startup window, saturated (see `deadlineAfter`). diff --git a/test/unit/AutomationDiscoveryTests.cpp b/test/unit/AutomationDiscoveryTests.cpp index e1ac0500..a228921f 100644 --- a/test/unit/AutomationDiscoveryTests.cpp +++ b/test/unit/AutomationDiscoveryTests.cpp @@ -547,6 +547,16 @@ const Scenario startupDeadlineSaturates{"A delay too large for the clock never e check(deadlineAfter(negative, milliseconds::max()) == limit); }); }); + given("a delay beyond the clock's tick range from an origin that makes the deadline representable", [&] { + then("the deadline is exact instead of overflowing in the conversion", [&] { + if constexpr (std::is_same_v && sizeof(steady_clock::rep) == 8) { + const auto from = steady_clock::time_point::min(); + // min() + 1e10 s = -9223372036854775808 + 1e19 ticks + check(deadlineAfter(from, seconds{10'000'000'000}) == steady_clock::time_point{nanoseconds{776'627'963'145'224'192LL}}); + check(deadlineAfter(from, milliseconds{10'000'000'000'000}) == steady_clock::time_point{nanoseconds{776'627'963'145'224'192LL}}); + } + }); + }); }}; const Scenario unboundedDelaysKeepTheSession{"Unbounded startup and grace options neither revoke the session nor end the window", [] {