From 6f1ff05f9769114bd8abd3eee67bf08969d6d114 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Mon, 9 Nov 2026 13:16:50 +0100 Subject: [PATCH 1/2] Retry the withdrawal of a discovery record that a reader holds open on Windows (#501) A reader that has the record open (discovery::list reads it with std::ifstream, which does not share deletion) made Registration::remove() fail with a sharing violation. The error was ignored, so a withdrawn session stayed listed while its process lived. This failed intermittently in CI on the Windows job, where the unit test polls list() every 10 ms while the controller withdraws the record. --- include/webfront/automation/Discovery.hpp | 27 ++++++++++++++++++++--- test/unit/AutomationDiscoveryTests.cpp | 26 ++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/include/webfront/automation/Discovery.hpp b/include/webfront/automation/Discovery.hpp index 1e59bad2..0ae77ae2 100644 --- a/include/webfront/automation/Discovery.hpp +++ b/include/webfront/automation/Discovery.hpp @@ -11,11 +11,13 @@ #include "Json.hpp" #include +#include #include #include #include #include #include +#include #include #ifndef _WIN32 #include @@ -144,6 +146,26 @@ inline void preparePrivateDirectory(const std::filesystem::path& directory) { #endif } +/// Removes a file, retrying for a short while on Windows: a reader that has the record open (`list` reads it without +/// sharing deletion) makes the removal fail with a sharing violation, and giving up would leave a withdrawn session +/// listed for as long as its process lives. Other failures, and every failure on POSIX, are final. +inline void removeWithRetry(const std::filesystem::path& file) noexcept { +#ifdef _WIN32 + constexpr int attempts = 200; +#else + constexpr int attempts = 1; +#endif + for (int attempt = 0; attempt < attempts; ++attempt) { + std::error_code error; + std::filesystem::remove(file, error); + if (!error) + return; +#ifdef _WIN32 + std::this_thread::sleep_for(std::chrono::milliseconds{5}); +#endif + } +} + /// The record of one shared session, present as long as this object lives. class Registration { public: @@ -167,9 +189,8 @@ class Registration { const std::scoped_lock lock{mutex}; if (file.empty()) return; - std::error_code ignored; - std::filesystem::remove(file, ignored); - std::filesystem::remove(file.string() + ".new", ignored); + removeWithRetry(file); + removeWithRetry(file.string() + ".new"); file.clear(); } diff --git a/test/unit/AutomationDiscoveryTests.cpp b/test/unit/AutomationDiscoveryTests.cpp index 205cb8cc..4aa901e9 100644 --- a/test/unit/AutomationDiscoveryTests.cpp +++ b/test/unit/AutomationDiscoveryTests.cpp @@ -8,7 +8,9 @@ #include #endif #include +#include #include +#include #include #include @@ -65,6 +67,30 @@ discovery::Entry entry(std::string session, long pid = discovery::currentProcess constexpr long deadProcess = 2147483000L; } // namespace +SCENARIO("A record that a reader holds open is still withdrawn") { + webfront::test::TemporaryDirectory root; + GIVEN("a registration whose record is open in a reader, as `list` reads it") { + const auto directory = root.path() / "sessions"; + discovery::Registration registration{directory, entry("alpha")}; + const auto record = directory / "alpha.json"; + auto reader = std::make_unique(record, std::ios::binary); + REQUIRE(reader->is_open()); + // Windows refuses to delete a file that is open without shared deletion: the withdrawal waits for the reader. + std::thread release{[&reader] { + std::this_thread::sleep_for(std::chrono::milliseconds{100}); + reader.reset(); + }}; + WHEN("the session is withdrawn while the reader still holds it") { + registration.remove(); + release.join(); + THEN("the record is gone and no session is listed") { + REQUIRE_FALSE(std::filesystem::exists(record)); + REQUIRE(discovery::list(directory).empty()); + } + } + } +} + SCENARIO("A shared session is listed with its grants and never with its capability") { webfront::test::TemporaryDirectory root; GIVEN("a registration in a discovery directory") { From 46c42f06f0a284c2332ad4e4313c5622bced7b36 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Mon, 9 Nov 2026 13:30:04 +0100 Subject: [PATCH 2/2] Retry a discovery record's removal only on transient Windows errors, until a steady_clock deadline (#506) --- include/webfront/automation/Discovery.hpp | 25 ++++++++++++++--------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/include/webfront/automation/Discovery.hpp b/include/webfront/automation/Discovery.hpp index 0ae77ae2..923e06d2 100644 --- a/include/webfront/automation/Discovery.hpp +++ b/include/webfront/automation/Discovery.hpp @@ -146,23 +146,28 @@ inline void preparePrivateDirectory(const std::filesystem::path& directory) { #endif } -/// Removes a file, retrying for a short while on Windows: a reader that has the record open (`list` reads it without -/// sharing deletion) makes the removal fail with a sharing violation, and giving up would leave a withdrawn session -/// listed for as long as its process lives. Other failures, and every failure on POSIX, are final. -inline void removeWithRetry(const std::filesystem::path& file) noexcept { +/// Whether a failed removal may succeed shortly: on Windows, a reader that has the record open (`list` reads it without +/// sharing deletion) makes it fail with a sharing or lock violation, or with an access denial while a deletion is pending. +/// Nothing is transient on POSIX, where unlinking an open file succeeds. +inline bool transientRemovalFailure([[maybe_unused]] const std::error_code& error) noexcept { #ifdef _WIN32 - constexpr int attempts = 200; + return error.category() == std::system_category() + && (error.value() == ERROR_SHARING_VIOLATION || error.value() == ERROR_LOCK_VIOLATION || error.value() == ERROR_ACCESS_DENIED); #else - constexpr int attempts = 1; + return false; #endif - for (int attempt = 0; attempt < attempts; ++attempt) { +} + +/// Removes a file. A transient failure (see `transientRemovalFailure`) is retried until a one second deadline, because +/// giving up would leave a withdrawn session listed for as long as its process lives; any other failure is final. +inline void removeWithRetry(const std::filesystem::path& file) noexcept { + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds{1}; + for (;;) { std::error_code error; std::filesystem::remove(file, error); - if (!error) + if (!error || !transientRemovalFailure(error) || std::chrono::steady_clock::now() >= deadline) return; -#ifdef _WIN32 std::this_thread::sleep_for(std::chrono::milliseconds{5}); -#endif } }