Skip to content

Commit 97588b6

Browse files
authored
Fix environment PATH pollution by CGrpc servers (#1590)
* Extract logic to get system's actual path Signed-off-by: paul.profizi <[email protected]> * Try commenting all path manipulations for CGrpc server in ServerFactory.get_server_type_from_config Signed-off-by: paul.profizi <[email protected]> * Add test on path pollution Signed-off-by: paul.profizi <[email protected]> * Make test retro-compatible (no server.release() Signed-off-by: paul.profizi <[email protected]> * Try and make get_system_path work on Linux Signed-off-by: paul.profizi <[email protected]> * Fix style in test Signed-off-by: paul.profizi <[email protected]> * Confirmed the old hack is no longer required Signed-off-by: paul.profizi <[email protected]> --------- Signed-off-by: paul.profizi <[email protected]>
1 parent bfa7912 commit 97588b6

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

src/ansys/dpf/core/server_factory.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -602,13 +602,6 @@ def get_server_type_from_config(
602602
if config.protocol == CommunicationProtocols.gRPC and config.legacy:
603603
return LegacyGrpcServer
604604
elif config.protocol == CommunicationProtocols.gRPC and not config.legacy:
605-
if ansys_path is None:
606-
from ansys.dpf.core.misc import __ansys_version__
607-
608-
ansys_path = os.environ.get("AWP_ROOT" + str(__ansys_version__), None)
609-
if ansys_path is not None:
610-
sub_folders = os.path.join(ansys_path, _get_path_in_install())
611-
os.environ["PATH"] += sub_folders
612605
return GrpcServer
613606
elif config.protocol == CommunicationProtocols.InProcess and not config.legacy:
614607
return InProcessServer

src/ansys/dpf/core/server_types.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import socket
1111
import subprocess
12+
import sys
1213
import time
1314
import warnings
1415
import traceback
@@ -927,14 +928,7 @@ def __init__(
927928
# Forced to use ctypes to get the updated PATH due to sys.exec not the Python
928929
# interpreter when running Python plugin test VS project
929930
# The better solution would be to not need to update the path
930-
windll.kernel32.GetEnvironmentVariableA.argtypes = (c_char_p, c_char_p, c_int)
931-
windll.kernel32.GetEnvironmentVariableA.restype = c_int
932-
name = "PATH"
933-
b_name = name.encode("utf-8")
934-
size = 32767
935-
buffer = create_string_buffer(b"", size)
936-
_ = windll.kernel32.GetEnvironmentVariableA(b_name, buffer, size)
937-
os.environ["PATH"] = buffer.value.decode("utf-8")
931+
os.environ["PATH"] = get_system_path()
938932

939933
@property
940934
def version(self):
@@ -977,6 +971,22 @@ def config(self):
977971
return server_factory.AvailableServerConfigs.InProcessServer
978972

979973

974+
def get_system_path() -> str:
975+
"""Return the current PATH environment variable value of the system."""
976+
if not os.name == "posix":
977+
windll.kernel32.GetEnvironmentVariableA.argtypes = (c_char_p, c_char_p, c_int)
978+
windll.kernel32.GetEnvironmentVariableA.restype = c_int
979+
name = "PATH"
980+
b_name = name.encode("utf-8")
981+
size = 32767
982+
buffer = create_string_buffer(b"", size)
983+
_ = windll.kernel32.GetEnvironmentVariableA(b_name, buffer, size)
984+
return buffer.value.decode("utf-8")
985+
else:
986+
return sys.path
987+
988+
989+
980990
class LegacyGrpcServer(BaseServer):
981991
"""Provides an instance of the DPF server using InProcess gRPC.
982992
Kept for backward-compatibility with dpf servers <0.5.0.

tests/test_server.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ def test_start_local_server(self, server_config):
9191
assert has_local_server()
9292
shutdown_all_session_servers()
9393

94+
def test_server_env_path_cleanup(self, server_config):
95+
"""Test that running and stopping servers does not pollute the system PATH."""
96+
from ansys.dpf.core.server_types import get_system_path
97+
path_len_init = len(get_system_path())
98+
_ = dpf.core.start_local_server(config=server_config)
99+
assert len(get_system_path()) == path_len_init
100+
94101
def test_start_local_server_with_config(self, server_config):
95102
set_server_configuration(None)
96103
shutdown_all_session_servers()

0 commit comments

Comments
 (0)