Skip to content

Commit d3c0c5d

Browse files
SMoraisAnsyspre-commit-ci[bot]pyansys-ci-botgerma89
authored
fix(cli): check access process on windows (#4293)
* fix(cli): check access process on windows When running on windows, the process `username()` can contain the DOMAIN which makes the comparison fail. * ci: auto fixes from pre-commit.com hooks. for more information, see https://pre-commit.ci * chore: adding changelog file 4293.fixed.md [dependabot-skip] * fix: add missing import and take into account review * test: add test with domain in username * ci: auto fixes from pre-commit.com hooks. for more information, see https://pre-commit.ci * test: remove print statement from domain username handling test --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: pyansys-ci-bot <[email protected]> Co-authored-by: German <[email protected]>
1 parent 5b7818a commit d3c0c5d

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

doc/changelog.d/4293.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Check access process on windows

src/ansys/mapdl/core/cli/stop.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,16 @@ def _can_access_process(proc):
187187
True if we can safely access and kill the process
188188
"""
189189
import getpass
190+
import platform
190191

191192
import psutil
192193

193194
try:
194195
# Check if we can access basic process info and if it belongs to current user
195196
current_user = getpass.getuser()
196197
process_user = proc.username()
198+
if platform.system() == "Windows" and "\\" in process_user:
199+
return current_user == process_user.split("\\")[-1]
197200
return process_user == current_user
198201
except (psutil.AccessDenied, psutil.NoSuchProcess):
199202
# Cannot access process or process doesn't exist

tests/test_cli.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# SOFTWARE.
2222

2323
import os
24+
import platform
2425
import re
2526
import subprocess
2627
from typing import Callable
@@ -289,6 +290,40 @@ def mock_kill_process(proc: psutil.Process):
289290
print("✅ Permission handling test passed - no crashes occurred")
290291

291292

293+
@requires("click")
294+
@pytest.mark.skipif(
295+
platform.system() != "Windows", reason="Domain usernames are Windows-specific"
296+
)
297+
def test_pymapdl_stop_with_username_containing_domain(run_cli):
298+
"""Test that pymapdl stop processes when a process username contains DOMAIN information."""
299+
current_user = "someuser"
300+
301+
mock_process = MagicMock(spec=psutil.Process)
302+
mock_process.pid = 12
303+
mock_process.name.return_value = "ansys252"
304+
mock_process.status.return_value = psutil.STATUS_RUNNING
305+
mock_process.cmdline.return_value = ["ansys251", "-grpc", "-port", "50052"]
306+
mock_process.username.return_value = f"DOMAIN\\{current_user}"
307+
308+
killed_processes: list[int] = []
309+
310+
def mock_kill_process(proc: psutil.Process):
311+
"""Track which processes would be killed."""
312+
killed_processes.append(proc.pid)
313+
314+
with (
315+
patch("getpass.getuser", return_value=current_user),
316+
patch("psutil.process_iter", return_value=[mock_process]),
317+
patch("psutil.pid_exists", return_value=True),
318+
patch("ansys.mapdl.core.cli.stop._kill_process", side_effect=mock_kill_process),
319+
):
320+
killed_processes.clear()
321+
output = run_cli("stop --all")
322+
323+
assert "success" in output.lower()
324+
assert killed_processes == [12]
325+
326+
292327
@requires("click")
293328
@pytest.mark.parametrize(
294329
"arg,check",

0 commit comments

Comments
 (0)