Skip to content

Commit

Permalink
Fix: utils: pass env to child process explicitly (bsc#1205925)
Browse files Browse the repository at this point in the history
A bug in python stardard library, python/cpython#100516, make
environment variables, `COLUMNS` and `ROWS` to be added at fork. These
environment variables may make some of the commands called by crmsh to
truncate its output even if those commands are not writing to the
terminal.

Passing argument `env` explicitly is a workaround for the python bug.
  • Loading branch information
nicholasyang2022 committed Mar 13, 2024
1 parent 11ada54 commit 2846124
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
6 changes: 5 additions & 1 deletion crmsh/report/utillib.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,11 @@ def get_command_info_timeout(cmd, timeout=5):
# Python 101: How to timeout a subprocess
def kill(process):
process.kill()
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env=os.environ, # bsc#1205925
)
my_timer = Timer(timeout, kill, [proc])
try:
my_timer.start()
Expand Down
20 changes: 16 additions & 4 deletions crmsh/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,9 @@ def get_stdout(cmd, input_s=None, stderr_on=True, shell=True, raw=False):
shell=shell,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=stderr)
stderr=stderr,
env=os.environ, # bsc#1205925
)
stdout_data, stderr_data = proc.communicate(input_s)
if raw:
return proc.returncode, stdout_data
Expand All @@ -1096,7 +1098,9 @@ def get_stdout_stderr(cmd, input_s=None, shell=True, raw=False, no_reg=False):
shell=shell,
stdin=input_s and subprocess.PIPE or None,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stderr=subprocess.PIPE,
env=os.environ, # bsc#1205925
)
stdout_data, stderr_data = proc.communicate(input_s)
if raw:
return proc.returncode, stdout_data, stderr_data
Expand All @@ -1115,6 +1119,7 @@ def su_get_stdout_stderr(user, cmd, input_s=None, raw=False):
input=input_s.encode('utf-8') if (input_s is not None and not raw) else input_s,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=os.environ, # bsc#1205925
)
if raw:
return result.returncode, result.stdout, result.stderr
Expand Down Expand Up @@ -2932,6 +2937,7 @@ def get_stdout_or_raise_error(cmd, remote=None, success_val_list=[0], no_raise=F
input=cmd.encode('utf-8'),
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL if no_raise else subprocess.PIPE,
env=os.environ, # bsc#1205925
)
else:
result = subprocess_run_auto_ssh_no_input(
Expand All @@ -2958,6 +2964,7 @@ def subprocess_run_auto_ssh_no_input(cmd, remote=None, user=None, **kwargs):
return subprocess.run(
args,
input=cmd.encode('utf-8'),
env=os.environ, # bsc#1205925
**kwargs,
)
else:
Expand All @@ -2983,7 +2990,8 @@ def su_get_stdout_or_raise_error(cmd, user, success_values={0}, no_raise=False):
result = subprocess.run(
args,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL if no_raise else subprocess.PIPE
stderr=subprocess.DEVNULL if no_raise else subprocess.PIPE,
env=os.environ, # bsc#1205925
)
if no_raise or result.returncode in success_values:
return result.stdout
Expand All @@ -3001,7 +3009,11 @@ def su_subprocess_run(user: str, cmd: str, tty=False, **kwargs):
args = ['su', user, '--login', '-c', cmd]
else:
raise AssertionError('trying to run su as a non-root user')
return subprocess.run(args, **kwargs)
return subprocess.run(
args,
env=os.environ, # bsc#1205925
**kwargs,
)


def get_quorum_votes_dict(remote=None):
Expand Down

0 comments on commit 2846124

Please sign in to comment.