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
40 changes: 37 additions & 3 deletions clean/installer/src/wg_core/record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "record.h"

#include <array>
#include <atomic>

#include "detect.h"
#include "gamerules.h"
Expand Down Expand Up @@ -350,14 +351,47 @@ size_t RemoveRuntimeFiles(const wstring& dir, const wstring& exeDir, const Recor
// programs it starts share one invisible console, so nothing appears on screen.
// state / self: the wronggpu-setup folder and the running uninstaller in it (empty: not this case); appData:
// %LOCALAPPDATA%\WrongGPU, removed with everything in it (empty: other games still use it).
static std::atomic<CleanupMoveFn> g_cleanupMove{nullptr}; // test seam (record.h SetCleanupMoveForTest)
void SetCleanupMoveForTest(CleanupMoveFn fn) { g_cleanupMove = fn; }

void StartCleanup(Context& ctx, const wstring& state, const wstring& self, const wstring& appData) {
// Detach this generation now. Delayed cleanup must never recursively delete the shared pathname,
// which a following install can already have recreated with its new model and runtime selection.
wstring retired;
if (!appData.empty() && IsDir(appData)) {
retired = appData + L".cleanup-" + std::to_wstring(GetCurrentProcessId()) + L"-" + std::to_wstring(GetTickCount64());
if (!MoveFileExW(appData.c_str(), retired.c_str(), MOVEFILE_WRITE_THROUGH)) {
LogLine(ctx, L"Shared data cleanup deferred: cannot detach " + appData + L"; it was left in place.");
// The name the folder is moved to is free and unique. It was <pid>-<GetTickCount64()> alone; that clock moves in
// ~16 ms steps, so a second cleanup of the same folder within one step (an install's restore, then the last game's
// uninstall) aimed at the previous cleanup's folder, which its cmd.exe deletes only seconds later: the move failed
// with ERROR_ALREADY_EXISTS and the folder was left in place.
static std::atomic<unsigned> sequence{0};
for (int k = 0; k < 64; ++k) {
retired = appData + L".cleanup-" + std::to_wstring(GetCurrentProcessId()) + L"-" + std::to_wstring(GetTickCount64()) + L"-" +
std::to_wstring(sequence++);
if (GetFileAttributesW(retired.c_str()) == INVALID_FILE_ATTRIBUTES) break;
}
// Nothing of WrongGPU's holds a file in the folder here (every write closes its handle, the log opens and closes
// per line). Another program can, briefly (a scanner reading a file just written): a sharing violation (32) or an
// access denied (5, how a folder rename fails while a file in it is open elsewhere) is tried again, at most
// kCleanupMoveTries times, 50-400 ms apart, each wait logged with the error. Any other error is final.
bool moved = false;
DWORD err = 0;
const CleanupMoveFn seam = g_cleanupMove.load();
const CleanupMoveFn move = seam ? seam : &MoveFileExW;
for (int t = 0; t < kCleanupMoveTries; ++t) {
if (move(appData.c_str(), retired.c_str(), MOVEFILE_WRITE_THROUGH)) { moved = true; break; }
err = GetLastError();
if ((err != ERROR_SHARING_VIOLATION && err != ERROR_ACCESS_DENIED) || t == kCleanupMoveTries - 1) break;
const DWORD wait = kCleanupMoveFirstWaitMs << t;
LogLine(ctx, L"Shared data cleanup: " + appData + L" is in use by another program (error " + std::to_wstring(err) +
(err == ERROR_SHARING_VIOLATION ? L", sharing violation" : L", access denied") + L"); trying again in " +
std::to_wstring(wait) + L" ms (try " + std::to_wstring(t + 2) + L" of " + std::to_wstring(kCleanupMoveTries) + L")");
Sleep(wait);
}
if (!moved) {
wstring why = ErrText(err);
while (!why.empty() && (why.back() == L'\n' || why.back() == L'\r' || why.back() == L' ' || why.back() == L'.')) why.pop_back();
LogLine(ctx, L"Shared data cleanup deferred: cannot detach " + appData + L" (error " + std::to_wstring(err) + L": " + why +
L"); it was left in place.");
retired.clear();
} else if (StartsWithI(ctx.logFile, appData + L"\\")) {
ctx.logFile = retired + ctx.logFile.substr(appData.size());
Expand Down
10 changes: 10 additions & 0 deletions clean/installer/src/wg_core/record.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ bool PortableLauncherUsesSharedData();
void NotePortableLauncher(const wstring& exe);
void UnregisterUninstall(const Record& m);
size_t RemoveRuntimeFiles(const wstring& dir, const wstring& exeDir, const Record& m, bool settings);
// The shared folder (appData) is moved aside, to a free name made unique with a process-wide sequence number, and deleted by a
// hidden cmd.exe. The move: a sharing violation (32) or an access denied (5: a scanner holding a file in the folder) is tried
// again, at most kCleanupMoveTries times (waits from kCleanupMoveFirstWaitMs doubling: 50, 100, 200, 400 ms), each wait logged
// with the error; any other error, or the last try, leaves the folder in place ("Shared data cleanup deferred ... (error N: ...)").
constexpr int kCleanupMoveTries = 5;
constexpr unsigned long kCleanupMoveFirstWaitMs = 50;
// Test seam: the call StartCleanup moves the folder with (MoveFileExW when unset). The tests put in one that fails with
// a chosen error for the first tries; nullptr puts MoveFileExW back.
using CleanupMoveFn = BOOL (WINAPI*)(LPCWSTR from, LPCWSTR to, DWORD flags);
void SetCleanupMoveForTest(CleanupMoveFn fn);
void StartCleanup(Context& ctx, const wstring& state, const wstring& self, const wstring& appData);
std::vector<wstring> DoRestore(Context& ctx, const wstring& dir, Ui& ui, bool failedInstall, bool keepState);
// rc6 L4: the player's things in the state folder that a failed install or update never removes when they were there before
Expand Down
86 changes: 86 additions & 0 deletions clean/installer/tests/wg_core_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Unit tests of wg_core (the installer's / launcher's engine) that need no game, no payload and no GPU: strings and
// paths, the install record, wronggpu.ini keys, the runtime options of a Model-M-only build, the Context's log file.
// Written for this project. Run by ctest ("wg-core") in a WRONGGPU_TESTING build.
#include <atomic>
#include <cstdio>
#include <iostream>
#include <stdexcept>
Expand Down Expand Up @@ -29,6 +30,19 @@ wstring TempFolder() {
EnsureDir(dir);
return dir;
}
// StartCleanup's folder move for the retry cases (record.h SetCleanupMoveForTest): the first g_moveFailFirst calls fail
// with g_moveError, later ones really move.
std::atomic<int> g_moveCalls{0}, g_moveFailFirst{0};
std::atomic<DWORD> g_moveError{0};
BOOL WINAPI FakeMove(LPCWSTR from, LPCWSTR to, DWORD flags) {
if (++g_moveCalls <= g_moveFailFirst) { SetLastError(g_moveError); return FALSE; }
return MoveFileExW(from, to, flags);
}
size_t CountIn(const string& s, const string& part) {
size_t n = 0;
for (size_t at = 0; (at = s.find(part, at)) != string::npos; ++at) ++n;
return n;
}
} // namespace

int main() {
Expand Down Expand Up @@ -241,6 +255,78 @@ int main() {
LogLine(ctx, L"second \u00e9");
string log;
require(ReadAll(ctx.logFile, log) && log.find("] first\r\n") != string::npos && log.find("] second \xc3\xa9\r\n") != string::npos, "LogLine appends UTF-8 lines");

// StartCleanup: the shared folder moved aside to a name that is free, with bounded retries. Synthetic folder in the
// temp folder; the hidden cmd.exe it starts deletes each moved-aside folder a few seconds later.
{
const wstring cleanRoot = Join(dir, L"cleanup"), appData = Join(cleanRoot, L"WrongGPU");
const wstring modelFile = Join(appData, L"model\\model.wgcm");
auto makeShared = [&] { EnsureDir(Parent(modelFile)); const char d[] = "synthetic model data"; WriteFileAtomic(modelFile, d, sizeof d - 1); };
auto contains = [](const string& s, const string& part) { return s.find(part) != string::npos; };
// two cleanups of one folder back to back, 20 runs (CI has one runner: not more). Within one GetTickCount64 step
// (~16 ms) the second move used to aim at the first one's folder, which its cmd.exe deletes seconds later: error
// 183, the folder left in place.
{
Context sctx;
sctx.logFile = Join(cleanRoot, L"cleanup-stress.log"); // outside the folder that is moved
const int runs = 20;
int gone = 0;
for (int i = 0; i < runs; ++i) {
makeShared();
StartCleanup(sctx, L"", L"", appData);
const bool first = !IsDir(appData);
makeShared();
StartCleanup(sctx, L"", L"", appData);
if (first && !IsDir(appData)) ++gone;
}
string slog;
ReadAll(sctx.logFile, slog);
std::printf("wg-core: cleanup stress %d/%d (deferred %zu)\n", gone, runs, CountIn(slog, "cleanup deferred"));
require(gone == runs && !contains(slog, "cleanup deferred"), "two cleanups of one folder back to back: it goes both times, 20 of 20 runs");
}
// the bounded retry (error 32 sharing violation, error 5 access denied: a scanner's open file), through the test
// seam: fails for the first N tries, then moves; or fails every time and ends deferred; any other error is final
{
Context rctx;
rctx.logFile = Join(cleanRoot, L"cleanup-retry.log");
struct Case { DWORD error; int failFirst; bool gone; int calls; size_t retries; const char* what; };
const Case cases[] = {
{ERROR_ACCESS_DENIED, 3, true, 4, 3, "error 5 for 3 tries, then the move: the folder goes after 3 logged retries"},
{ERROR_ACCESS_DENIED, 1000, false, kCleanupMoveTries, kCleanupMoveTries - 1,
"error 5 every time: 5 tries (4 logged retries), then deferred with the error named, the folder left in place"},
{ERROR_SHARING_VIOLATION, 2, true, 3, 2, "error 32 for 2 tries, then the move: the folder goes"},
{ERROR_ALREADY_EXISTS, 1, false, 1, 0, "error 183 once: no retry, deferred at once"}};
for (const Case& rc : cases) {
DeleteFileW(rctx.logFile.c_str());
makeShared();
g_moveCalls = 0; g_moveFailFirst = rc.failFirst; g_moveError = rc.error;
SetCleanupMoveForTest(&FakeMove);
const ULONGLONG t0 = GetTickCount64();
StartCleanup(rctx, L"", L"", appData);
const ULONGLONG took = GetTickCount64() - t0;
SetCleanupMoveForTest(nullptr);
string rlog;
ReadAll(rctx.logFile, rlog);
const string code = "(error " + std::to_string(rc.error);
const size_t retries = CountIn(rlog, code + (rc.error == ERROR_SHARING_VIOLATION ? ", sharing violation); trying again" : ", access denied); trying again"));
const size_t deferred = CountIn(rlog, "cleanup deferred: cannot detach " + ToUtf8(appData) + " " + code + ":");
std::printf("wg-core: cleanup retry, error %lu x %d: %d tries, %zu retries logged, deferred %zu, %llu ms\n", rc.error, rc.failFirst,
g_moveCalls.load(), retries, deferred, (unsigned long long)took);
require(IsDir(appData) == !rc.gone && g_moveCalls == rc.calls && retries == rc.retries && deferred == (rc.gone ? 0u : 1u) && took < 5000, rc.what);
}
if (IsDir(appData)) { // the deferred cases left it: the real move now
StartCleanup(rctx, L"", L"", appData);
require(!IsDir(appData), "after the retry cases: the folder goes");
}
}
for (int i = 0; i < 600; ++i) { // the hidden cleanups delete the moved-aside folders (ping -n 3 first)
bool left = false;
for (auto& d : ListDirs(cleanRoot)) left = left || d.find(L".cleanup-") != wstring::npos;
if (!left) break;
Sleep(100);
}
}

RemoveTree(dir);
require(!IsDir(dir), "RemoveTree");
} catch (const std::exception& ex) {
Expand Down
Loading