Skip to content

Commit

Permalink
Don't read from stdout/stderr on ES startup (#879)
Browse files Browse the repository at this point in the history
With this commit we avoid reading from stdout/stderr which might have
been closed by the application and rely solely on the process' return
code.

Relates elastic/elasticsearch#50259
  • Loading branch information
danielmitterdorfer authored Jan 27, 2020
1 parent 21347e1 commit 015d25f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
18 changes: 17 additions & 1 deletion esrally/mechanic/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# under the License.
import logging
import os
import shlex
import subprocess

import psutil

Expand Down Expand Up @@ -186,14 +188,28 @@ def _set_env(self, env, k, v, separator=' ', prepend=False):
else:
env[k] = env[k] + separator + v

@staticmethod
def _run_subprocess(command_line, env):
command_line_args = shlex.split(command_line)

# pylint: disable=subprocess-popen-preexec-fn
with subprocess.Popen(command_line_args,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
env=env,
preexec_fn=os.setpgrp) as command_line_process:
# wait for it to finish
command_line_process.poll()
return command_line_process.returncode

@staticmethod
def _start_process(binary_path, env):
if os.name == "posix" and os.geteuid() == 0:
raise exceptions.LaunchError("Cannot launch Elasticsearch as root. Please run Rally as a non-root user.")
os.chdir(binary_path)
cmd = [io.escape_path(os.path.join(".", "bin", "elasticsearch"))]
cmd.extend(["-d", "-p", "pid"])
ret = process.run_subprocess_with_logging(command_line=" ".join(cmd), env=env, detach=True)
ret = ProcessLauncher._run_subprocess(command_line=" ".join(cmd), env=env)
if ret != 0:
msg = "Daemon startup failed with exit code [{}]".format(ret)
logging.error(msg)
Expand Down
3 changes: 3 additions & 0 deletions tests/mechanic/launcher_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ def __init__(self, *args, **kwargs):
def communicate(self, input=None, timeout=None):
return [b"", b""]

def poll(self):
pass

def __enter__(self):
return self

Expand Down

0 comments on commit 015d25f

Please sign in to comment.