Skip to content

Commit bf60636

Browse files
committed
test(cli): no timeout decides a cli_scope test; finish keeps the environment
Follow-up to DeusData#2589. No timeout decides a cli_scope test any more: - The status probe and the parent's client connect repeat an attempt that reaches a live but mute endpoint (a loaded runner answering slowly). A reply or a definite refusal decides. Each attempt keeps the API's finite 5 s budget. This replaces the 15 s status budget and the single 5 s connect. - The host child is reaped without a deadline (it used to get SIGKILL after 60 s). - The host child joins its version cohort with UINT64_MAX, which waits for locks but answers a conflicting holder at once (it used to have a 45 s deadline). A wedged child is left to the harness's per-suite ceiling. cli_scope_fixture_finish restored HOME, CBM_CACHE_DIR and SHELL even when start had failed before saving them, which unset them for every later test in the suite. It now restores only what start saved. New tests: - cli_scope_fixture_finish_keeps_env_after_an_early_failure fails on main at ASSERT(home_kept); - cli_scope_retries_a_mute_endpoint_until_it_answers checks the retry rule on scripted attempt outcomes. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent f216a10 commit bf60636

1 file changed

Lines changed: 172 additions & 46 deletions

File tree

‎tests/test_cli.c‎

Lines changed: 172 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,25 +1572,30 @@ TEST(cli_install_recovers_markerless_stale_rendezvous) {
15721572
* install under test and asks the daemon itself afterwards. */
15731573
#define CLI_SCOPE_HOST_DRAINED 3
15741574
#define CLI_SCOPE_TIMEOUT_MS 5000U
1575-
/* The host child's readiness has NO deadline (USER RULE: no timeout decides a
1576-
* test). The child answers 'R' or 'E', or the pipe reaches EOF when it dies,
1577-
* and the fixture names the failing step in every case. A child that wedges
1578-
* before answering is left to the harness's per-suite ceiling. A 90 s bound
1579-
* used to decide this wait, and on a starved machine (2026-10-09, during a
1580-
* kernel index) ASSERT(ready) failed with nothing in the log to say why.
1581-
* Generous, bounded waits remain for reaping and status probes below. */
1582-
#define CLI_SCOPE_HOST_REAP_TIMEOUT_MS 60000U
1583-
#define CLI_SCOPE_HOST_SERVING_TIMEOUT_MS 15000U
1575+
/* No timeout decides a cli_scope test (USER RULE). That covers the host child's
1576+
* readiness, its cohort join, the parent's client connect, the "still serving"
1577+
* probe, and the reap:
1578+
* - the child answers 'R' or 'E', or the pipe reaches EOF when it dies, and the
1579+
* fixture names the failing step in every case;
1580+
* - each IPC attempt keeps the API's finite CLI_SCOPE_TIMEOUT_MS budget, but an
1581+
* attempt that reaches a live, mute endpoint (a loaded runner answering
1582+
* slowly) is repeated, and only a reply or a definite refusal decides
1583+
* (cli_scope_until_answered);
1584+
* - a child that wedges is left to the harness's per-suite ceiling.
1585+
* A 90 s readiness bound, a 15 s status budget and a 60 s reap used to decide
1586+
* these; on a starved machine (2026-10-09, during a kernel index)
1587+
* ASSERT(ready) failed with nothing in the log to say why. */
15841588
/* Teardown budget for a child the activation ALREADY drained: its service is
15851589
* gone, so each free/release succeeds immediately and this is only the
15861590
* give-up point if one unexpectedly does not. The undrained path keeps the
15871591
* full 2 x CLI_SCOPE_TIMEOUT_MS, which is where a wedged teardown is real. */
15881592
#define CLI_SCOPE_CLEANUP_DRAINED_MS 1000U
15891593
/* Budget for probing that an ALREADY-drained host has stopped serving.
15901594
*
1591-
* The generous CLI_SCOPE_HOST_SERVING_TIMEOUT_MS exists for the POSITIVE
1592-
* question -- "is this daemon still up?" -- where a slow reply on a loaded
1593-
* runner must not be misread as drained. Asked in the negative it inverts:
1595+
* The positive question -- "is this daemon still up?" -- repeats a mute
1596+
* attempt until the daemon answers (cli_scope_host_serving), because a slow
1597+
* reply on a loaded runner must not be misread as drained. Asked in the
1598+
* negative it inverts:
15941599
* there is no reply coming, so the whole budget is spent proving silence, and
15951600
* the assertion is decided by a timeout expiring rather than by the system's
15961601
* own behaviour. That was 15 s of the drain test's wall clock and the largest
@@ -1620,6 +1625,7 @@ typedef struct {
16201625
char *old_home;
16211626
char *old_cache;
16221627
char *old_shell;
1628+
bool env_saved; /* HOME, CBM_CACHE_DIR and SHELL were saved for finish */
16231629
char failed_step[160]; /* the setup step that failed, "" when ready */
16241630
} cli_scope_fixture_t;
16251631

