Skip to content

Commit

Permalink
[FIX] Use process.run instead of process.Popen to avoid deadlock (#208)
Browse files Browse the repository at this point in the history
* Use process.run instead of process.Popen, to avoid deadlock

When using process.Popen, and there is a large amount of
data being returned on std out or error by the subprocess,
it might cause a deadlock. So Popen is not recommended to
use in these casses, we should rather use the run command.

* PR review adjustments

see suggestions from code review

---------

Co-authored-by: Benjamin K <53038537+treee111@users.noreply.github.com>
  • Loading branch information
alfh and treee111 authored Jun 15, 2023
1 parent fe03d9c commit 5e29dc1
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions wahoomc/osm_maps_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,25 @@ def run_subprocess_and_log_output(cmd, error_message, cwd=""):
run given cmd-subprocess and issue error message if wished
"""
if not cwd:
process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
process = subprocess.run(
cmd, capture_output=True, text=True, encoding="utf-8", check=False)

else:
process = subprocess.Popen( # pylint: disable=consider-using-with
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd)
process = subprocess.run( # pylint: disable=consider-using-with
cmd, capture_output=True, cwd=cwd, text=True, encoding="utf-8", check=False)

if error_message and process.wait() != 0: # 0 means success
for line in iter(process.stdout.readline, b''): # b'\n'-separated lines
# print(line.rstrip())
try:
log.error('subprocess:%r', line.decode("utf-8").strip())
except UnicodeDecodeError:
log.error('subprocess:%r', line.decode("latin-1").strip())

if error_message and process.returncode != 0: # 0 means success
log.error('subprocess error output:')
if process.stderr:
log.error(process.stderr)

log.error(error_message)
sys.exit()

else:
for line in iter(process.stdout.readline, b''): # b'\n'-separated lines
# print(line.rstrip())
try:
log.debug('subprocess:%r', line.decode("utf-8").strip())
except UnicodeDecodeError:
log.debug('subprocess:%r', line.decode("latin-1").strip())
elif process.stdout:
log.debug('subprocess debug output:')
log.debug(process.stdout)


def get_timestamp_last_changed(file_path):
Expand Down

0 comments on commit 5e29dc1

Please sign in to comment.