Skip to content

windows.process_alive: treat ERROR_ACCESS_DENIED as alive process - #10649

Merged
ThomasWaldmann merged 3 commits into
borgbackup:masterfrom
underiya-ai:fix-windows-process-alive
Nov 29, 2026
Merged

ThomasWaldmann merged 3 commits into
borgbackup:masterfrom
underiya-ai:fix-windows-process-alive

Conversation

@underiya-ai

Copy link
Copy Markdown
Contributor

What does this PR fix?

On Windows, platform.windows.process_alive() previously returned False whenever OpenProcess() failed, even if the failure was due to ERROR_ACCESS_DENIED (error code 5).

When a lock is held by a process running in a different security context on the same host (e.g., an elevated Administrator or SYSTEM scheduled task), a non-elevated borg process receives ERROR_ACCESS_DENIED and mistakenly treats the live process as dead, allowing Lock._is_stale_lock() to break an active lock.

Solution

  • Use _kernel32.OpenProcess with PROCESS_QUERY_LIMITED_INFORMATION (use_last_error=True) in src/borg/platform/windows.pyx.
  • Return True when ctypes.get_last_error() == ERROR_ACCESS_DENIED (matching POSIX behavior for EPERM).
  • Add unit tests in src/borg/testsuite/platform/windows_test.py covering ERROR_ACCESS_DENIED and dead-process error paths.

@codecov

codecov Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.48%. Comparing base (6e04ded) to head (bf2fdea).
⚠️ Report is 3 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #10649      +/-   ##
==========================================
+ Coverage   89.46%   89.48%   +0.01%     
==========================================
  Files         105      105              
  Lines       20343    20343              
  Branches     3199     3199              
==========================================
+ Hits        18200    18203       +3     
+ Misses       1488     1486       -2     
+ Partials      655      654       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@ThomasWaldmann ThomasWaldmann left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The change looks correct and goes in the safe direction:

  • OpenProcess fails with ERROR_ACCESS_DENIED only when the PID belongs to an existing process object we may not open; a PID nobody uses gives ERROR_INVALID_PARAMETER (87). Treating 5 as alive matches the POSIX EPERM handling. The worst case is a stale lock that isn't broken automatically (borg break-lock still works), which is much better than breaking a lock that is still held.
  • PROCESS_QUERY_LIMITED_INFORMATION is granted more often than PROCESS_QUERY_INFORMATION (e.g. for protected processes), so access denied becomes rarer in the first place.
  • use_last_error=True is already set on _kernel32 and restype=HANDLE makes NULL come back as None, so if handle: and ctypes.get_last_error() are fine. Removing the cdef extern block is fine, nothing else used it.

Some small things:

  1. Removing the cdef extern block left 5 blank lines after from .base import MountEntry - please reduce that to 2. Also, the new constants are glued to the end of the FILETIME block. Please put ERROR_ACCESS_DENIED next to ERROR_FILE_EXISTS and give PROCESS_QUERY_LIMITED_INFORMATION its own spot (it does not belong under the "constants for CreateFileW" comment), separated by a blank line from _kernel32 = ....
  2. The '@' -> "@" change is unrelated to the fix - please drop it.
  3. Tests: import ctypes / import platform can go to module level. The windows module import has to stay inside the functions (it is not importable on other platforms), but please use the relative form from ...platform import windows like the rest of the file.
  4. _CloseHandle has no argtypes/restype (that was already the case before, but this PR adds another call). Declaring argtypes = [ctypes.wintypes.HANDLE] and restype = ctypes.wintypes.BOOL would be cleaner.

Note that the new tests only exercise mocks; the real OpenProcess call is covered by the existing platform_test.py::test_process_alive in the windows_tests CI job, so that one needs to be green.

Not caused by this PR, but in the same function (can be a separate PR, if you like to work on it):

  • The host check compares host.split("@")[0].lower() with platform.node().lower(), but the lock stores get_hostid(), which is f"{get_fqdn()}@{uuid.getnode()}" (or BORG_HOST_ID). On a domain-joined machine or with a DHCP-provided DNS suffix, the fqdn is e.g. DESKTOP-X.fritz.box while platform.node() is DESKTOP-X, so every local lock looks remote and the OpenProcess code is never reached. In the other direction, two hosts with the same name but different uuid.getnode() look like the same host, so a remote lock would get a local PID check. Comparing the full hostid like posix.process_alive does (if host != hostid: return True, plus the thread != 0 check) would fix both.
  • OpenProcess also succeeds for a process that has already exited, as long as some other process still holds a handle to it (e.g. a parent like Vorta that spawned borg). Checking GetExitCodeProcess(h) == STILL_ACTIVE (259) after a successful open would catch that; it works with PROCESS_QUERY_LIMITED_INFORMATION.

@underiya-ai

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review! I've updated the PR to address all your feedback.

@ThomasWaldmann ThomasWaldmann left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@ThomasWaldmann
ThomasWaldmann merged commit 8016707 into borgbackup:master Nov 29, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants