Skip to content

Commit 760c08f

Browse files
authored
Merge pull request #1894 from mvanhorn/fix/1760-stale-rendezvous-recovery
fix: recover markerless stale rendezvous sockets during activation
2 parents 5e5405c + 4d3d06a commit 760c08f

4 files changed

Lines changed: 344 additions & 28 deletions

File tree

‎src/daemon/ipc.c‎

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,26 @@ static bool posix_directory_sync(int dir_fd) {
19831983
return unsupported;
19841984
}
19851985

1986+
static bool posix_socket_link_pair_unlink_if_matches(const cbm_daemon_ipc_endpoint_t *endpoint,
1987+
const posix_socket_identity_t *identity) {
1988+
posix_socket_identity_t stable = {0};
1989+
posix_socket_identity_t anchor = {0};
1990+
struct stat stable_status = {0};
1991+
struct stat anchor_status = {0};
1992+
return endpoint && identity &&
1993+
posix_socket_path_identity_read(endpoint, endpoint->socket_name, &stable,
1994+
&stable_status) == 1 &&
1995+
posix_socket_path_identity_read(endpoint, endpoint->socket_anchor_name, &anchor,
1996+
&anchor_status) == 1 &&
1997+
stable_status.st_nlink == 2 && anchor_status.st_nlink == 2 &&
1998+
posix_socket_identity_equal(&stable, identity) &&
1999+
posix_socket_identity_equal(&anchor, identity) &&
2000+
posix_socket_path_unlink_inode_if_matches(endpoint->dir_fd, endpoint->socket_name,
2001+
identity, 2) &&
2002+
posix_socket_path_unlink_inode_if_matches(endpoint->dir_fd, endpoint->socket_anchor_name,
2003+
identity, 1);
2004+
}
2005+
19862006
static bool posix_socket_record_temp_name(const char *record_name, char temp_name[NAME_MAX + 1]) {
19872007
if (!record_name || !temp_name) {
19882008
return false;
@@ -2366,23 +2386,7 @@ static int posix_stale_generation_cleanup_locked(const cbm_daemon_ipc_endpoint_t
23662386
result = -1;
23672387
goto cleanup_done;
23682388
}
2369-
posix_socket_identity_t confirmed_stable = {0};
2370-
posix_socket_identity_t confirmed_anchor = {0};
2371-
struct stat confirmed_stable_status = {0};
2372-
struct stat confirmed_anchor_status = {0};
2373-
bool confirmed =
2374-
posix_socket_path_identity_read(endpoint, endpoint->socket_name, &confirmed_stable,
2375-
&confirmed_stable_status) == 1 &&
2376-
posix_socket_path_identity_read(endpoint, endpoint->socket_anchor_name,
2377-
&confirmed_anchor, &confirmed_anchor_status) == 1 &&
2378-
confirmed_stable_status.st_nlink == 2 && confirmed_anchor_status.st_nlink == 2 &&
2379-
posix_socket_identity_equal(&confirmed_stable, &marker.identity) &&
2380-
posix_socket_identity_equal(&confirmed_anchor, &marker.identity);
2381-
if (!confirmed ||
2382-
!posix_socket_path_unlink_inode_if_matches(endpoint->dir_fd, endpoint->socket_name,
2383-
&marker.identity, 2) ||
2384-
!posix_socket_path_unlink_inode_if_matches(
2385-
endpoint->dir_fd, endpoint->socket_anchor_name, &marker.identity, 1)) {
2389+
if (!posix_socket_link_pair_unlink_if_matches(endpoint, &marker.identity)) {
23862390
result = -1;
23872391
goto cleanup_done;
23882392
}
@@ -2441,10 +2445,33 @@ static int posix_stale_generation_cleanup_locked(const cbm_daemon_ipc_endpoint_t
24412445
goto cleanup_done;
24422446
}
24432447

2448+
/* A hard kill can land after the stable link is durable but before either
2449+
* publication record exists. With startup serialized and lifetime
2450+
* reserved above, the exact owner-private two-name/two-link shape is
2451+
* sufficient authority: no unrelated socket can acquire the deterministic
2452+
* anchor name without being the same inode. Re-read both names immediately
2453+
* before inode-matched unlinking so replacement or link-count races fail
2454+
* closed. */
2455+
if (stable_state == 1 && anchor_state == 1 && stable_status.st_nlink == 2 &&
2456+
anchor_status.st_nlink == 2 &&
2457+
posix_socket_identity_equal(&stable_identity, &anchor_identity)) {
2458+
if (!posix_socket_link_pair_unlink_if_matches(endpoint, &stable_identity) ||
2459+
!posix_directory_sync(endpoint->dir_fd)) {
2460+
result = -1;
2461+
goto cleanup_done;
2462+
}
2463+
result = 1;
2464+
goto cleanup_done;
2465+
}
2466+
24442467
/* The deterministic generation-local anchor lets us collect the sole
24452468
* otherwise-untrackable crash boundary: bind/listen completed but the
2446-
* pending record was not yet durable. It never grants authority over the
2447-
* public stable path. */
2469+
* pending record was not yet durable. It never grants authority over a
2470+
* differing stable socket, so preserve both names when one is present. */
2471+
if (anchor_state == 1 && stable_state == 1) {
2472+
result = 0;
2473+
goto cleanup_done;
2474+
}
24482475
if (anchor_state == 1) {
24492476
if (anchor_status.st_nlink != 1 ||
24502477
!posix_socket_path_unlink_inode_if_matches(
@@ -3141,11 +3168,11 @@ int cbm_daemon_ipc_generation_probe_under_startup_lock(
31413168
if (!posix_startup_lock_matches_endpoint(endpoint, startup_lock) || startup_lock->prepared) {
31423169
return -1;
31433170
}
3144-
int lifetime = cbm_daemon_ipc_lifetime_reservation_probe(endpoint);
3145-
if (lifetime != 0) {
3146-
return lifetime;
3171+
int cleanup = cbm_daemon_ipc_stale_generation_cleanup(endpoint, startup_lock);
3172+
if (cleanup == 1) {
3173+
return 0;
31473174
}
3148-
return cbm_daemon_ipc_endpoint_probe(endpoint, 0);
3175+
return cleanup == 0 ? 1 : -1;
31493176
}
31503177

31513178
bool cbm_daemon_ipc_startup_lock_prepare_handoff(cbm_daemon_ipc_startup_lock_t *lock) {

‎src/daemon/ipc.h‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,11 @@ int cbm_daemon_ipc_lifetime_reservation_probe(const cbm_daemon_ipc_endpoint_t *e
131131
/* Remove only a provably current-generation stale Unix socket identity. The
132132
* caller must first observe an absent lifetime reservation and retain the
133133
* matching startup lock for the complete call; the implementation rechecks
134-
* both conditions. Stable deletion requires a committed marker whose named
135-
* anchor and stable path are the same secure socket inode; pending alone may
136-
* only complete that commit when both paths independently corroborate it.
134+
* both conditions. Stable deletion normally requires a committed marker whose
135+
* named anchor and stable path are the same secure socket inode; pending alone
136+
* may only complete that commit when both paths independently corroborate it.
137+
* With both records absent, the exact owner-private two-name/two-link stable
138+
* socket and anchor shape is also recoverable after immediate revalidation.
137139
* A differing stable replacement is preserved while owned anchor/records are
138140
* collected. Returns 1 when the stable endpoint is absent and all owned
139141
* artifacts are absent or were removed, 0 when cleanup is refused for a live,
@@ -193,8 +195,10 @@ int cbm_daemon_ipc_startup_lock_try_acquire(const cbm_daemon_ipc_endpoint_t *end
193195
cbm_daemon_ipc_startup_lock_t **lock_out);
194196
/* Activation-only no-spawn generation probe under the exact matching,
195197
* retained, unprepared startup lock. It ignores the caller's own startup-v2
196-
* and frozen-legacy startup claims, and reports only a live daemon lifetime,
197-
* stable current transport, or deterministic legacy/current sentinel.
198+
* and frozen-legacy startup claims, serially cleans a provable stale POSIX
199+
* generation under a temporary lifetime reservation, and reports only a live
200+
* daemon lifetime, remaining stable transport, or deterministic
201+
* legacy/current sentinel.
198202
* Returns 1 when active, 0 when authoritatively absent, and -1 when the lock,
199203
* endpoint, transport, or ownership cannot be validated. */
200204
int cbm_daemon_ipc_generation_probe_under_startup_lock(

‎tests/test_cli.c‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include <sys/stat.h>
3737
#include <unistd.h>
3838
#ifndef _WIN32
39+
#include <sys/socket.h>
40+
#include <sys/un.h>
3941
#include <sys/wait.h>
4042
#endif
4143
#ifdef __APPLE__
@@ -1341,6 +1343,122 @@ TEST(cli_activation_quiesce_does_not_wait_on_bootstrap_startup) {
13411343
ASSERT_TRUE(event_order);
13421344
PASS();
13431345
}
1346+
1347+
TEST(cli_install_recovers_markerless_stale_rendezvous) {
1348+
char tmpdir[256];
1349+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-markerless-rendezvous-XXXXXX");
1350+
if (!cbm_mkdtemp(tmpdir)) {
1351+
FAIL("cbm_mkdtemp failed");
1352+
}
1353+
char runtime_parent[512];
1354+
char cache_dir[512];
1355+
char install_dir[512];
1356+
char target_path[640];
1357+
char activation_log[640];
1358+
snprintf(runtime_parent, sizeof(runtime_parent), "%s/runtime", tmpdir);
1359+
snprintf(cache_dir, sizeof(cache_dir), "%s/cache", tmpdir);
1360+
snprintf(install_dir, sizeof(install_dir), "%s/custom/bin", tmpdir);
1361+
snprintf(target_path, sizeof(target_path), "%s/codebase-memory-mcp", install_dir);
1362+
snprintf(activation_log, sizeof(activation_log), "%s/logs/activation-events.ndjson", cache_dir);
1363+
if (test_mkdirp(runtime_parent) != 0) {
1364+
test_rmdir_r(tmpdir);
1365+
FAIL("runtime parent setup failed");
1366+
}
1367+
1368+
char *old_home = NULL;
1369+
char *old_cache = NULL;
1370+
cli_activation_save_env(&old_home, &old_cache);
1371+
const char *shell = getenv("SHELL");
1372+
char *old_shell = shell ? strdup(shell) : NULL;
1373+
cbm_setenv("HOME", tmpdir, 1);
1374+
cbm_setenv("SHELL", "/bin/zsh", 1);
1375+
cbm_setenv("CBM_CACHE_DIR", cache_dir, 1);
1376+
1377+
cbm_daemon_ipc_endpoint_t *endpoint = cbm_daemon_bootstrap_endpoint_new(runtime_parent);
1378+
const char *socket_path = endpoint ? cbm_daemon_ipc_endpoint_address(endpoint) : NULL;
1379+
char anchor_path[1024] = {0};
1380+
size_t socket_length = socket_path ? strlen(socket_path) : 0;
1381+
bool anchor_path_ok =
1382+
socket_length > 5 && strcmp(socket_path + socket_length - 5, ".sock") == 0;
1383+
if (anchor_path_ok) {
1384+
int written = snprintf(anchor_path, sizeof(anchor_path), "%.*s.anc",
1385+
(int)(socket_length - 5), socket_path);
1386+
anchor_path_ok = written > 0 && written < (int)sizeof(anchor_path);
1387+
}
1388+
struct sockaddr_un address;
1389+
memset(&address, 0, sizeof(address));
1390+
address.sun_family = AF_UNIX;
1391+
size_t address_length = socket_path ? strlen(socket_path) : 0;
1392+
bool address_ok = address_length > 0 && address_length < sizeof(address.sun_path);
1393+
if (address_ok) {
1394+
memcpy(address.sun_path, socket_path, address_length + 1);
1395+
}
1396+
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
1397+
address.sun_len = (uint8_t)(offsetof(struct sockaddr_un, sun_path) + address_length + 1);
1398+
#endif
1399+
socklen_t sockaddr_length =
1400+
(socklen_t)(offsetof(struct sockaddr_un, sun_path) + address_length + 1);
1401+
int raw_listener = address_ok && anchor_path_ok ? socket(AF_UNIX, SOCK_STREAM, 0) : -1;
1402+
struct stat socket_status = {0};
1403+
struct stat anchor_status = {0};
1404+
bool orphan_created =
1405+
raw_listener >= 0 &&
1406+
bind(raw_listener, (const struct sockaddr *)&address, sockaddr_length) == 0 &&
1407+
chmod(socket_path, 0600) == 0 && listen(raw_listener, 1) == 0 &&
1408+
link(socket_path, anchor_path) == 0 && lstat(socket_path, &socket_status) == 0 &&
1409+
lstat(anchor_path, &anchor_status) == 0 && S_ISSOCK(socket_status.st_mode) &&
1410+
S_ISSOCK(anchor_status.st_mode) && socket_status.st_nlink == 2 &&
1411+
anchor_status.st_nlink == 2 && socket_status.st_dev == anchor_status.st_dev &&
1412+
socket_status.st_ino == anchor_status.st_ino;
1413+
if (raw_listener >= 0) {
1414+
(void)close(raw_listener);
1415+
}
1416+
1417+
cbm_cli_set_activation_runtime_parent_for_test(runtime_parent);
1418+
char dir_arg[640];
1419+
snprintf(dir_arg, sizeof(dir_arg), "--dir=%s", install_dir);
1420+
char *install_argv[] = {"--force", "--skip-config", "--yes", dir_arg};
1421+
int install_rc = orphan_created ? cli_test_cmd_install(4, install_argv) : -1;
1422+
cbm_cli_set_activation_runtime_parent_for_test(g_cli_suite_runtime_parent);
1423+
cbm_set_auto_answer_for_test(0);
1424+
1425+
struct stat target_status = {0};
1426+
struct stat log_status = {0};
1427+
struct stat absent_status = {0};
1428+
bool target_exists = stat(target_path, &target_status) == 0;
1429+
bool log_private = stat(activation_log, &log_status) == 0 && (log_status.st_mode & 0077) == 0;
1430+
const char *events = read_test_file(activation_log);
1431+
const char *requested = events ? strstr(events, "\"phase\":\"requested\"") : NULL;
1432+
const char *stopped = events ? strstr(events, "\"phase\":\"daemon_stopped\"") : NULL;
1433+
const char *completed = events ? strstr(events, "\"phase\":\"completed\"") : NULL;
1434+
bool event_order =
1435+
requested && stopped && completed && requested < stopped && stopped < completed;
1436+
errno = 0;
1437+
bool socket_removed = socket_path && lstat(socket_path, &absent_status) != 0 && errno == ENOENT;
1438+
errno = 0;
1439+
bool anchor_removed = lstat(anchor_path, &absent_status) != 0 && errno == ENOENT;
1440+
1441+
if (old_shell) {
1442+
cbm_setenv("SHELL", old_shell, 1);
1443+
} else {
1444+
cbm_unsetenv("SHELL");
1445+
}
1446+
free(old_shell);
1447+
cli_activation_restore_env(old_home, old_cache);
1448+
cbm_daemon_ipc_endpoint_free(endpoint);
1449+
test_rmdir_r(tmpdir);
1450+
1451+
ASSERT_TRUE(anchor_path_ok);
1452+
ASSERT_TRUE(address_ok);
1453+
ASSERT_TRUE(orphan_created);
1454+
ASSERT_EQ(install_rc, 0);
1455+
ASSERT_TRUE(target_exists);
1456+
ASSERT_TRUE(log_private);
1457+
ASSERT_TRUE(event_order);
1458+
ASSERT_TRUE(socket_removed);
1459+
ASSERT_TRUE(anchor_removed);
1460+
PASS();
1461+
}
13441462
#endif
13451463

13461464
TEST(cli_install_force_quiesces_active_cohort_before_replacing_binary) {
@@ -14401,6 +14519,7 @@ SUITE(cli) {
1440114519
#ifndef _WIN32
1440214520
RUN_TEST(cli_activation_cleanup_failure_fail_stops_before_lease_release);
1440314521
RUN_TEST(cli_activation_quiesce_does_not_wait_on_bootstrap_startup);
14522+
RUN_TEST(cli_install_recovers_markerless_stale_rendezvous);
1440414523
#endif
1440514524
RUN_TEST(cli_install_force_quiesces_active_cohort_before_replacing_binary);
1440614525
RUN_TEST(cli_install_dir_and_skip_config_stage_first_install_safely);

0 commit comments

Comments
 (0)