Harden stock probe shutdown capture

This commit is contained in:
Selten23
2026-12-03 11:30:58 +04:00
parent ab74703571
commit 52dd8f4d55
3 changed files with 49 additions and 39 deletions

View File

@ -116,7 +116,7 @@ def snapshot_unit(unit, pending, read=read_owned):
def watch(source, output, stop):
deadline = time.monotonic() + TIME_LIMIT
result = {"status": "no fresh PBENTRY"}
while not stop.is_set() and time.monotonic() < deadline:
while time.monotonic() < deadline:
offset = source.tell()
line = source.readline(65537)
if len(line) > 65536:
@ -124,6 +124,8 @@ def watch(source, output, stop):
break
if not line.endswith("\n"):
source.seek(offset)
if stop.is_set():
break
stop.wait(0.02)
continue
try:

View File

@ -142,13 +142,15 @@ def watch(source, output, stop, read):
deadline = time.monotonic() + TIME_LIMIT
matched = set()
seen = 0
while not stop.is_set() and time.monotonic() < deadline and seen < MAX_CANDIDATES:
while time.monotonic() < deadline and seen < MAX_CANDIDATES:
offset = source.tell()
line = source.readline(8193)
if len(line) > 8192:
break
if not line.endswith("\n"):
source.seek(offset)
if stop.is_set():
break
stop.wait(0.001)
continue
row = json.loads(line)

View File

@ -83,45 +83,51 @@ def sample_run(mapping, device, folder, probe, uuid, node):
unstable = 0
count = 0
start = time.monotonic()
stdout_path = folder / "probe.stdout"
stderr_path = folder / "probe.stderr"
with raw.open("x", encoding="utf-8", buffering=1) as stream:
with subprocess.Popen(
[sys.executable, str(Path(__file__).with_name("stock_ce0_owned.py")),
str(raw), str(owned), str(ready), node, str(probe)],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, env=env,
) as process:
try:
while process.poll() is None and time.monotonic() - start < TIME_LIMIT:
stock_driver(device)
if not ready.exists() or count >= MAX_CANDIDATES:
with stdout_path.open("x", encoding="utf-8") as stdout_stream:
with stderr_path.open("x", encoding="utf-8") as stderr_stream:
with subprocess.Popen(
[sys.executable, str(Path(__file__).with_name("stock_ce0_owned.py")),
str(raw), str(owned), str(ready), node, str(probe)],
stdout=stdout_stream, stderr=stderr_stream, text=True, env=env,
) as process:
try:
while process.poll() is None and time.monotonic() - start < TIME_LIMIT:
stock_driver(device)
if not ready.exists() or count >= MAX_CANDIDATES:
time.sleep(0.001)
continue
for unit in range(4):
state = snapshot(mapping, unit)
if state is None:
unstable += 1
continue
if state == baseline[unit]:
continue
selected = stage(state)
if selected and selected[0] == LATE_STAGE and later is None:
later = {"unit": unit, "state": state}
if selected is None or per_stage[selected[0]] >= CANDIDATES_PER_STAGE:
continue
key = (unit, *(state[field] for field in ANCHORS))
if key in seen:
continue
seen.add(key)
row = {"elapsed": time.monotonic() - start, "unit": unit, "state": state}
stream.write(json.dumps(row) + "\n")
count += 1
per_stage[selected[0]] += 1
if count >= MAX_CANDIDATES:
break
time.sleep(0.001)
continue
for unit in range(4):
state = snapshot(mapping, unit)
if state is None:
unstable += 1
continue
if state == baseline[unit]:
continue
selected = stage(state)
if selected and selected[0] == LATE_STAGE and later is None:
later = {"unit": unit, "state": state}
if selected is None or per_stage[selected[0]] >= CANDIDATES_PER_STAGE:
continue
key = (unit, *(state[field] for field in ANCHORS))
if key in seen:
continue
seen.add(key)
row = {"elapsed": time.monotonic() - start, "unit": unit, "state": state}
stream.write(json.dumps(row) + "\n")
count += 1
per_stage[selected[0]] += 1
if count >= MAX_CANDIDATES:
break
time.sleep(0.001)
finally:
if process.poll() is None:
process.kill()
stdout, stderr = process.communicate(timeout=3)
finally:
if process.poll() is None:
process.kill()
process.wait(timeout=3)
stdout = stdout_path.read_text(encoding="utf-8")
stderr = stderr_path.read_text(encoding="utf-8")
calls = [json.loads(line) for line in stdout.splitlines() if line.startswith("{")]
results = [json.loads(line) for line in owned.read_text().splitlines()] if owned.exists() else []
matched = {row["stage"] for row in results if row.get("packet_matches")}