"""Run the native importer without aborting database owners on timeout.""" import json import os from pathlib import Path import subprocess import tempfile import threading def run_content_check(command, timeout_seconds, cwd, *, evidence_root=None): root = (Path(evidence_root) if evidence_root is not None else Path(tempfile.mkdtemp(prefix='cbb-content-check-'))) root.mkdir(parents=True, exist_ok=True) stdout_path, stderr_path = root/'content-check.stdout.log', root/'content-check.stderr.log' record_path = root/'content-check-process.json' with stdout_path.open('wb') as stdout, stderr_path.open('wb') as stderr: process = subprocess.Popen(command, cwd=str(cwd), stdout=stdout, stderr=stderr, creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0)) record = {'pid': process.pid, 'command': list(command), 'cwd': str(cwd), 'stdout': str(stdout_path), 'stderr': str(stderr_path)} def save_record(**fields): try: record_path.write_text(json.dumps({**record, **fields}, ensure_ascii=False, indent=2), encoding='utf-8') except OSError: pass save_record(status='RUNNING') try: process.wait(timeout=max(.1, float(timeout_seconds))) except subprocess.TimeoutExpired: # Never kill this client or its descendants. The importer can own the # shared symbol database even though contents checking sounds read-only. # Files avoid pipe backpressure/BrokenPipe if the supervisor exits. save_record(status='RETAINED', safe_to_continue=False) def observe_exit(): process.wait() save_record(status='EXITED', returncode=process.returncode, note='Importer exit alone does not prove native database release.') threading.Thread(target=observe_exit, name='retained-content-check', daemon=True).start() return (None, stdout_path.read_bytes().decode('utf-8', errors='replace'), stderr_path.read_bytes().decode('utf-8', errors='replace') + f'\nIMPORTER_RELEASE_UNCONFIRMED: pid={process.pid}; ' f'内容检查超时,保留原生操作并停止批次;进程记录:{record_path}', True) save_record(status='EXITED', returncode=process.returncode) return (process.returncode, stdout_path.read_bytes().decode('utf-8', errors='replace'), stderr_path.read_bytes().decode('utf-8', errors='replace'), False)