Repository navigation
Stream local check output into a bounded diagnostic tail #1227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,11 +5,13 @@ | |
| import json | ||
| import os | ||
| import resource | ||
| import shlex | ||
| import shutil | ||
| import signal | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| import tracemalloc | ||
| import traceback | ||
| from contextlib import ExitStack | ||
| from dataclasses import replace | ||
|
|
@@ -108,9 +110,10 @@ def test_report_fails_when_head_changes_during_run(git_repo, bwrap_log): | |
|
|
||
| @pytest.mark.parametrize("command,exit_code,line_count,start,end", [ | ||
| ("for i in $(seq 1 50); do echo $i; done; echo err >&2", 0, 40, "12", "err"), | ||
| ("""python3 -c 'print("x" * 16777216 + " Ω end")'""", 0, 1, "x", " Ω end"), | ||
| ("""python3 -c 'print("x" * 20000 + " Ω end")'""", 0, 1, "x", " Ω end"), | ||
| ("""python3 -c 'print("terminal line" + "\\n" * 20_000)'""", 0, 1, "terminal line", "terminal line"), | ||
| ("./" * 8_200 + "absent/file", 1, 1, "", "otherwise restore the required file."), | ||
| ], ids=["lines", "oversized-line", "missing-file"]) | ||
| ], ids=["lines", "oversized-line", "trailing-newlines", "missing-file"]) | ||
| def test_tail_merges_streams_and_truncates(git_repo, bwrap_log, command, exit_code, line_count, start, end): | ||
| (result,) = sandbox.run_checks(make_config(checks=(command,)), git_repo).results | ||
| lines = result.tail.splitlines() | ||
|
|
@@ -119,6 +122,33 @@ def test_tail_merges_streams_and_truncates(git_repo, bwrap_log, command, exit_co | |
| assert len(result.tail.encode("utf-8")) <= 16 * 1024 | ||
|
|
||
|
|
||
| def test_verbose_stage_bounds_parent_memory(git_repo): | ||
| command = shlex.join([sys.executable, "-c", """ | ||
| import os | ||
| import sys | ||
|
|
||
| block = b"x" * (1024 * 1024) | ||
| for _ in range(50): | ||
| sys.stdout.buffer.write(block) | ||
| sys.stdout.flush() | ||
| os.write(1, b"\\r\\nlast stdout\\r\\n") | ||
| os.write(2, "final Ω diagnostic\\r\\n".encode()) | ||
| sys.exit(7) | ||
| """]) | ||
| tracemalloc.start() | ||
| try: | ||
| tracemalloc.reset_peak() | ||
| baseline, _ = tracemalloc.get_traced_memory() | ||
| result = sandbox._run_one(make_config(), git_repo, [], command, 30) | ||
| _, peak = tracemalloc.get_traced_memory() | ||
| finally: | ||
| tracemalloc.stop() | ||
| assert result.exit_code == 7 | ||
| assert result.tail.endswith("\nlast stdout\nfinal Ω diagnostic") | ||
| assert len(result.tail.encode()) == 16 * 1024 | ||
| assert peak - baseline < 4 * 1024 * 1024 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("command", "stage_exit"), | ||
| [ | ||
|
|
@@ -231,16 +261,27 @@ def test_temp_parent_cannot_be_exposed(git_repo, bwrap_log): | |
|
|
||
|
|
||
| def inject_check_timeout(monkeypatch, output, before_timeout=None): | ||
| communicate = subprocess.Popen.communicate | ||
| def read_output_tail(process, limit, timeout): | ||
| if before_timeout is not None: | ||
| before_timeout() | ||
| raise subprocess.TimeoutExpired(process.args, timeout, output=output[-limit:]) | ||
|
|
||
| monkeypatch.setattr(sandbox._proc, "_read_output_tail", read_output_tail) | ||
|
|
||
|
|
||
| def communicate_with_timeout(process, input=None, timeout=None): | ||
| if tuple(process.args[-6:-1]) == sandbox.CHECK_SHELL: | ||
| if before_timeout is not None: | ||
| before_timeout() | ||
| raise subprocess.TimeoutExpired(process.args, timeout, output=output) | ||
| return communicate(process, input, timeout) | ||
| def test_local_stage_timeout_retains_drained_output(git_repo): | ||
| command = shlex.join([sys.executable, "-c", """ | ||
| import os | ||
| import signal | ||
|
|
||
| monkeypatch.setattr(subprocess.Popen, "communicate", communicate_with_timeout) | ||
| os.write(1, b"x" * 20_000) | ||
| os.write(2, b"\\nlast available diagnostic\\n") | ||
| signal.pause() | ||
| """]) | ||
| result = sandbox._run_one(make_config(), git_repo, [], command, 1) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Real-timeout test depends on the child writing its output within 1s
|
||
| assert result.exit_code == sandbox.TIMEOUT_EXIT | ||
| assert result.tail.endswith("\nlast available diagnostic\n\n[loopzero] timed out after 1s") | ||
| assert len(result.tail.encode()) == 16 * 1024 | ||
|
|
||
|
|
||
| def test_timeout_is_recorded_not_raised(git_repo, bwrap_log, monkeypatch): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Document
_read_output_tail's trailing-newline contract next to_check_tail_read_output_tailholds trailing newlines separately. It drops them on success and keeps them, capped atlimit, on timeout. This matches what_check_tailproduces from the full text, which doesrstrip("\n")and then slices the last 16 KiB. The equivalence depends on themin(limit, ...)cap and on_check_tailrunningrstripfirst, and neither function mentions the other. A one-line docstring saying the result equalstext.rstrip('\n')[-limit:]on success andtext[-limit:]on timeout would stop the two from drifting apart in a later edit. Non-blocking.