Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions include/webfront/automation/Discovery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
#include "Json.hpp"

#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <mutex>
#include <optional>
#include <thread>
#include <utility>
#ifndef _WIN32
#include <csignal>
Expand Down Expand Up @@ -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:
Expand All @@ -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();
}

Expand Down
26 changes: 26 additions & 0 deletions test/unit/AutomationDiscoveryTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#include <sys/stat.h>
#endif
#include <atomic>
#include <chrono>
#include <functional>
#include <memory>
#include <thread>
#include <sstream>

Expand Down Expand Up @@ -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<std::ifstream>(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") {
Expand Down
Loading