Author SHA1 Message Date
Pavlo Ratushnyi bbc8018e8b Bound runtime image probe waits and cleanup
Linux / keyless-cuda-gate (pull_request) Skipped
Desktop foundation / core (ubuntu-latest) (pull_request) Successful in 2m50s
Linux / verify (pull_request) Successful in 6m33s
Linux / keyless-cuda (pull_request) Skipped
Linux / static-cuda-package (pull_request) Skipped
Linux / hosted-cuda-package (pull_request) Skipped
Linux / linux-cli-container (pull_request) Successful in 1m34s
Linux / linux-static-cli (pull_request) Successful in 6m26s
Linux / release-cli (pull_request) Skipped
2026-10-08 00:53:28 +02:00
2 changed files with 235 additions and 3 deletions
+96 -3
View File
@@ -9,6 +9,7 @@ import re
import shutil
import stat
import subprocess
import sys
import tempfile
import uuid
@@ -17,6 +18,9 @@ NAME = 'strata-next-linux-x86_64-musl'
FILES = {NAME, 'PROVENANCE.json', 'SHA256SUMS'}
# Official Ubuntu 26.04 linux/amd64 manifest, observed 2026-10-04; glibc is probed.
BASE = 'ubuntu@sha256:88a381d5b5eeb2b35d3ad70925a362c37ce569daf43ede89ff818ec20e4d3794'
PROBE_INFRA_TIMEOUT = 120
PROBE_APP_TIMEOUT = 30
PROBE_OUTPUT_LIMIT = 64 * 1024
def digest(path):
@@ -124,18 +128,107 @@ def docker(*args, timeout=30, check=True):
check=check, timeout=timeout)
def _bounded_output(value, stream):
if not isinstance(value, str) or len(value.encode('utf-8')) > PROBE_OUTPUT_LIMIT:
raise ValueError(f'isolated CLI {stream} exceeds {PROBE_OUTPUT_LIMIT}-byte limit')
return value
def _report_best_effort(message):
try:
print(message, file=sys.stderr, flush=True)
except BaseException:
pass
def _safe_report(message):
try:
_report_best_effort(message)
except BaseException:
pass
def _diagnose_owned_container(name):
for label, args in (
('inspect', ('inspect', '--format', '{{json .State}}', name)),
('logs', ('logs', '--tail', '40', name)),
):
try:
result = docker(*args, timeout=8, check=False)
output = (result.stdout or '') + (result.stderr or '')
if len(output.encode('utf-8')) > PROBE_OUTPUT_LIMIT:
output = '[earlier output truncated]\n' + output[-PROBE_OUTPUT_LIMIT:]
_safe_report(
f'Owned runtime-image probe {label} (exit {result.returncode}):\n{output}')
except BaseException as error:
_safe_report(
f'Owned runtime-image probe {label} unavailable: {type(error).__name__}')
def isolated_run(image, arguments, *, entrypoint=None):
name = 'strata-image-probe-' + uuid.uuid4().hex
args = ['run', '--name', name, '--rm', '--platform', 'linux/amd64', '--network', 'none',
args = ['create', '--name', name, '--platform', 'linux/amd64', '--network', 'none',
'--read-only', '--user', '65532:65532', '--cap-drop', 'ALL',
'--security-opt', 'no-new-privileges', '-e', 'CUDA_VISIBLE_DEVICES=',
'-e', 'NVIDIA_VISIBLE_DEVICES=void']
if entrypoint:
args += ['--entrypoint', entrypoint]
primary_error = None
try:
return docker(*args, image, *arguments).stdout
created = docker(*args, image, *arguments, timeout=PROBE_INFRA_TIMEOUT)
_bounded_output(created.stdout, 'create output')
docker('start', name, timeout=PROBE_INFRA_TIMEOUT)
waited = docker('wait', name, timeout=PROBE_APP_TIMEOUT, check=False)
_bounded_output(waited.stdout, 'wait status')
_bounded_output(waited.stderr, 'wait stderr')
if waited.returncode:
raise subprocess.CalledProcessError(
waited.returncode, ['docker', 'wait', name],
output=waited.stdout, stderr=waited.stderr)
if not re.fullmatch(r'(?:0|[1-9][0-9]{0,2})\n?', waited.stdout):
raise ValueError('docker wait returned a malformed CLI exit status')
exit_status = int(waited.stdout.rstrip('\n'))
if exit_status > 255:
raise ValueError('docker wait returned a CLI exit status outside 0..255')
logs = docker('logs', name, timeout=30, check=False)
stdout = _bounded_output(logs.stdout, 'stdout')
stderr = _bounded_output(logs.stderr, 'stderr')
if logs.returncode:
raise subprocess.CalledProcessError(
logs.returncode, ['docker', 'logs', name], output=stdout, stderr=stderr)
if exit_status:
raise subprocess.CalledProcessError(
exit_status, ['docker', 'container', 'probe', name],
output=stdout, stderr=stderr)
if stderr:
_report_best_effort(f'Runtime-image probe stderr:\n{stderr}')
return stdout
except BaseException as error:
primary_error = error
try:
_diagnose_owned_container(name)
except BaseException as diagnostic_error:
_safe_report(
f'Owned runtime-image probe diagnostics failed: {type(diagnostic_error).__name__}')
raise
finally:
docker('rm', '-f', name, check=False)
cleanup_error = None
try:
result = docker('rm', '-f', name, timeout=10, check=False)
if result.returncode:
cleanup_error = subprocess.CalledProcessError(
result.returncode, ['docker', 'rm', '-f', name],
output=result.stdout, stderr=result.stderr)
except BaseException as error:
cleanup_error = error
if cleanup_error is not None:
if primary_error is None:
raise cleanup_error
_safe_report(
f'Owned runtime-image probe cleanup failed after '
f'{type(primary_error).__name__}: {type(cleanup_error).__name__}')
def assemble(args):
+139
View File
@@ -3,6 +3,7 @@
import importlib.util
import hashlib
import json
import subprocess
from pathlib import Path
import shutil
import tempfile
@@ -156,5 +157,143 @@ class PackageTests(unittest.TestCase):
b.frozen_file(self.pkg/b.NAME, Path(stage)/'out', 1)
class IsolatedProbeTests(unittest.TestCase):
def setUp(self):
reporter = patch.object(b, '_report_best_effort')
reporter.start()
self.addCleanup(reporter.stop)
def fake_docker(self, calls, *, wait_stdout='0\n', wait_stderr='', wait_rc=0,
logs_stdout='probe output\n', logs_stderr='', logs_rc=0,
start_error=None, wait_error=None, cleanup_error=None, logs_size=None):
container = None
def invoke(*args, timeout=30, check=True):
nonlocal container
calls.append((args, timeout, check))
if args[0] == 'create':
container = args[args.index('--name') + 1]
return SimpleNamespace(stdout='a' * 64 + '\n', stderr='', returncode=0)
if args[0] == 'start':
self.assertEqual(args[1], container)
if start_error is not None:
raise start_error
return SimpleNamespace(stdout=container + '\n', stderr='', returncode=0)
if args[0] == 'wait':
self.assertEqual(args[1], container)
if wait_error is not None:
raise wait_error
return SimpleNamespace(stdout=wait_stdout, stderr=wait_stderr, returncode=wait_rc)
if args[0] == 'logs':
self.assertEqual(args[-1], container)
stdout = logs_stdout if logs_size is None else 'x' * logs_size
return SimpleNamespace(stdout=stdout, stderr=logs_stderr, returncode=logs_rc)
if args[:2] == ('inspect', '--format'):
self.assertEqual(args[-1], container)
raise OSError('synthetic inspect unavailable')
if args[0] == 'inspect':
self.assertEqual(args[-1], container)
raise OSError('synthetic inspect unavailable')
if args[:2] == ('rm', '-f'):
self.assertEqual(args[-1], container)
if cleanup_error is not None:
raise cleanup_error
return SimpleNamespace(stdout='', stderr='', returncode=0)
raise AssertionError(f'unexpected Docker call: {args!r}')
return invoke
def test_probe_uses_long_infrastructure_deadline_and_original_cli_deadline(self):
calls = []
with patch.object(b, 'docker', side_effect=self.fake_docker(calls)):
output = b.isolated_run('sha256:' + 'a' * 64, ['engine-info'], entrypoint='strata-next')
self.assertEqual(output, 'probe output\n')
operations = [call[0][0] for call in calls]
self.assertEqual(operations, ['create', 'start', 'wait', 'logs', 'rm'])
create, start, wait, logs, remove = calls
self.assertEqual(create[1], 120)
self.assertEqual(start[1], 120)
self.assertEqual(wait[1], 30)
self.assertEqual(logs[1], 30)
self.assertEqual(remove[1], 10)
self.assertNotIn('--rm', create[0])
self.assertEqual(create[0][create[0].index('--network') + 1], 'none')
self.assertIn('--read-only', create[0])
self.assertIn('--cap-drop', create[0])
self.assertIn('65532:65532', create[0])
self.assertIn('strata-next', create[0])
self.assertEqual(start[0][1], create[0][create[0].index('--name') + 1])
self.assertEqual(wait[0][1], start[0][1])
self.assertEqual(logs[0][-1], start[0][1])
self.assertEqual(remove[0][-1], start[0][1])
def test_probe_rejects_malformed_wait_status_and_bounds_both_streams(self):
for status in ('', '0\n1\n', '-1\n', '256\n'):
with self.subTest(status=status):
calls = []
with patch.object(b, 'docker', side_effect=self.fake_docker(calls, wait_stdout=status)):
with self.assertRaisesRegex(ValueError, 'malformed CLI exit status|outside 0..255'):
b.isolated_run('image', ['engine-help'])
self.assertEqual([x[0][0] for x in calls[:3]], ['create', 'start', 'wait'])
self.assertEqual(calls[-1][0][0:2], ('rm', '-f'))
for stream in ('stdout', 'stderr'):
with self.subTest(stream=stream):
calls = []
kwargs = {'logs_size': b.PROBE_OUTPUT_LIMIT + 1} if stream == 'stdout' else {
'logs_stderr': 'x' * (b.PROBE_OUTPUT_LIMIT + 1)}
with patch.object(b, 'docker', side_effect=self.fake_docker(calls, **kwargs)):
with self.assertRaisesRegex(ValueError, f'CLI {stream} exceeds'):
b.isolated_run('image', ['engine-help'])
self.assertEqual(calls[-1][0][0:2], ('rm', '-f'))
def test_nonzero_cli_exit_preserves_both_streams(self):
calls = []
with patch.object(b, 'docker', side_effect=self.fake_docker(
calls, wait_stdout='17\n', logs_stdout='help stdout', logs_stderr='help stderr')):
with self.assertRaises(subprocess.CalledProcessError) as raised:
b.isolated_run('image', ['engine-help'])
self.assertEqual(raised.exception.returncode, 17)
self.assertEqual(raised.exception.output, 'help stdout')
self.assertEqual(raised.exception.stderr, 'help stderr')
self.assertEqual([x[0][0] for x in calls[:4]], ['create', 'start', 'wait', 'logs'])
self.assertEqual(calls[-1][0][0:2], ('rm', '-f'))
def test_infrastructure_timeout_keeps_original_across_broken_reporting_and_cleanup(self):
calls = []
timeout = subprocess.TimeoutExpired(['docker', 'start'], 120)
with patch.object(b, 'docker', side_effect=self.fake_docker(
calls, start_error=timeout,
cleanup_error=subprocess.TimeoutExpired(['docker', 'rm'], 10))), \
patch.object(b, '_report_best_effort', side_effect=BrokenPipeError('closed stderr')):
with self.assertRaises(subprocess.TimeoutExpired) as raised:
b.isolated_run('image', ['engine-info'])
self.assertIs(raised.exception, timeout)
operations = [x[0][0] for x in calls]
self.assertEqual(operations, ['create', 'start', 'inspect', 'logs', 'rm'])
container = calls[0][0][calls[0][0].index('--name') + 1]
self.assertTrue(all(call[0][-1] == container for call in calls[2:]))
self.assertTrue(all(call[1] <= 10 for call in calls[2:]))
def test_application_timeout_is_not_extended_by_infrastructure_deadlines(self):
calls = []
timeout = subprocess.TimeoutExpired(['docker', 'wait'], 30)
with patch.object(b, 'docker', side_effect=self.fake_docker(calls, wait_error=timeout)):
with self.assertRaises(subprocess.TimeoutExpired) as raised:
b.isolated_run('image', ['engine-help'])
self.assertIs(raised.exception, timeout)
self.assertEqual([x[1] for x in calls[:3]], [120, 120, 30])
self.assertEqual(calls[-1][0][:2], ('rm', '-f'))
def test_cleanup_failure_after_success_is_not_silently_ignored(self):
calls = []
with patch.object(b, 'docker', side_effect=self.fake_docker(
calls, cleanup_error=OSError('synthetic cleanup failure'))):
with self.assertRaisesRegex(OSError, 'synthetic cleanup failure'):
b.isolated_run('image', ['engine-info'])
self.assertEqual(calls[-1][0][0:2], ('rm', '-f'))
self.assertEqual(calls[-1][1], 10)
if __name__ == '__main__':
unittest.main()