From 4805b2f879948557f3b831510308b41519792c4c Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Sat, 31 Oct 2026 12:08:25 +0100 Subject: [PATCH 1/2] Remove the Windows agent's profile after a forced exit [#439] The WebView2 processes can hold the profile past the 2 s in-process retry, which then leaves it behind once the agent exits. A detached cmd.exe repeats the removal for about 20 s. The watchdog case's second CLI invocation now has a 45 s limit. --- docs/CHANGELOG.md | 5 +++++ js/developer/tools/WebFrontAgent.cpp | 30 ++++++++++++++++++++++++++++ test/tools/AutomationClient.py | 2 +- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 539160d7..63e36564 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -19,6 +19,11 @@ value, so a conflicting former name cannot change that prefixed cache entry. CI, the documentation and `TestOptionsConfiguration` use the new names. +- Remove the Windows agent's native profile after a forced exit (#439): when WebView2 processes still held the profile + at the end of the 2 s in-process retry, the agent exited and left it behind, so `AutomationCliWatchdog` failed + intermittently on loaded runners. A detached `cmd.exe` now repeats the removal for about 20 s, and the case's + second CLI invocation has a 45 s limit instead of 15 s. + ## 0.6.0 - 2026-10-31 - Build the React store tests in Release with GCC 16 (#429): at -O2 and above, GCC reported the moved-from path of diff --git a/js/developer/tools/WebFrontAgent.cpp b/js/developer/tools/WebFrontAgent.cpp index f78c2d54..cbd3d1c7 100644 --- a/js/developer/tools/WebFrontAgent.cpp +++ b/js/developer/tools/WebFrontAgent.cpp @@ -137,6 +137,34 @@ void releaseWebView(std::chrono::milliseconds grace) noexcept { // Without the process list, the removal below still retries the transient errors. } } +// The WebView2 processes can keep the profile open past the in-process retry, and the agent cannot wait longer when its +// exit is forced. A detached shell, outside the agent's job and console, repeats the removal for about 20 s; false when +// it cannot be started. +bool removeAfterExit(const std::filesystem::path& path) noexcept { + try { + const auto profile = path.wstring(); + // Inside quotes, cmd still expands these. + if (profile.find_first_of(L"%\"^!&") != std::wstring::npos) + return false; + std::wstring command = L"cmd.exe /d /c \"for /l %i in (1,1,20) do @if exist \"" + profile + L"\" (rmdir /s /q \"" + + profile + L"\" 2>nul & ping -n 2 127.0.0.1 >nul)\""; + STARTUPINFOW startup{}; + startup.cb = sizeof startup; + PROCESS_INFORMATION information{}; + for (const DWORD breakaway : {DWORD{CREATE_BREAKAWAY_FROM_JOB}, DWORD{0}}) { + if (CreateProcessW(nullptr, command.data(), nullptr, nullptr, FALSE, + CREATE_NO_WINDOW | DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP | breakaway, nullptr, nullptr, + &startup, &information)) { + CloseHandle(information.hThread); + CloseHandle(information.hProcess); + return true; + } + } + return false; + } catch (...) { + return false; + } +} #elif defined(__linux__) // The parent and state ('Z' for a zombie) of a process, from /proc//stat; nothing when it is gone. std::optional> processStat(pid_t pid) { @@ -406,6 +434,8 @@ struct Profile { std::this_thread::sleep_for(std::chrono::milliseconds{20}); continue; } + if (removeAfterExit(path)) + return; #endif std::cerr << "Automation: cannot remove temporary profile: " << error.message() << '\n'; return; diff --git a/test/tools/AutomationClient.py b/test/tools/AutomationClient.py index 36cdca3f..8273d2fd 100644 --- a/test/tools/AutomationClient.py +++ b/test/tools/AutomationClient.py @@ -344,7 +344,7 @@ def watchdog(args): failed_command[failed_command.index("--environment") + 1] = str(failed_environment) failed_command[failed_command.index("--integrity") + 1] = "sha512-" + base64.b64encode(hashlib.sha512(failed_environment.read_bytes()).digest()).decode() failed = subprocess.run([args.agent, *failed_command, "--listen", str(endpoint), "--operation", "build"], - capture_output=True, text=True, encoding="utf-8", timeout=15) + capture_output=True, text=True, encoding="utf-8", timeout=45) assert failed.returncode == 70, (failed.returncode, failed.stdout, failed.stderr) assert "Automation startup: B" + "é" * 511 + "\n" in failed.stderr, failed.stderr assert not endpoint.exists() From 343bb37b7e223ed08020f670feb17c7705859940 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Sat, 31 Oct 2026 13:46:12 +0100 Subject: [PATCH 2/2] Fix Windows profile cleanup review findings --- docs/CHANGELOG.md | 5 ++-- js/developer/tools/WebFrontAgent.cpp | 40 +++++++++++++++++++++------- test/tools/AutomationClient.py | 34 +++++++++++++++++++++-- 3 files changed, 66 insertions(+), 13 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 63e36564..d8513bf3 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -21,8 +21,9 @@ - Remove the Windows agent's native profile after a forced exit (#439): when WebView2 processes still held the profile at the end of the 2 s in-process retry, the agent exited and left it behind, so `AutomationCliWatchdog` failed - intermittently on loaded runners. A detached `cmd.exe` now repeats the removal for about 20 s, and the case's - second CLI invocation has a 45 s limit instead of 15 s. + intermittently on loaded runners. A detached agent process now retries removal for about 20 s, including for Unicode + paths and paths containing shell metacharacters. The watchdog test waits for the cleanup window, and its second CLI + invocation has a 45 s limit instead of 15 s. ## 0.6.0 - 2026-10-31 diff --git a/js/developer/tools/WebFrontAgent.cpp b/js/developer/tools/WebFrontAgent.cpp index cbd3d1c7..f8bdd98b 100644 --- a/js/developer/tools/WebFrontAgent.cpp +++ b/js/developer/tools/WebFrontAgent.cpp @@ -137,22 +137,21 @@ void releaseWebView(std::chrono::milliseconds grace) noexcept { // Without the process list, the removal below still retries the transient errors. } } -// The WebView2 processes can keep the profile open past the in-process retry, and the agent cannot wait longer when its -// exit is forced. A detached shell, outside the agent's job and console, repeats the removal for about 20 s; false when -// it cannot be started. +// A separate agent process can retry after a forced exit without interpreting the profile path as shell syntax. bool removeAfterExit(const std::filesystem::path& path) noexcept { try { - const auto profile = path.wstring(); - // Inside quotes, cmd still expands these. - if (profile.find_first_of(L"%\"^!&") != std::wstring::npos) + std::wstring executable(32768, L'\0'); + const DWORD length = GetModuleFileNameW(nullptr, executable.data(), static_cast(executable.size())); + if (!length || length >= executable.size()) return false; - std::wstring command = L"cmd.exe /d /c \"for /l %i in (1,1,20) do @if exist \"" + profile + L"\" (rmdir /s /q \"" - + profile + L"\" 2>nul & ping -n 2 127.0.0.1 >nul)\""; + executable.resize(length); + const std::wstring original = L"\"" + executable + L"\" --cleanup-profile \"" + path.wstring() + L"\""; STARTUPINFOW startup{}; startup.cb = sizeof startup; PROCESS_INFORMATION information{}; for (const DWORD breakaway : {DWORD{CREATE_BREAKAWAY_FROM_JOB}, DWORD{0}}) { - if (CreateProcessW(nullptr, command.data(), nullptr, nullptr, FALSE, + auto command = original; // CreateProcessW may modify the command line even on failure. + if (CreateProcessW(executable.c_str(), command.data(), nullptr, nullptr, FALSE, CREATE_NO_WINDOW | DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP | breakaway, nullptr, nullptr, &startup, &information)) { CloseHandle(information.hThread); @@ -165,6 +164,25 @@ bool removeAfterExit(const std::filesystem::path& path) noexcept { return false; } } + +int cleanupProfile() { + // The launcher supplies a quoted path as the final argument. Read the Unicode command line directly: argv uses the + // process code page and cannot represent every Windows path. + const std::wstring command = GetCommandLineW(); + const std::wstring marker = L" --cleanup-profile \""; + const auto begin = command.find(marker); + if (begin == std::wstring::npos || command.back() != L'"') + return 1; + const std::filesystem::path path{command.substr(begin + marker.size(), command.size() - begin - marker.size() - 1)}; + for (int attempt = 0; attempt != 20; ++attempt) { + std::error_code error; + std::filesystem::remove_all(path, error); + if (!error) + return 0; + std::this_thread::sleep_for(std::chrono::seconds{1}); + } + return 1; +} #elif defined(__linux__) // The parent and state ('Z' for a zombie) of a process, from /proc//stat; nothing when it is gone. std::optional> processStat(pid_t pid) { @@ -687,6 +705,10 @@ Options parse(int argc, char** argv) { int main(int argc, char** argv) { try { +#ifdef _WIN32 + if (argc == 3 && std::string_view(argv[1]) == "--cleanup-profile") + return cleanupProfile(); +#endif #ifndef _WIN32 std::signal(SIGPIPE, SIG_IGN); #endif diff --git a/test/tools/AutomationClient.py b/test/tools/AutomationClient.py index 8273d2fd..d2fde97c 100644 --- a/test/tools/AutomationClient.py +++ b/test/tools/AutomationClient.py @@ -123,14 +123,14 @@ def profile_directory(): rather than an inherited TMPDIR, which may be long: Chromium's singleton socket path must stay below sockaddr_un's limit.""" root = "/tmp" if sys.platform.startswith("linux") and os.access("/tmp", os.W_OK | os.X_OK) else None - path = tempfile.mkdtemp(prefix="wf-profile-", dir=root) + path = tempfile.mkdtemp(prefix="wf-profile- &%!^ 雪 " if sys.platform == "win32" else "wf-profile-", dir=root) try: yield path finally: shutil.rmtree(path, ignore_errors=True) -def remaining_profiles(profiles, timeout=10, settle=2.5): +def remaining_profiles(profiles, timeout=25, settle=2.5): """The agent profiles left in `profiles`, once absent for `settle` seconds or after `timeout` more, with what they contain. @@ -155,6 +155,34 @@ def remaining_profiles(profiles, timeout=10, settle=2.5): time.sleep(0.05) +def check_windows_profile_cleaner(agent, root): + """A locked file must be retried, including when its Unicode path contains shell metacharacters.""" + if sys.platform != "win32": + return + import ctypes + from ctypes import wintypes + profile = root / "profile &%!^ 雪" + profile.mkdir() + file = profile / "locked" + file.write_text("locked") + kernel = ctypes.WinDLL("kernel32", use_last_error=True) + kernel.CreateFileW.argtypes = [wintypes.LPCWSTR, wintypes.DWORD, wintypes.DWORD, wintypes.LPVOID, + wintypes.DWORD, wintypes.DWORD, wintypes.HANDLE] + kernel.CreateFileW.restype = wintypes.HANDLE + kernel.CloseHandle.argtypes = [wintypes.HANDLE] + handle = kernel.CreateFileW(str(file), 0x80000000, 0, None, 3, 0, None) + if handle == wintypes.HANDLE(-1).value: + raise ctypes.WinError(ctypes.get_last_error()) + try: + cleaner = subprocess.Popen([agent, "--cleanup-profile", str(profile)]) + time.sleep(3) + assert cleaner.poll() is None, "Cleaner stopped while the profile was locked" + finally: + kernel.CloseHandle(handle) + assert cleaner.wait(timeout=10) == 0 + assert not profile.exists(), "Cleaner left the profile behind" + + def check_startup_backoff(agent, root, ready_delay=0): """The attach-only path needs no renderer and must not create 50 connections per second.""" endpoint = root / f"owner-{ready_delay}" @@ -263,6 +291,8 @@ def watchdog(args): pathlib.Path(args.work).mkdir(parents=True, exist_ok=True) with tempfile.TemporaryDirectory(dir=args.work, prefix="watchdog-") as temporary: root = pathlib.Path(temporary) + if args.scenario == "cli-watchdog": + check_windows_profile_cleaner(args.agent, root) project = root / "project" shutil.copytree(args.fixture, project) environment = root / "watchdog-environment.tgz"