From 46b1bbd08e5512420dc94c67c0ba55079885f187 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Morais?= <146729917+SMoraisAnsys@users.noreply.github.com> Date: Thu, 6 Nov 2025 15:31:20 +0100 Subject: [PATCH 1/6] fix(cli): check access process on windows When running on windows, the process `username()` can contain the DOMAIN which makes the comparison fail. --- src/ansys/mapdl/core/cli/stop.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ansys/mapdl/core/cli/stop.py b/src/ansys/mapdl/core/cli/stop.py index 2479ab6d824..a540455b1d4 100644 --- a/src/ansys/mapdl/core/cli/stop.py +++ b/src/ansys/mapdl/core/cli/stop.py @@ -194,6 +194,8 @@ def _can_access_process(proc): # Check if we can access basic process info and if it belongs to current user current_user = getpass.getuser() process_user = proc.username() + if os.name == 'nt' and '\\' in process_user: + return current_user == process_user.split('\\')[-1] return process_user == current_user except (psutil.AccessDenied, psutil.NoSuchProcess): # Cannot access process or process doesn't exist From 4ff3623a1e81c861345a30441b4b2c8fc647162a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 6 Nov 2025 14:32:35 +0000 Subject: [PATCH 2/6] ci: auto fixes from pre-commit.com hooks. for more information, see https://pre-commit.ci --- src/ansys/mapdl/core/cli/stop.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ansys/mapdl/core/cli/stop.py b/src/ansys/mapdl/core/cli/stop.py index a540455b1d4..710f35cd41e 100644 --- a/src/ansys/mapdl/core/cli/stop.py +++ b/src/ansys/mapdl/core/cli/stop.py @@ -194,8 +194,8 @@ def _can_access_process(proc): # Check if we can access basic process info and if it belongs to current user current_user = getpass.getuser() process_user = proc.username() - if os.name == 'nt' and '\\' in process_user: - return current_user == process_user.split('\\')[-1] + if os.name == "nt" and "\\" in process_user: + return current_user == process_user.split("\\")[-1] return process_user == current_user except (psutil.AccessDenied, psutil.NoSuchProcess): # Cannot access process or process doesn't exist From 7c9ca9f0e6b65b5b72fd9139fc271cc0d9b99683 Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Thu, 6 Nov 2025 14:33:56 +0000 Subject: [PATCH 3/6] chore: adding changelog file 4293.fixed.md [dependabot-skip] --- doc/changelog.d/4293.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/4293.fixed.md diff --git a/doc/changelog.d/4293.fixed.md b/doc/changelog.d/4293.fixed.md new file mode 100644 index 00000000000..7798bf44e46 --- /dev/null +++ b/doc/changelog.d/4293.fixed.md @@ -0,0 +1 @@ +Check access process on windows From c16d61f01fb706ae8088c2b0b7b5affcfc49858b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Morais?= <146729917+SMoraisAnsys@users.noreply.github.com> Date: Thu, 6 Nov 2025 16:00:16 +0100 Subject: [PATCH 4/6] fix: add missing import and take into account review --- src/ansys/mapdl/core/cli/stop.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ansys/mapdl/core/cli/stop.py b/src/ansys/mapdl/core/cli/stop.py index 710f35cd41e..697814b8531 100644 --- a/src/ansys/mapdl/core/cli/stop.py +++ b/src/ansys/mapdl/core/cli/stop.py @@ -187,6 +187,7 @@ def _can_access_process(proc): True if we can safely access and kill the process """ import getpass + import platform import psutil @@ -194,7 +195,7 @@ def _can_access_process(proc): # Check if we can access basic process info and if it belongs to current user current_user = getpass.getuser() process_user = proc.username() - if os.name == "nt" and "\\" in process_user: + if platform.system() == "Windows" and "\\" in process_user: return current_user == process_user.split("\\")[-1] return process_user == current_user except (psutil.AccessDenied, psutil.NoSuchProcess): From d5e361df498fd8399cbacb959f214ca638d80f86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Morais?= Date: Fri, 7 Nov 2025 17:02:01 +0100 Subject: [PATCH 5/6] test: add test with domain in username --- tests/test_cli.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/test_cli.py b/tests/test_cli.py index 2dc5119753b..92a175a99a2 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -21,6 +21,7 @@ # SOFTWARE. import os +import platform import re import subprocess from typing import Callable @@ -289,6 +290,40 @@ def mock_kill_process(proc: psutil.Process): print("✅ Permission handling test passed - no crashes occurred") +@requires("click") +@pytest.mark.skipif(platform.system() != "Windows", reason="Domain usernames are Windows-specific") +def test_pymapdl_stop_with_username_containing_domain(run_cli): + """Test that pymapdl stop processes when a process username contains DOMAIN information.""" + current_user = "someuser" + + mock_process = MagicMock(spec=psutil.Process) + mock_process.pid = 12 + mock_process.name.return_value = "ansys252" + mock_process.status.return_value = psutil.STATUS_RUNNING + mock_process.cmdline.return_value = ["ansys251", "-grpc", "-port", "50052"] + mock_process.username.return_value = f"DOMAIN\\{current_user}" + + killed_processes: list[int] = [] + + def mock_kill_process(proc: psutil.Process): + """Track which processes would be killed.""" + killed_processes.append(proc.pid) + + with ( + patch("getpass.getuser", return_value=current_user), + patch("psutil.process_iter", return_value=[mock_process]), + patch("psutil.pid_exists", return_value=True), + patch("ansys.mapdl.core.cli.stop._kill_process", side_effect=mock_kill_process), + ): + killed_processes.clear() + output = run_cli("stop --all") + + assert "success" in output.lower() + + assert killed_processes == [12] + + print("✅ Domain username handling test passed - processes with domain usernames handled correctly") + @requires("click") @pytest.mark.parametrize( "arg,check", From 60ad3766066ff516cd6d4b1f06a868ee579c02ff Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 7 Nov 2025 16:02:37 +0000 Subject: [PATCH 6/6] ci: auto fixes from pre-commit.com hooks. for more information, see https://pre-commit.ci --- tests/test_cli.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 92a175a99a2..ba72a6ea9d9 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -291,11 +291,13 @@ def mock_kill_process(proc: psutil.Process): @requires("click") -@pytest.mark.skipif(platform.system() != "Windows", reason="Domain usernames are Windows-specific") +@pytest.mark.skipif( + platform.system() != "Windows", reason="Domain usernames are Windows-specific" +) def test_pymapdl_stop_with_username_containing_domain(run_cli): """Test that pymapdl stop processes when a process username contains DOMAIN information.""" current_user = "someuser" - + mock_process = MagicMock(spec=psutil.Process) mock_process.pid = 12 mock_process.name.return_value = "ansys252" @@ -322,7 +324,10 @@ def mock_kill_process(proc: psutil.Process): assert killed_processes == [12] - print("✅ Domain username handling test passed - processes with domain usernames handled correctly") + print( + "✅ Domain username handling test passed - processes with domain usernames handled correctly" + ) + @requires("click") @pytest.mark.parametrize(