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
20 changes: 17 additions & 3 deletions src/borg/platform/windows.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import ctypes.wintypes
import errno as errno_mod
import msvcrt
import os
import platform

from .base import SyncFile as BaseSyncFile
from .base import MountEntry
Expand Down Expand Up @@ -185,9 +184,24 @@ def getosusername():
def process_alive(host, pid, thread):
"""
Check whether the (host, pid, thread_id) combination corresponds to a process potentially alive.

If the process is local, then this will be accurate. If the process is not local, then this
always returns True, since there is no real way to check.
"""
if host.split('@')[0].lower() != platform.node().lower():
# If not running on the same node, assume the process is running.
from . import get_hostid

hostid = get_hostid()
assert isinstance(host, str)
assert isinstance(hostid, str)
assert isinstance(pid, int)
assert isinstance(thread, int)

if host != hostid:
return True

if thread != 0:
# Currently, thread is always 0; if we ever decide to set this to a non-zero value,
# this code needs to be revisited to do a sensible thing.
return True

# If the process can be opened (or access is denied because it belongs to
Expand Down
25 changes: 19 additions & 6 deletions src/borg/testsuite/platform/windows_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ctypes
import os
import platform
import subprocess
import sys
import tempfile
Expand All @@ -9,7 +8,7 @@

from .platform_test import skipif_not_win32
from .. import are_symlinks_supported
from ...platform import SyncFile, set_times
from ...platform import SyncFile, get_hostid, set_times

# Set module-level skips
pytestmark = [skipif_not_win32]
Expand Down Expand Up @@ -155,7 +154,7 @@ def fake_open_process(access, inherit, pid):
return None

monkeypatch.setattr(windows, "_OpenProcess", fake_open_process)
assert windows.process_alive(platform.node(), 4, 0) is True
assert windows.process_alive(get_hostid(), 4, 0) is True


def test_process_alive_dead_process(monkeypatch):
Expand All @@ -167,7 +166,7 @@ def fake_open_process(access, inherit, pid):
return None

monkeypatch.setattr(windows, "_OpenProcess", fake_open_process)
assert windows.process_alive(platform.node(), 999999, 0) is False
assert windows.process_alive(get_hostid(), 999999, 0) is False


def test_process_alive_exited_with_open_handle():
Expand All @@ -177,15 +176,29 @@ def test_process_alive_exited_with_open_handle():
proc = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(60)"])
handle = None
try:
assert windows.process_alive(platform.node(), proc.pid, 0) is True
assert windows.process_alive(get_hostid(), proc.pid, 0) is True
handle = windows._OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, False, proc.pid)
assert handle
proc.kill()
proc.wait()
assert windows.process_alive(platform.node(), proc.pid, 0) is False
assert windows.process_alive(get_hostid(), proc.pid, 0) is False
finally:
if proc.poll() is None:
proc.kill()
proc.wait()
if handle:
windows._CloseHandle(handle)


def test_process_alive_different_host_or_thread(monkeypatch):
"""process_alive returns True without querying OS if host != hostid or thread != 0."""
from ...platform import windows

def fail_if_called(*args):
raise AssertionError("_OpenProcess should not be called for remote host or non-zero thread")

monkeypatch.setattr(windows, "_OpenProcess", fail_if_called)
hostid = get_hostid()
assert windows.process_alive(hostid + "-other", 999999, 0) is True
assert windows.process_alive(hostid.split("@")[0], 999999, 0) is True
assert windows.process_alive(hostid, 999999, 1) is True