|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Bounded diagnostics for historical Linux Bun crashes before lockfile edits. |
| 3 | +
|
| 4 | +Run after a failed binary matrix. This never changes its acceptance result or |
| 5 | +downloads replacement tools. Each installed Bun gets a pristine package project |
| 6 | +and a 30-second total budget, including an optional strace reproduction. |
| 7 | +""" |
| 8 | + |
| 9 | +import argparse |
| 10 | +import json |
| 11 | +import os |
| 12 | +from pathlib import Path |
| 13 | +import platform |
| 14 | +import shutil |
| 15 | +import signal |
| 16 | +import subprocess |
| 17 | +import tempfile |
| 18 | +import time |
| 19 | + |
| 20 | + |
| 21 | +def run(command, cwd, env, output, label, timeout): |
| 22 | + started = time.monotonic() |
| 23 | + row = {'command': [str(part) for part in command], 'timeoutSeconds': timeout} |
| 24 | + try: |
| 25 | + with (output / f'{label}.stdout').open('wb') as stdout, \ |
| 26 | + (output / f'{label}.stderr').open('wb') as stderr: |
| 27 | + process = subprocess.Popen(command, cwd=cwd, env=env, stdin=subprocess.DEVNULL, |
| 28 | + stdout=stdout, stderr=stderr, start_new_session=True) |
| 29 | + try: |
| 30 | + process.wait(timeout=timeout) |
| 31 | + except subprocess.TimeoutExpired: |
| 32 | + row['timedOut'] = True |
| 33 | + os.killpg(process.pid, signal.SIGKILL) |
| 34 | + process.wait(timeout=5) |
| 35 | + row['returnCode'] = process.returncode |
| 36 | + if process.returncode < 0: |
| 37 | + row['signal'] = signal.Signals(-process.returncode).name |
| 38 | + except (OSError, subprocess.SubprocessError) as error: |
| 39 | + row['error'] = str(error) |
| 40 | + row['elapsedSeconds'] = round(time.monotonic() - started, 3) |
| 41 | + (output / f'{label}.json').write_text(json.dumps(row, indent=2) + '\n') |
| 42 | + print(json.dumps({'probe': label, **row}), flush=True) |
| 43 | + return row |
| 44 | + |
| 45 | + |
| 46 | +def fixture(root): |
| 47 | + # Match the acceptance test's Unicode/spaced path and isolated cache/temp. |
| 48 | + project = root / 'binary project café' |
| 49 | + project.mkdir(parents=True) |
| 50 | + (project / 'package.json').write_text(json.dumps({ |
| 51 | + 'name': 'native-binary-bun', 'version': '1.0.0', 'private': True, |
| 52 | + 'dependencies': {'minimist': '1.2.2', 'is-number': '7.0.0'}, |
| 53 | + }) + '\n') |
| 54 | + (project / 'bunfig.toml').write_text('[install]\nsaveTextLockfile = false\n') |
| 55 | + env = {key: value for key, value in os.environ.items() |
| 56 | + if not key.startswith(('BUN_', 'SOCKET_', 'NPM_CONFIG_', 'npm_config_'))} |
| 57 | + for key, name in { |
| 58 | + 'HOME': 'home', 'XDG_CONFIG_HOME': 'config', 'XDG_CACHE_HOME': 'cache', |
| 59 | + 'BUN_INSTALL': 'bun-home', 'BUN_INSTALL_CACHE_DIR': 'bun-cache', |
| 60 | + 'TMPDIR': 'temporary', 'TMP': 'temporary', 'TEMP': 'temporary', |
| 61 | + 'BUN_TMPDIR': 'temporary', |
| 62 | + }.items(): |
| 63 | + path = root / name |
| 64 | + path.mkdir(exist_ok=True) |
| 65 | + env[key] = str(path) |
| 66 | + return project, env |
| 67 | + |
| 68 | + |
| 69 | +def main(): |
| 70 | + parser = argparse.ArgumentParser(description=__doc__) |
| 71 | + parser.add_argument('--tools', type=Path, required=True) |
| 72 | + parser.add_argument('--output', type=Path, required=True) |
| 73 | + args = parser.parse_args() |
| 74 | + output = args.output.resolve() |
| 75 | + output.mkdir(parents=True, exist_ok=True) |
| 76 | + if platform.system() != 'Linux': |
| 77 | + print('Historical Linux diagnostics require Linux; no probes run.') |
| 78 | + return |
| 79 | + |
| 80 | + context = {'platform': platform.platform(), 'strace': shutil.which('strace')} |
| 81 | + for name in ['kernel/io_uring_disabled', 'kernel/io_uring_group', 'vm/mmap_rnd_bits']: |
| 82 | + path = Path('/proc/sys') / name |
| 83 | + context[name] = path.read_text().strip() if path.exists() else None |
| 84 | + for name in ['status', 'limits']: |
| 85 | + path = Path('/proc/self') / name |
| 86 | + (output / f'process-{name}.txt').write_text(path.read_text()) |
| 87 | + (output / 'context.json').write_text(json.dumps(context, indent=2) + '\n') |
| 88 | + for executable, arguments in [('uname', ['-a']), ('lscpu', []), ('ldd', ['--version'])]: |
| 89 | + if program := shutil.which(executable): |
| 90 | + run([program, *arguments], output, os.environ.copy(), output, executable, 5) |
| 91 | + |
| 92 | + summary = [] |
| 93 | + for version in ['0.5.9', '0.6.7', '0.6.8']: |
| 94 | + matches = sorted((args.tools / version).glob('bun-linux-*/bun')) |
| 95 | + if not matches: |
| 96 | + summary.append({'version': version, 'error': 'downloaded Bun executable missing'}) |
| 97 | + continue |
| 98 | + bun = matches[0].resolve() |
| 99 | + deadline = time.monotonic() + 30 |
| 100 | + with tempfile.TemporaryDirectory(prefix=f'bun-linux-probe-{version}-', dir=output) as temporary: |
| 101 | + root = Path(temporary) |
| 102 | + project, env = fixture(root / 'pristine') |
| 103 | + plain = run([bun, 'install', '--ignore-scripts'], project, env, output, |
| 104 | + f'{version}-pristine', min(10, deadline - time.monotonic())) |
| 105 | + row = {'version': version, 'pristine': plain} |
| 106 | + if plain.get('returnCode') != 0 and context['strace']: |
| 107 | + project, env = fixture(root / 'traced') |
| 108 | + row['strace'] = run([ |
| 109 | + context['strace'], '-f', '-tt', '-s', '160', '-o', |
| 110 | + output / f'{version}.strace', bun, 'install', '--ignore-scripts', |
| 111 | + ], project, env, output, f'{version}-traced', max(1, deadline - time.monotonic())) |
| 112 | + summary.append(row) |
| 113 | + (output / 'summary.json').write_text(json.dumps(summary, indent=2) + '\n') |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + main() |
0 commit comments