Skip to content

Commit bd4d182

Browse files
authored
Merge pull request #2653 from shbhmexe/bugfix/su2py-run-command-stderr-deadlock
fix: prevent SU2_PY runner stderr deadlock and improve error handling
2 parents 8ddd591 + 882d9eb commit bd4d182

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

SU2_PY/SU2/run/interface.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@
3838
# Setup
3939
# ------------------------------------------------------------
4040

41-
SU2_RUN = os.environ["SU2_RUN"]
41+
try:
42+
SU2_RUN = os.environ["SU2_RUN"]
43+
except KeyError as exc:
44+
raise RuntimeError(
45+
'Environment variable "SU2_RUN" is not set. Please set SU2_RUN to the SU2 installation bin directory.'
46+
) from exc
47+
4248
sys.path.append(SU2_RUN)
4349
quote = '"' if sys.platform == "win32" else ""
4450

@@ -263,14 +269,17 @@ def run_command(Command):
263269
"""
264270
sys.stdout.flush()
265271

272+
# Avoid potential deadlocks when a child process writes heavily to stderr:
273+
# read stderr via communicate() while streaming stdout to the console.
266274
# Use communicate() to continuously drain stderr and avoid deadlocks if the
267275
# subprocess writes a lot of output to stderr.
268276
proc = subprocess.Popen(
269277
Command, shell=True, stdout=sys.stdout, stderr=subprocess.PIPE
270278
)
271279
_, stderr = proc.communicate()
280+
272281
return_code = proc.returncode
273-
message = stderr.decode(errors="replace")
282+
message = (stderr or b"").decode(errors="replace")
274283

275284
if return_code < 0:
276285
message = "SU2 process was terminated by signal '%s'\n%s" % (

0 commit comments

Comments
 (0)