-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
log_vc_info: handle long command output (#5821)
* Log the commands run by log_vc_info (debug level). * Handle the risk of large command output clogging up the buffer causing commands to hang.
- Loading branch information
1 parent
e692a06
commit 8b2feae
Showing
4 changed files
with
127 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fixed issue where large uncommitted changes could cause `cylc install` to hang. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. | ||
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Utility for preventing pipes from getting clogged up. | ||
If you're reading files from Popen (i.e. to extract command output) where the | ||
command output has the potential to be long-ish, then you should use this | ||
function to protect against the buffer filling up. | ||
Note, there is a more advanced version of this baked into the subprocpool. | ||
""" | ||
|
||
from select import select | ||
|
||
|
||
def pipe_poller(proc, *files, chunk_size=4096): | ||
"""Read from a process without hitting buffer issues. | ||
Standin for subprocess.Popen.communicate. | ||
When PIPE'ing from subprocesses, the output goes into a buffer. If the | ||
buffer gets full, the subprocess will hang trying to write to it. | ||
This function polls the process, reading output from the buffers into | ||
memory to prevent them from filling up. | ||
Args: | ||
proc: | ||
The process to poll. | ||
files: | ||
The files you want to read from, likely anything you've directed to | ||
PIPE. | ||
chunk_size: | ||
The amount of text to read from the buffer on each pass. | ||
Returns: | ||
tuple - The text read from each of the files in the order they were | ||
specified. | ||
""" | ||
_files = { | ||
file: b'' if 'b' in getattr(file, 'mode', 'r') else '' | ||
for file in files | ||
} | ||
|
||
def _read(timeout=1.0): | ||
# read any data from files | ||
nonlocal chunk_size, files | ||
for file in select(list(files), [], [], timeout)[0]: | ||
buffer = file.read(chunk_size) | ||
if len(buffer) > 0: | ||
_files[file] += buffer | ||
|
||
while proc.poll() is None: | ||
# read from the buffers | ||
_read() | ||
# double check the buffers now that the process has finished | ||
_read(timeout=0.01) | ||
|
||
return tuple(_files.values()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. | ||
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
from subprocess import Popen, PIPE | ||
|
||
from cylc.flow.pipe_poller import pipe_poller | ||
|
||
|
||
def test_pipe_poller_str(): | ||
proc = Popen(['echo', 'Hello World!'], stdout=PIPE, text=True) | ||
(stdout,) = pipe_poller(proc, proc.stdout) | ||
assert proc.returncode == 0 | ||
assert stdout == 'Hello World!\n' | ||
|
||
|
||
def test_pipe_poller_bytes(): | ||
proc = Popen(['echo', 'Hello World!'], stdout=PIPE, text=False) | ||
(stdout,) = pipe_poller(proc, proc.stdout) | ||
assert proc.returncode == 0 | ||
assert stdout == b'Hello World!\n' |