Skip to content

Commit 9dda4ac

Browse files
committed
preserve(test): Reap native hook descendants
why: A post-checkout hook can be reparented after its supervisor first sees no waitable child. what: Pass expected process IDs to the supervisor and wait for SIGCHLD until each has been reaped.
1 parent b7d0d3b commit 9dda4ac

1 file changed

Lines changed: 26 additions & 9 deletions

File tree

‎tests/sync/test_preservation_crash.py‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,36 @@ def publish(self, token, record, value):
6666
[sys.executable, '-c', sys.argv[1], *sys.argv[2:]], start_new_session=True
6767
)
6868
try:
69-
sys.stdin.buffer.read(1)
69+
event = json.load(sys.stdin)
7070
finally:
71+
expected = {child.pid}
72+
if 'native' in event:
73+
expected.update((event['native'], event['pid']))
74+
previous_mask = signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGCHLD})
7175
try:
7276
os.killpg(child.pid, signal.SIGKILL)
7377
except ProcessLookupError:
7478
pass
75-
code = child.wait()
76-
reaped = []
77-
while True:
79+
reaped = {}
80+
while expected - reaped.keys():
7881
try:
79-
pid, status = os.waitpid(-1, 0)
80-
reaped.append([pid, status])
82+
while True:
83+
pid, status = os.waitpid(-1, os.WNOHANG)
84+
if pid == 0:
85+
break
86+
reaped[pid] = status
8187
except ChildProcessError:
82-
break
83-
print(json.dumps({'owner': child.pid, 'code': code, 'reaped': reaped}), flush=True)
88+
pass
89+
if expected - reaped.keys():
90+
signal.sigwait({signal.SIGCHLD})
91+
signal.pthread_sigmask(signal.SIG_SETMASK, previous_mask)
92+
code = os.waitstatus_to_exitcode(reaped[child.pid])
93+
print(
94+
json.dumps(
95+
{'owner': child.pid, 'code': code, 'reaped': list(reaped.items())}
96+
),
97+
flush=True,
98+
)
8499
"""
85100

86101
_HOOK = r"""
@@ -204,7 +219,9 @@ def _kill_at_barrier(
204219
assert repo.get_position().revision == checkout.target
205220
native = pathlib.Path(f"/proc/{event['native']}/comm")
206221
assert native.read_text().strip() == "git"
207-
stdout, stderr = supervisor.communicate("kill", timeout=10)
222+
stdout, stderr = supervisor.communicate(
223+
json.dumps(event), timeout=10
224+
)
208225
except TimeoutError:
209226
stdout, stderr = supervisor.communicate("kill", timeout=10)
210227
pytest.fail(f"barrier not reached: {stdout} {stderr}")

0 commit comments

Comments
 (0)