Skip to content

Commit

Permalink
[core] SpooledTemporaryFile for subprocess output (#3002)
Browse files Browse the repository at this point in the history
https://docs.python.org/2/library/tempfile.html#tempfile.SpooledTemporaryFile
SpooledTemporaryFile stores data in memory (up to max_size, 1 MB)
instead of on disk, it should stop the agent from completely crashing when disk is
full.
  • Loading branch information
degemer authored and truthbk committed Nov 15, 2016
1 parent e9436a2 commit ceb0dce
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions utils/subprocess_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,27 @@
# Licensed under Simplified BSD License (see LICENSE)

# stdlib
from contextlib import nested
from functools import wraps
import logging
import subprocess
import tempfile

log = logging.getLogger(__name__)


class SubprocessOutputEmptyError(Exception):
pass

# FIXME: python 2.7 has a far better way to do this

def get_subprocess_output(command, log, raise_on_empty_output=True):
"""
Run the given subprocess command and return it's output. Raise an Exception
Run the given subprocess command and return its output. Raise an Exception
if an error occurs.
"""

# Use tempfile, allowing a larger amount of memory. The subprocess.Popen
# docs warn that the data read is buffered in memory. They suggest not to
# use subprocess.PIPE if the data size is large or unlimited.
with nested(tempfile.TemporaryFile(), tempfile.TemporaryFile()) as (stdout_f, stderr_f):
max_size = 1024 * 1024 # 1 MB

with tempfile.SpooledTemporaryFile(max_size=max_size) as stdout_f, tempfile.SpooledTemporaryFile(max_size=max_size) as stderr_f:
proc = subprocess.Popen(command, stdout=stdout_f, stderr=stderr_f)
proc.wait()
stderr_f.seek(0)
Expand Down

0 comments on commit ceb0dce

Please sign in to comment.