data-gen-bpu/scripts/test_execution_parent_receipt.py
ming01.wang 7440ba8cb8 fix: verify nested reaper cleanup through enclosing receipts
Cancellation can end a compiler parent before its nested reaper. The nested
receipt then reports owner loss even when both reapers reached waitpid(ECHILD).
Recognize the verified enclosing receipt only when execution, epoch, immediate
parent, and full supervisor birth identity match and the nested children were
fully reaped. Preserve the original receipts and fail closed without that proof.

Add eight regression tests, including identity mismatch, incomplete cleanup,
missing parent proof, and live-process preservation. The positive regression
fails on the previous code and passes on the patch; recorded incident receipts
also distinguish the two implementations without changing the cancelled outcome.
2026-09-22 20:29:45 +08:00

105 lines
5.1 KiB
Python

"""A closed enclosing reaper can prove cleanup after a nested owner is lost."""
import copy
import json
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
from harness import execution
class ParentReceiptTests(unittest.TestCase):
def setUp(self):
temp = tempfile.TemporaryDirectory()
self.addCleanup(temp.cleanup)
self.root = Path(temp.name)
def identity(pid, ppid):
return dict(host='fixture', boot_id='fixture-boot', pid=pid, ppid=ppid,
proc_start_ticks=pid * 100)
parent = dict(execution_id='execution', epoch=1, proof='waitpid:ECHILD',
supervisor=identity(100, 1), leader=identity(101, 100))
child = dict(execution_id='execution', epoch=1, proof='waitpid:ECHILD',
supervisor=identity(102, 101), leader=identity(103, 102))
self.state = dict(processes=[], process_sets={}, process_scopes={
'parent': dict(binding=parent, receipt_path=str(self.root / 'parent.json')),
'child': dict(binding=child, receipt_path=str(self.root / 'child.json'))})
self.receipts = {
'parent': dict(binding=parent, exit_code=-15, cleanup_state='VERIFIED',
all_children_reaped=True, owner_lost=False,
members=[copy.deepcopy(child['supervisor'])]),
'child': dict(binding=child, exit_code=-15, cleanup_state='QUARANTINED',
all_children_reaped=True, owner_lost=True, supervisor_error=None)}
def inspect(self, state=None, receipts=None, status='DEAD'):
state = self.state if state is None else state
receipts = self.receipts if receipts is None else receipts
for name, value in receipts.items():
(self.root / (name + '.json')).write_text(json.dumps(value))
original = {p: p.read_bytes() for p in self.root.glob('*.json')}
with patch.object(execution, 'process_status', return_value=status):
result = execution.owned_processes(state)
self.assertEqual(original, {p: p.read_bytes() for p in original}, 'original receipts must not be rewritten')
return result
def test_verified_parent_closes_nested_owner_loss(self):
self.assertEqual(self.inspect(), [])
def test_dead_pids_without_enclosing_receipt_are_not_proof(self):
self.state['process_scopes'].pop('parent')
self.assertIsNone(self.inspect())
def test_incomplete_or_failed_nested_cleanup_stays_unknown(self):
for changes in ({'all_children_reaped': False}, {'owner_lost': False},
{'supervisor_error': 'cleanup deadline exceeded'}, {'exit_code': None}):
with self.subTest(changes=changes):
receipts = copy.deepcopy(self.receipts)
receipts['child'].update(changes)
self.assertIsNone(self.inspect(receipts=receipts))
def test_unverified_enclosing_cleanup_stays_unknown(self):
for changes in ({'cleanup_state': 'QUARANTINED'}, {'all_children_reaped': False},
{'owner_lost': True}, {'members': []}, {'exit_code': None}):
with self.subTest(changes=changes):
receipts = copy.deepcopy(self.receipts)
receipts['parent'].update(changes)
self.assertIsNone(self.inspect(receipts=receipts))
def test_reused_pid_or_other_host_is_not_the_enclosed_supervisor(self):
for field, value in (('pid', 999), ('proc_start_ticks', -1), ('host', 'other-host'),
('boot_id', 'other-boot')):
with self.subTest(field=field):
receipts = copy.deepcopy(self.receipts)
receipts['parent']['members'][0][field] = value
self.assertIsNone(self.inspect(receipts=receipts))
def test_parent_must_match_execution_epoch_and_immediate_parent(self):
for field, value in (('execution_id', 'other-execution'), ('epoch', 2), ('leader_pid', 999)):
with self.subTest(field=field):
state, receipts = copy.deepcopy((self.state, self.receipts))
binding = state['process_scopes']['parent']['binding']
if field == 'leader_pid':
binding['leader']['pid'] = value
else:
binding[field] = value
# The receipt still binds correctly; its relationship is wrong.
self.assertIs(receipts['parent']['binding'], binding)
self.assertIsNone(self.inspect(state=state, receipts=receipts))
def test_changed_receipt_binding_stays_unknown(self):
receipts = copy.deepcopy(self.receipts)
receipts['parent']['binding']['supervisor']['proc_start_ticks'] += 1
self.assertIsNone(self.inspect(receipts=receipts))
def test_live_recorded_process_is_not_hidden_by_receipts(self):
self.state['processes'] = [self.state['process_scopes']['child']['binding']['supervisor']]
members = self.inspect(status='ALIVE')
self.assertEqual(len(members), 1)
self.assertEqual(members[0]['pid'], 102)
if __name__ == '__main__':
unittest.main()