Repository navigation
windows.process_alive: treat ERROR_ACCESS_DENIED as alive process - #10649
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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. |
ThomasWaldmann
left a comment
There was a problem hiding this comment.
Thanks! The change looks correct and goes in the safe direction:
OpenProcessfails withERROR_ACCESS_DENIEDonly when the PID belongs to an existing process object we may not open; a PID nobody uses givesERROR_INVALID_PARAMETER(87). Treating 5 as alive matches the POSIXEPERMhandling. The worst case is a stale lock that isn't broken automatically (borg break-lockstill works), which is much better than breaking a lock that is still held.PROCESS_QUERY_LIMITED_INFORMATIONis granted more often thanPROCESS_QUERY_INFORMATION(e.g. for protected processes), so access denied becomes rarer in the first place.use_last_error=Trueis already set on_kernel32andrestype=HANDLEmakes NULL come back asNone, soif handle:andctypes.get_last_error()are fine. Removing thecdef externblock is fine, nothing else used it.
Some small things:
- Removing the
cdef externblock left 5 blank lines afterfrom .base import MountEntry- please reduce that to 2. Also, the new constants are glued to the end of the FILETIME block. Please putERROR_ACCESS_DENIEDnext toERROR_FILE_EXISTSand givePROCESS_QUERY_LIMITED_INFORMATIONits own spot (it does not belong under the "constants for CreateFileW" comment), separated by a blank line from_kernel32 = .... - The
'@'->"@"change is unrelated to the fix - please drop it. - Tests:
import ctypes/import platformcan go to module level. Thewindowsmodule import has to stay inside the functions (it is not importable on other platforms), but please use the relative formfrom ...platform import windowslike the rest of the file. _CloseHandlehas noargtypes/restype(that was already the case before, but this PR adds another call). Declaringargtypes = [ctypes.wintypes.HANDLE]andrestype = ctypes.wintypes.BOOLwould 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()withplatform.node().lower(), but the lock storesget_hostid(), which isf"{get_fqdn()}@{uuid.getnode()}"(orBORG_HOST_ID). On a domain-joined machine or with a DHCP-provided DNS suffix, the fqdn is e.g.DESKTOP-X.fritz.boxwhileplatform.node()isDESKTOP-X, so every local lock looks remote and theOpenProcesscode is never reached. In the other direction, two hosts with the same name but differentuuid.getnode()look like the same host, so a remote lock would get a local PID check. Comparing the full hostid likeposix.process_alivedoes (if host != hostid: return True, plus thethread != 0check) would fix both. OpenProcessalso 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). CheckingGetExitCodeProcess(h) == STILL_ACTIVE(259) after a successful open would catch that; it works withPROCESS_QUERY_LIMITED_INFORMATION.
|
Thanks for the detailed review! I've updated the PR to address all your feedback. |
What does this PR fix?
On Windows,
platform.windows.process_alive()previously returnedFalsewheneverOpenProcess()failed, even if the failure was due toERROR_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
SYSTEMscheduled task), a non-elevatedborgprocess receivesERROR_ACCESS_DENIEDand mistakenly treats the live process as dead, allowingLock._is_stale_lock()to break an active lock.Solution
_kernel32.OpenProcesswithPROCESS_QUERY_LIMITED_INFORMATION(use_last_error=True) insrc/borg/platform/windows.pyx.Truewhenctypes.get_last_error() == ERROR_ACCESS_DENIED(matching POSIX behavior forEPERM).src/borg/testsuite/platform/windows_test.pycoveringERROR_ACCESS_DENIEDand dead-process error paths.