Skip to content

Commit cbc25bc

Browse files
authored
Make sure retrieving _USE_VFORK can't fail (#312)
1 parent cd04de2 commit cbc25bc

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/ansys/hps/data_transfer/client/binary.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,31 @@ def __init__(self):
7373
"""Initialize the PrepareSubprocess class object."""
7474
# Check if not Windows
7575
self.disable_vfork = os.name != "nt" and platform.system() != "Windows"
76+
self._orig_use_vfork = None
77+
self._orig_use_pspawn = None
7678

7779
def __enter__(self):
7880
"""Disable vfork and posix_spawn in subprocess."""
79-
if self.disable_vfork:
81+
if not self.disable_vfork:
82+
return
83+
84+
if hasattr(subprocess, "_USE_VFORK"):
8085
self._orig_use_vfork = subprocess._USE_VFORK
81-
self._orig_use_pspawn = subprocess._USE_POSIX_SPAWN
8286
subprocess._USE_VFORK = False
87+
88+
if hasattr(subprocess, "_USE_POSIX_SPAWN"):
89+
self._orig_use_pspawn = getattr(subprocess, "_USE_POSIX_SPAWN", False)
8390
subprocess._USE_POSIX_SPAWN = False
8491

8592
def __exit__(self, exc_type, exc_val, exc_tb):
8693
"""Restore original values of _USE_VFORK and _USE_POSIX_SPAWN."""
87-
if self.disable_vfork:
94+
if not self.disable_vfork:
95+
return
96+
97+
if self._orig_use_vfork is not None:
8898
subprocess._USE_VFORK = self._orig_use_vfork
99+
100+
if self._orig_use_pspawn is not None:
89101
subprocess._USE_POSIX_SPAWN = self._orig_use_pspawn
90102

91103

0 commit comments

Comments
 (0)