Skip to content

windows.process_alive: check GetExitCodeProcess == STILL_ACTIVE - #10650

Open
underiya-ai wants to merge 1 commit into
borgbackup:masterfrom
underiya-ai:fix-windows-process-exit-code
Open

underiya-ai wants to merge 1 commit into
borgbackup:masterfrom
underiya-ai:fix-windows-process-exit-code

Conversation

@underiya-ai

Copy link
Copy Markdown
Contributor

What does this PR fix?

On Windows, OpenProcess() still succeeds and returns a valid handle for a process that has already exited if another process (such as a parent launcher like Vorta) still holds an open handle to it. As a result, platform.windows.process_alive() could report an exited process as still alive.

(Note: Built on top of #10649.)

Solution

  • Call GetExitCodeProcess() via _kernel32 after opening the process handle with PROCESS_QUERY_LIMITED_INFORMATION and verify that the exit code equals STILL_ACTIVE (259).
  • Ensure the opened handle is always closed via try ... finally.
  • Add unit tests in src/borg/testsuite/platform/windows_test.py covering both STILL_ACTIVE and exited-process open-handle cases.

@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.48%. Comparing base (06e0482) to head (49d0bbe).
⚠️ Report is 2 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #10650   +/-   ##
=======================================
  Coverage   89.47%   89.48%           
=======================================
  Files         105      105           
  Lines       20343    20343           
  Branches     3199     3199           
=======================================
+ Hits        18201    18203    +2     
+ Misses       1489     1486    -3     
- Partials      653      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 logic is correct:

  • GetExitCodeProcess works with the PROCESS_QUERY_LIMITED_INFORMATION access we already open the process with, so no extra access right is needed.
  • The handle is always closed via finally, and returning True when GetExitCodeProcess itself fails is the conservative choice.

Rebase needed: #10649 was squash-merged, so this branch still carries its three original commits and now conflicts with master. Please rebase onto master so that only the new changes remain (ideally squashed into one commit).

Some small things:

  1. Please add a short comment that an exit code of 259 is indistinguishable from STILL_ACTIVE, so a process that exited with 259 is treated as alive. That errs on the safe side and borg never exits with 259, so no logic change is needed, just the note.

  2. The new tests write the fake exit code via exit_code_ptr._obj.value. _obj is an undocumented attribute of the object byref() returns. It works on CPython, but it is fragile.

  3. The scenario this PR is about can be tested with real processes instead of mocks:

    • start a child process, check process_alive(platform.node(), pid, 0) is True;
    • kill it, wait() for it, and keep a handle to it open via windows._OpenProcess(...);
    • check process_alive(platform.node(), pid, 0) is now False, then close the handle.

    That would make the _obj mocks for this case unnecessary. The ERROR_ACCESS_DENIED mock can stay, that case is hard to set up for real.

  4. Imports: please keep the stdlib imports together and sorted, separated by a blank line from pytest:

    import ctypes
    import os
    import platform
    import tempfile
    
    import pytest
  5. Two of the four test functions have a blank line after the docstring, the other two don't - please make them consistent.

@underiya-ai
underiya-ai force-pushed the fix-windows-process-exit-code branch from 482c81b to 49d0bbe Compare November 29, 2026 18:11
@underiya-ai

Copy link
Copy Markdown
Contributor Author

Thanks for the review! I've rebased onto master as a single commit, added the comment about exit code 259, replaced the _obj mocks with a real subprocess test, and cleaned up the test imports.

This branch has not been deployed

No deployments
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