@@ -1634,8 +1640,10 @@ static _Noreturn void cli_scope_host_child(const cli_scope_fixture_t *fixture, i
16341640
cbm_daemon_runtime_service_t *service = NULL;
16351641
int acquire_status = -1;
16361642
if (endpoint && manager) {
1637-
acquire_status = (int)cbm_version_cohort_acquire(manager, &fixture->identity,
1638-
cbm_now_ms() + 45000U, &lease, &conflict);
1643+
/* UINT64_MAX: wait as long as a lock is held, never for a conflicting
1644+
* holder (that is answered at once), so no deadline decides the join. */
1645+
acquire_status = (int)cbm_version_cohort_acquire(manager, &fixture->identity, UINT64_MAX,
1646+
&lease, &conflict);
16391647
}
16401648
bool admitted = acquire_status == (int)CBM_VERSION_COHORT_OK;
16411649
if (admitted) {
@@ -1745,32 +1753,60 @@ static bool cli_scope_setup_failed(cli_scope_fixture_t *fixture, const char *tag
17451753
return false;
17461754
}
17471755

1748-
/* Reap the host child within a bound: the release signal makes a healthy child
1749-
* break within ~50 ms and finish teardown in a few seconds, so a child still
1750-
* alive past the deadline is wedged — SIGKILL it and reap so the suite always
1751-
* makes progress. Returns the child's exit code, or -1 when it had to be
1752-
* killed or did not exit cleanly; a -1 fails the caller's host_exit assertion
1753-
* cleanly rather than hanging. */
1754-
static int cli_scope_reap_host(pid_t host, uint32_t timeout_ms) {
1755-
uint64_t deadline = cbm_now_ms() + timeout_ms;
1756+
/* Reap the host child, with no deadline: the release signal makes a healthy
1757+
* child exit within seconds, and a wedged one is left to the harness's
1758+
* per-suite ceiling (see CLI_SCOPE_TIMEOUT_MS). Returns the child's exit code,
1759+
* or -1 when it did not exit normally. */
1760+
static int cli_scope_reap_host(pid_t host) {
17561761
int status = 0;
17571762
for (;;) {
1758-
pid_t reaped = waitpid(host, &status, WNOHANG);
1763+
pid_t reaped = waitpid(host, &status, 0);
17591764
if (reaped == host) {
17601765
return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
17611766
}
17621767
if (reaped < 0 && errno != EINTR) {
17631768
return -1;
17641769
}
1765-
if (cbm_now_ms() >= deadline) {
1766-
(void)kill(host, SIGKILL);
1767-
(void)waitpid(host, &status, 0);
1768-
return -1;
1770+
}
1771+
}
1772+
1773+
/* One IPC attempt's outcome for cli_scope_until_answered. */
1774+
typedef enum {
1775+
CLI_SCOPE_ANSWERED, /* a reply arrived */
1776+
CLI_SCOPE_REFUSED, /* definite: no endpoint, a refusal, or a conflict */
1777+
CLI_SCOPE_MUTE, /* a live process holds the endpoint but did not answer in time */
1778+
} cli_scope_attempt_t;
1779+
1780+
typedef cli_scope_attempt_t (*cli_scope_attempt_fn)(void *context);
1781+
1782+
/* Repeat an IPC attempt while it reaches a live but mute endpoint, so a slow
1783+
* reply on a loaded runner is waited for instead of failing the test. A reply
1784+
* or a definite refusal ends it. Each attempt keeps the API's finite budget.
1785+
* Returns true when an attempt was answered. */
1786+
static bool cli_scope_until_answered(cli_scope_attempt_fn attempt, void *context) {
1787+
for (;;) {
1788+
cli_scope_attempt_t outcome = attempt(context);
1789+
if (outcome != CLI_SCOPE_MUTE) {
1790+
return outcome == CLI_SCOPE_ANSWERED;
17691791
}
1770-
cbm_usleep(2000);
17711792
}
17721793
}
17731794

1795+
/* One attempt to connect the parent's committed client to the host. */
1796+
static cli_scope_attempt_t cli_scope_connect_attempt(void *context) {
1797+
cli_scope_fixture_t *fixture = context;
1798+
cbm_daemon_runtime_connect_result_t result = {0};
1799+
fixture->client = cbm_daemon_runtime_client_connect(fixture->endpoint, &fixture->identity,
1800+
CLI_SCOPE_TIMEOUT_MS, &result);
1801+
if (fixture->client) {
1802+
return CLI_SCOPE_ANSWERED;
1803+
}
1804+
return result.status == CBM_DAEMON_RUNTIME_CONNECT_ERROR &&
1805+
result.muted_endpoint_holder_pid != 0
1806+
? CLI_SCOPE_MUTE
1807+
: CLI_SCOPE_REFUSED;
1808+
}
1809+
17741810
static bool cli_scope_fixture_start(cli_scope_fixture_t *fixture, const char *tag) {
17751811
memset(fixture, 0, sizeof(*fixture));
17761812
fixture->host = -1;
@@ -1842,6 +1878,7 @@ static bool cli_scope_fixture_start(cli_scope_fixture_t *fixture, const char *ta
18421878
cli_activation_save_env(&fixture->old_home, &fixture->old_cache);
18431879
const char *shell = getenv("SHELL");
18441880
fixture->old_shell = shell ? strdup(shell) : NULL;
1881+
fixture->env_saved = true;
18451882
cbm_setenv("SHELL", "/bin/zsh", 1);
18461883
if (child < 0) {
18471884
return cli_scope_setup_failed(fixture, tag, "forking the host", fork_error);
@@ -1854,11 +1891,9 @@ static bool cli_scope_fixture_start(cli_scope_fixture_t *fixture, const char *ta
18541891
0);
18551892
}
18561893
fixture->endpoint = cbm_daemon_bootstrap_endpoint_new(fixture->runtime_parent);
1857-
cbm_daemon_runtime_connect_result_t connect_result = {0};
1858-
fixture->client = fixture->endpoint
1859-
? cbm_daemon_runtime_client_connect(fixture->endpoint, &fixture->identity,
1860-
CLI_SCOPE_TIMEOUT_MS, &connect_result)
1861-
: NULL;
1894+
if (fixture->endpoint) {
1895+
(void)cli_scope_until_answered(cli_scope_connect_attempt, fixture);
1896+
}
18621897
if (!fixture->client) {
18631898
return cli_scope_setup_failed(fixture, tag,
18641899
fixture->endpoint ? "connecting the parent's client"
@@ -1876,15 +1911,33 @@ static bool cli_scope_host_serving_within(const cli_scope_fixture_t *fixture, ui
18761911
!status.stopping && status.committed_clients == 1;
18771912
}
18781913

1914+
typedef struct {
1915+
const cli_scope_fixture_t *fixture;
1916+
cbm_daemon_runtime_status_t status;
1917+
} cli_scope_status_probe_t;
1918+
1919+
/* One status request. A failed request still reports the PID of a live but
1920+
* mute endpoint holder, which is what tells slow apart from gone. */
1921+
static cli_scope_attempt_t cli_scope_status_attempt(void *context) {
1922+
cli_scope_status_probe_t *probe = context;
1923+
memset(&probe->status, 0, sizeof(probe->status));
1924+
if (cbm_daemon_runtime_request_status(probe->fixture->endpoint, &probe->fixture->identity,
1925+
CLI_SCOPE_TIMEOUT_MS, &probe->status)) {
1926+
return CLI_SCOPE_ANSWERED;
1927+
}
1928+
return probe->status.muted_endpoint_holder_pid != 0 ? CLI_SCOPE_MUTE : CLI_SCOPE_REFUSED;
1929+
}
1930+
18791931
/* Ask the host daemon itself: still running, not stopping, and the parent's
1880-
* committed client still admitted. */
1932+
* committed client still admitted. A foreign-namespace install leaves this
1933+
* daemon serving, so a slow reply on a loaded runner must not be misread as
1934+
* "drained" (the flaky failure this fixture showed): a mute attempt is
1935+
* repeated until the daemon answers, while a drained daemon is unreachable or
1936+
* reports stopping, which decides at once. */
18811937
static bool cli_scope_host_serving(const cli_scope_fixture_t *fixture) {
1882-
/* A generous, bounded status deadline: a foreign-namespace install leaves
1883-
* this daemon serving, so a slow response on a loaded runner must not be
1884-
* misread as "drained" (the flaky failure this fixture showed). The call
1885-
* still fails cleanly — a genuinely drained daemon is unreachable or
1886-
* reports stopping — it just no longer decides survival on a 5 s budget. */
1887-
return cli_scope_host_serving_within(fixture, CLI_SCOPE_HOST_SERVING_TIMEOUT_MS);
1938+
cli_scope_status_probe_t probe = {.fixture = fixture};
1939+
return fixture->endpoint && cli_scope_until_answered(cli_scope_status_attempt, &probe) &&
1940+
!probe.status.stopping && probe.status.committed_clients == 1;
18881941
}
18891942

18901943
static int cli_scope_install(cli_scope_fixture_t *fixture, const char *home, const char *cache,
@@ -1917,22 +1970,28 @@ static int cli_scope_fixture_finish(cli_scope_fixture_t *fixture) {
19171970
}
19181971
int host_exit = -1;
19191972
if (fixture->host > 0) {
1920-
host_exit = cli_scope_reap_host(fixture->host, CLI_SCOPE_HOST_REAP_TIMEOUT_MS);
1973+
host_exit = cli_scope_reap_host(fixture->host);
19211974
fixture->host = -1;
19221975
}
19231976
cbm_daemon_ipc_endpoint_free(fixture->endpoint);
19241977
fixture->endpoint = NULL;
19251978
if (fixture->previous_supervisor_build[0]) {
19261979
cbm_index_supervisor_set_build_fingerprint_for_test(fixture->previous_supervisor_build);
19271980
}
1928-
if (fixture->old_shell) {
1929-
cbm_setenv("SHELL", fixture->old_shell, 1);
1930-
} else {
1931-
cbm_unsetenv("SHELL");
1981+
/* Restore only what start saved: a setup that failed before saving left
1982+
* HOME, CBM_CACHE_DIR and SHELL untouched, and restoring "nothing saved"
1983+
* would unset them for every later test in the suite. */
1984+
if (fixture->env_saved) {
1985+
if (fixture->old_shell) {
1986+
cbm_setenv("SHELL", fixture->old_shell, 1);
1987+
} else {
1988+
cbm_unsetenv("SHELL");
1989+
}
1990+
cli_activation_restore_env(fixture->old_home, fixture->old_cache);
1991+
fixture->env_saved = false;
19321992
}
19331993
free(fixture->old_shell);
19341994
fixture->old_shell = NULL;
1935-
cli_activation_restore_env(fixture->old_home, fixture->old_cache);
19361995
fixture->old_home = NULL;
19371996
fixture->old_cache = NULL;
19381997
test_rmdir_r(fixture->tmpdir);
@@ -2080,6 +2139,71 @@ TEST(cli_scope_fixture_names_its_failing_setup_step) {
20802139
PASS();
20812140
}
20822141

2142+
/* Every cli_scope test calls finish even when start failed. A start that
2143+
* failed before saving HOME, CBM_CACHE_DIR and SHELL used to have finish
2144+
* "restore" them to nothing, unsetting them for every later test. */
2145+
TEST(cli_scope_fixture_finish_keeps_env_after_an_early_failure) {
2146+
char *saved_home = save_test_env("HOME");
2147+
char *saved_cache = save_test_env("CBM_CACHE_DIR");
2148+
char *saved_shell = save_test_env("SHELL");
2149+
cbm_setenv("HOME", "/tmp/cli-scope-sentinel-home", 1);
2150+
cbm_setenv("CBM_CACHE_DIR", "/tmp/cli-scope-sentinel-cache", 1);
2151+
cbm_setenv("SHELL", "/bin/sentinel-shell", 1);
2152+
cli_scope_fixture_t fixture;
2153+
bool ready = cli_scope_fixture_start(&fixture, "no-such-parent/y");
2154+
(void)cli_scope_fixture_finish(&fixture);
2155+
const char *home = getenv("HOME");
2156+
const char *cache = getenv("CBM_CACHE_DIR");
2157+
const char *shell = getenv("SHELL");
2158+
bool home_kept = home && strcmp(home, "/tmp/cli-scope-sentinel-home") == 0;
2159+
bool cache_kept = cache && strcmp(cache, "/tmp/cli-scope-sentinel-cache") == 0;
2160+
bool shell_kept = shell && strcmp(shell, "/bin/sentinel-shell") == 0;
2161+
restore_test_env("HOME", saved_home);
2162+
restore_test_env("CBM_CACHE_DIR", saved_cache);
2163+
restore_test_env("SHELL", saved_shell);
2164+
2165+
ASSERT_FALSE(ready);
2166+
ASSERT_TRUE(home_kept);
2167+
ASSERT_TRUE(cache_kept);
2168+
ASSERT_TRUE(shell_kept);
2169+
PASS();
2170+
}
2171+
2172+
/* A scripted sequence of attempt outcomes for cli_scope_until_answered. */
2173+
typedef struct {
2174+
const cli_scope_attempt_t *outcomes;
2175+
int count;
2176+
int calls;
2177+
} cli_scope_scripted_attempts_t;
2178+
2179+
static cli_scope_attempt_t cli_scope_scripted_attempt(void *context) {
2180+
cli_scope_scripted_attempts_t *script = context;
2181+
int index = script->calls < script->count ? script->calls : script->count - 1;
2182+
script->calls++;
2183+
return script->outcomes[index];
2184+
}
2185+
2186+
/* The rule that replaced the status and connect timeouts: a mute endpoint is
2187+
* asked again, and only a reply or a definite refusal decides. */
2188+
TEST(cli_scope_retries_a_mute_endpoint_until_it_answers) {
2189+
static const cli_scope_attempt_t slow_then_answer[] = {CLI_SCOPE_MUTE, CLI_SCOPE_MUTE,
2190+
CLI_SCOPE_MUTE, CLI_SCOPE_ANSWERED};
2191+
cli_scope_scripted_attempts_t slow = {slow_then_answer, 4, 0};
2192+
ASSERT_TRUE(cli_scope_until_answered(cli_scope_scripted_attempt, &slow));
2193+
ASSERT_EQ(slow.calls, 4);
2194+
2195+
static const cli_scope_attempt_t slow_then_refused[] = {CLI_SCOPE_MUTE, CLI_SCOPE_REFUSED};
2196+
cli_scope_scripted_attempts_t refused = {slow_then_refused, 2, 0};
2197+
ASSERT_FALSE(cli_scope_until_answered(cli_scope_scripted_attempt, &refused));
2198+
ASSERT_EQ(refused.calls, 2);
2199+
2200+
static const cli_scope_attempt_t answered_at_once[] = {CLI_SCOPE_ANSWERED};
2201+
cli_scope_scripted_attempts_t at_once = {answered_at_once, 1, 0};
2202+
ASSERT_TRUE(cli_scope_until_answered(cli_scope_scripted_attempt, &at_once));
2203+
ASSERT_EQ(at_once.calls, 1);
2204+
PASS();
2205+
}
2206+
20832207
/* cli_scope_install with the activation's stdout captured into out. */
20842208
static int cli_scope_install_captured(cli_scope_fixture_t *fixture, const char *home,
20852209
const char *cache, const char *bin_dir, bool skip_binary,
@@ -17940,6 +18064,8 @@ SUITE(cli) {
1794018064
RUN_TEST(cli_install_into_host_namespace_still_drains_host_cohort);
1794118065
RUN_TEST(cli_install_skip_binary_unchanged_in_host_namespace_quiesces_nothing);
1794218066
RUN_TEST(cli_scope_fixture_names_its_failing_setup_step);
18067+
RUN_TEST(cli_scope_fixture_finish_keeps_env_after_an_early_failure);
18068+
RUN_TEST(cli_scope_retries_a_mute_endpoint_until_it_answers);
1794318069
RUN_TEST(cli_install_foreign_home_unreadable_cohort_cache_keeps_host_daemon);
1794418070
RUN_TEST(cli_install_foreign_home_busy_cohort_keeps_host_daemon);
1794518071
#endif

0 commit comments

Comments
 (0)