diff --git a/include/webfront/automation/Discovery.hpp b/include/webfront/automation/Discovery.hpp index 1e59bad2..923e06d2 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,31 @@ inline void preparePrivateDirectory(const std::filesystem::path& directory) { #endif } +/// 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 + return error.category() == std::system_category() + && (error.value() == ERROR_SHARING_VIOLATION || error.value() == ERROR_LOCK_VIOLATION || error.value() == ERROR_ACCESS_DENIED); +#else + return false; +#endif +} + +/// 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 || !transientRemovalFailure(error) || std::chrono::steady_clock::now() >= deadline) + return; + std::this_thread::sleep_for(std::chrono::milliseconds{5}); + } +} + /// The record of one shared session, present as long as this object lives. class Registration { public: @@ -167,9 +194,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") {