main zmx / test / kill_run_race.bats
Kai Fronsdal  ·  2026-06-10
 1#!/usr/bin/env bats
 2# Regression test for the `zmx kill X; zmx run X` race.
 3#
 4# Previously `zmx kill` returned immediately after sending the IPC .Kill,
 5# while the daemon's shutdown defer ran handleKill() -- SIGHUP, 500ms sleep,
 6# SIGKILL -- BEFORE closing/unlinking the listen socket. A `zmx run X`
 7# issued in that window would connect() into the kernel backlog of a
 8# socket the daemon would never accept() on again, then get RST'd
 9# (ConnectionResetByPeer) when the daemon finally closed the listen fd,
10# exiting 1 with no output and no session created.
11
12load test_helper
13
14@test "kill then immediate run with same name succeeds" {
15  for i in 1 2 3; do
16    "$ZMX" run race-x -d echo first
17    wait_for_session race-x
18
19    "$ZMX" kill race-x
20
21    # Immediately reuse the same session name. Must not land in the
22    # dying daemon's listen backlog.
23    run "$ZMX" run race-x -d echo second
24    echo "iteration $i: status=$status output=$output"
25    [ "$status" -eq 0 ]
26    [[ "$output" == *"session \"race-x\" created"* ]]
27
28    # New session must be live and serving requests.
29    wait_for_session race-x
30    run "$ZMX" history race-x
31    [ "$status" -eq 0 ]
32
33    "$ZMX" kill race-x
34  done
35}