Skip to content

Commit 3cf2a66

Browse files
halindromeclaude
andcommitted
fix(daemon): address QA round 1
- Separate loss from transient failure. Touch helpers now report OK / lost / transient (version_cohort: OK/UNSAFE/IO; ipc: 1/0/-1). Only a proven unlink or replacement stops the daemon. A refresh failure on a still-valid file (EROFS, EPERM, EMFILE on the marker reopen) logs daemon.coordination_touch_failed once, retries every tick, and logs daemon.coordination_touch_recovered when it clears. - Cohort and claim locks now prove the path still names the held inode (private_file_revalidate against the stored directory + name), not just st_nlink, so rename-away, hard-link+unlink and a replaced directory are caught like unlink. - The runtime service's self-owned participant guard is refreshed by the listener heartbeat too. - Tests: rename-away detection; cohort lease/claim touch (lease with only lifetime held is OK, each unlinked file reported independently); IPC listener touch now checks timestamps are actually refreshed and covers the participant guard and identity-marker replacement. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Signed-off-by: Shane McCarron <[email protected]>
1 parent 9e793df commit 3cf2a66

12 files changed

Lines changed: 323 additions & 123 deletions

‎src/daemon/host.c‎

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -879,26 +879,41 @@ typedef struct {
879879
cbm_daemon_ipc_participant_guard_t *participant_guard;
880880
} host_coordination_t;
881881

882-
/* Refresh every long-held runtime file and name the first one found lost, or
883-
* return NULL. A held lock whose path now names another inode coordinates
884-
* nothing: forked index workers read the daemon as uncoordinated and a peer
885-
* can start a second generation (#2178). */
886-
static const char *host_coordination_touch(const host_coordination_t *coordination,
887-
cbm_daemon_runtime_service_t *service) {
888-
if (!cbm_version_cohort_daemon_claim_touch(coordination->daemon_claim)) {
889-
return "cohort_daemon_claim";
890-
}
891-
if (!cbm_version_cohort_lease_touch(coordination->cohort_lease)) {
892-
return "cohort_lease";
893-
}
894-
if (!cbm_daemon_ipc_participant_guard_touch(coordination->endpoint,
895-
coordination->participant_guard)) {
896-
return "participant_guard";
897-
}
898-
if (!cbm_daemon_runtime_service_touch_listener(service)) {
899-
return "listener";
882+
typedef struct {
883+
const char *lost; /* first held file whose path no longer names it */
884+
const char *transient; /* first still-valid file that could not be refreshed */
885+
} host_touch_result_t;
886+
887+
static void host_touch_note(host_touch_result_t *result, const char *file, int status) {
888+
if (status == 0 && !result->lost) {
889+
result->lost = file;
890+
} else if (status < 0 && !result->transient) {
891+
result->transient = file;
900892
}
901-
return NULL;
893+
}
894+
895+
static int host_cohort_touch_status(cbm_version_cohort_status_t status) {
896+
return status == CBM_VERSION_COHORT_OK ? 1 : (status == CBM_VERSION_COHORT_IO ? -1 : 0);
897+
}
898+
899+
/* Refresh every long-held runtime file. A held lock whose path now names
900+
* another inode coordinates nothing: forked index workers read the daemon as
901+
* uncoordinated and a peer can start a second generation (#2178). Every file
902+
* is visited even after one fails so the rest keep being refreshed. */
903+
static host_touch_result_t host_coordination_touch(const host_coordination_t *coordination,
904+
cbm_daemon_runtime_service_t *service) {
905+
host_touch_result_t result = {0};
906+
host_touch_note(&result, "cohort_daemon_claim",
907+
host_cohort_touch_status(
908+
cbm_version_cohort_daemon_claim_touch(coordination->daemon_claim)));
909+
host_touch_note(
910+
&result, "cohort_lease",
911+
host_cohort_touch_status(cbm_version_cohort_lease_touch(coordination->cohort_lease)));
912+
host_touch_note(&result, "participant_guard",
913+
cbm_daemon_ipc_participant_guard_touch(coordination->endpoint,
914+
coordination->participant_guard));
915+
host_touch_note(&result, "listener", cbm_daemon_runtime_service_touch_listener(service));
916+
return result;
902917
}
903918

904919
static bool host_wait_for_lifetime(cbm_daemon_runtime_service_t *service,
@@ -907,6 +922,7 @@ static bool host_wait_for_lifetime(cbm_daemon_runtime_service_t *service,
907922
uint64_t initial_deadline = cbm_now_ms() + HOST_INITIAL_CLIENT_TIMEOUT_MS;
908923
uint64_t stopping_deadline = 0;
909924
uint64_t next_touch = 0;
925+
bool touch_degraded = false;
910926
for (;;) {
911927
cbm_daemon_runtime_service_state_t state = cbm_daemon_runtime_service_state(service);
912928
if (state == CBM_DAEMON_RUNTIME_SERVICE_EXITED) {
@@ -942,12 +958,23 @@ static bool host_wait_for_lifetime(cbm_daemon_runtime_service_t *service,
942958
* coordinated generation, while re-claiming would race that client. */
943959
if (cbm_now_ms() >= next_touch) {
944960
next_touch = cbm_now_ms() + HOST_COORDINATION_TOUCH_MS;
945-
const char *lost = host_coordination_touch(coordination, service);
946-
if (lost) {
961+
host_touch_result_t touched = host_coordination_touch(coordination, service);
962+
if (touched.lost) {
947963
cbm_log_warn("daemon.lifetime_end", "reason", "coordination_file_lost", "file",
948-
lost);
964+
touched.lost);
949965
return cbm_daemon_runtime_service_stop(service, HOST_RUNTIME_SHUTDOWN_MS);
950966
}
967+
/* A refresh failure on a still-valid file (read-only remount,
968+
* EMFILE) is not loss: keep serving, retry next tick, and log
969+
* only transitions so a persistent condition cannot flood. */
970+
if ((touched.transient != NULL) != touch_degraded) {
971+
touch_degraded = touched.transient != NULL;
972+
if (touch_degraded) {
973+
cbm_log_warn("daemon.coordination_touch_failed", "file", touched.transient);
974+
} else {
975+
cbm_log_info("daemon.coordination_touch_recovered");
976+
}
977+
}
951978
}
952979
host_http_reconcile_at(host, cbm_now_ms(), false);
953980
/* Retire an ephemeral generation that lingered for cold-storm cohort

‎src/daemon/ipc.c‎

Lines changed: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3220,44 +3220,65 @@ void cbm_daemon_ipc_listener_close(cbm_daemon_ipc_listener_t *listener) {
32203220
free(listener);
32213221
}
32223222

3223-
/* Touch through the held descriptor only after proving the path still names
3224-
* it: refreshing by path would keep a replacement inode fresh and hide the
3225-
* loss (#2178). */
3226-
static bool posix_held_file_touch(int directory_fd, const char *base_name, int fd) {
3227-
return private_regular_file_snapshot(directory_fd, base_name, fd, 1, NULL) &&
3228-
futimens(fd, NULL) == 0;
3223+
/* Touch results: 1 refreshed, 0 lost (the path no longer names the owned
3224+
* file), -1 transient (a still-valid file could not be refreshed). Touch
3225+
* through the held descriptor only after proving the path still names it:
3226+
* refreshing by path would keep a replacement inode fresh and hide the loss
3227+
* (#2178). */
3228+
static int posix_touch_fold(int result, int next) {
3229+
return result == 0 || next == 0 ? 0 : (result < 0 || next < 0 ? -1 : 1);
32293230
}
32303231

3231-
static bool posix_held_lock_touch(int directory_fd, const process_lock_entry_t *entry, int fd) {
3232-
return entry && posix_held_file_touch(directory_fd, entry->lock_name, fd);
3232+
static int posix_held_file_touch(int directory_fd, const char *base_name, int fd) {
3233+
if (!private_regular_file_snapshot(directory_fd, base_name, fd, 1, NULL)) {
3234+
return 0;
3235+
}
3236+
return futimens(fd, NULL) == 0 ? 1 : -1;
3237+
}
3238+
3239+
static int posix_held_lock_touch(int directory_fd, const process_lock_entry_t *entry, int fd) {
3240+
return entry ? posix_held_file_touch(directory_fd, entry->lock_name, fd) : 0;
3241+
}
3242+
3243+
static int posix_identity_marker_touch(const cbm_daemon_ipc_listener_t *listener) {
3244+
/* The marker is not held open; reopen it and require the published inode.
3245+
* Only ENOENT proves loss: EMFILE and friends say nothing about the file. */
3246+
int marker_fd = openat(listener->dir_fd, listener->socket_identity_name,
3247+
O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK);
3248+
if (marker_fd < 0) {
3249+
return errno == ENOENT ? 0 : -1;
3250+
}
3251+
struct stat marker_status;
3252+
int result = 0;
3253+
if (private_regular_file_snapshot(listener->dir_fd, listener->socket_identity_name, marker_fd,
3254+
1, &marker_status) &&
3255+
marker_status.st_dev == listener->identity_device &&
3256+
marker_status.st_ino == listener->identity_inode) {
3257+
result = futimens(marker_fd, NULL) == 0 ? 1 : -1;
3258+
}
3259+
(void)close(marker_fd);
3260+
return result;
32333261
}
32343262

3235-
bool cbm_daemon_ipc_listener_touch(cbm_daemon_ipc_listener_t *listener) {
3263+
int cbm_daemon_ipc_listener_touch(cbm_daemon_ipc_listener_t *listener,
3264+
cbm_daemon_ipc_participant_guard_t *external_guard) {
32363265
if (!listener || listener->dir_fd < 0 || listener->owner_pid != getpid() ||
32373266
!listener->lifetime_reservation) {
3238-
return false;
3267+
return 0;
32393268
}
3240-
bool lifetime =
3269+
int result =
32413270
posix_held_lock_touch(listener->dir_fd, listener->lifetime_reservation->process_entry,
32423271
listener->lifetime_reservation->fd);
3243-
const cbm_daemon_ipc_participant_guard_t *guard = listener->participant_guard;
3244-
bool participant =
3245-
!guard ||
3246-
posix_held_lock_touch(listener->dir_fd, guard->legacy_process_entry, guard->legacy_fd);
3247-
/* The marker is not held open; reopen it and require the published inode. */
3248-
int marker_fd = openat(listener->dir_fd, listener->socket_identity_name,
3249-
O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_NONBLOCK);
3250-
struct stat marker_status;
3251-
bool marker = marker_fd >= 0 &&
3252-
private_regular_file_snapshot(listener->dir_fd, listener->socket_identity_name,
3253-
marker_fd, 1, &marker_status) &&
3254-
marker_status.st_dev == listener->identity_device &&
3255-
marker_status.st_ino == listener->identity_inode &&
3256-
futimens(marker_fd, NULL) == 0;
3257-
if (marker_fd >= 0) {
3258-
(void)close(marker_fd);
3272+
const cbm_daemon_ipc_participant_guard_t *guards[] = {listener->participant_guard,
3273+
external_guard};
3274+
for (size_t i = 0; i < sizeof(guards) / sizeof(guards[0]); i++) {
3275+
if (guards[i]) {
3276+
result = posix_touch_fold(result, posix_held_lock_touch(listener->dir_fd,
3277+
guards[i]->legacy_process_entry,
3278+
guards[i]->legacy_fd));
3279+
}
32593280
}
3260-
return lifetime && participant && marker;
3281+
return posix_touch_fold(result, posix_identity_marker_touch(listener));
32613282
}
32623283

32633284
int cbm_daemon_ipc_accept(cbm_daemon_ipc_listener_t *listener, uint32_t timeout_ms,
@@ -3643,10 +3664,12 @@ bool cbm_daemon_ipc_participant_guard_release(cbm_daemon_ipc_participant_guard_t
36433664
return true;
36443665
}
36453666

3646-
bool cbm_daemon_ipc_participant_guard_touch(const cbm_daemon_ipc_endpoint_t *endpoint,
3647-
cbm_daemon_ipc_participant_guard_t *guard) {
3648-
return endpoint && guard && guard->owner_pid == getpid() &&
3649-
posix_held_lock_touch(endpoint->dir_fd, guard->legacy_process_entry, guard->legacy_fd);
3667+
int cbm_daemon_ipc_participant_guard_touch(const cbm_daemon_ipc_endpoint_t *endpoint,
3668+
cbm_daemon_ipc_participant_guard_t *guard) {
3669+
if (!endpoint || !guard || guard->owner_pid != getpid()) {
3670+
return 0;
3671+
}
3672+
return posix_held_lock_touch(endpoint->dir_fd, guard->legacy_process_entry, guard->legacy_fd);
36503673
}
36513674

36523675
int cbm_daemon_ipc_local_transition_try_acquire(
@@ -6794,13 +6817,15 @@ bool cbm_daemon_ipc_participant_guard_release(cbm_daemon_ipc_participant_guard_t
67946817

67956818
/* Windows has no age-based cleaner of the private runtime directory, and its
67966819
* held handles deny deletion; the #2178 heartbeat has nothing to refresh. */
6797-
bool cbm_daemon_ipc_listener_touch(cbm_daemon_ipc_listener_t *listener) {
6798-
return listener != NULL;
6820+
int cbm_daemon_ipc_listener_touch(cbm_daemon_ipc_listener_t *listener,
6821+
cbm_daemon_ipc_participant_guard_t *external_guard) {
6822+
(void)external_guard;
6823+
return listener ? 1 : 0;
67996824
}
68006825

6801-
bool cbm_daemon_ipc_participant_guard_touch(const cbm_daemon_ipc_endpoint_t *endpoint,
6802-
cbm_daemon_ipc_participant_guard_t *guard) {
6803-
return endpoint && guard;
6826+
int cbm_daemon_ipc_participant_guard_touch(const cbm_daemon_ipc_endpoint_t *endpoint,
6827+
cbm_daemon_ipc_participant_guard_t *guard) {
6828+
return endpoint && guard ? 1 : 0;
68046829
}
68056830

68066831
int cbm_daemon_ipc_local_transition_try_acquire(

‎src/daemon/ipc.h‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,17 @@ cbm_daemon_ipc_listener_t *cbm_daemon_ipc_listen_reserved(
7777
void cbm_daemon_ipc_listener_close(cbm_daemon_ipc_listener_t *listener);
7878

7979
/* Heartbeat for the daemon's long-lived runtime artifacts: refresh the held
80-
* lifetime reservation, the listener's participant guard (if it owns one) and
81-
* the published identity marker so an age-based temp cleaner never deletes
82-
* them (#2178). Returns false once any of them no longer names the file this
83-
* listener owns: a lost lifetime file lets a peer start a second generation,
84-
* and a lost identity marker makes listener close leave the socket pair
85-
* behind. Only the owning thread may call it while the listener is open. */
86-
bool cbm_daemon_ipc_listener_touch(cbm_daemon_ipc_listener_t *listener);
80+
* lifetime reservation, the listener's participant guard (if it owns one),
81+
* external_guard (optional, a guard the caller retains beside this listener)
82+
* and the published identity marker so an age-based temp cleaner never
83+
* deletes them (#2178). Returns 1 when all were refreshed, 0 once any no
84+
* longer names the file this listener owns (a lost lifetime file lets a peer
85+
* start a second generation; a lost identity marker makes listener close
86+
* leave the socket pair behind), and -1 when a still-valid file could not be
87+
* refreshed (transient). Only the thread that closes the listener may call it
88+
* while the listener is open. */
89+
int cbm_daemon_ipc_listener_touch(cbm_daemon_ipc_listener_t *listener,
90+
cbm_daemon_ipc_participant_guard_t *external_guard);
8791

8892
/* Create or validate one private current-user directory at an already
8993
* canonical local path. Ancestors are handle-validated without mutation:
@@ -279,10 +283,10 @@ bool cbm_daemon_ipc_startup_lock_release(cbm_daemon_ipc_startup_lock_t **lock_io
279283
int cbm_daemon_ipc_participant_guard_try_join(const cbm_daemon_ipc_endpoint_t *endpoint,
280284
cbm_daemon_ipc_participant_guard_t **guard_out);
281285
bool cbm_daemon_ipc_participant_guard_release(cbm_daemon_ipc_participant_guard_t **guard_io);
282-
/* Same heartbeat contract as cbm_daemon_ipc_listener_touch, for a guard the
283-
* caller retains itself. */
284-
bool cbm_daemon_ipc_participant_guard_touch(const cbm_daemon_ipc_endpoint_t *endpoint,
285-
cbm_daemon_ipc_participant_guard_t *guard);
286+
/* Same heartbeat contract and 1/0/-1 result as cbm_daemon_ipc_listener_touch,
287+
* for a guard the caller retains itself. */
288+
int cbm_daemon_ipc_participant_guard_touch(const cbm_daemon_ipc_endpoint_t *endpoint,
289+
cbm_daemon_ipc_participant_guard_t *guard);
286290

287291
/* Standalone CLI work joins the legacy-compatible current participant group
288292
* without becoming a daemon client. Acquisition retains startup-v2 only for

‎src/daemon/runtime.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2578,8 +2578,10 @@ size_t cbm_daemon_runtime_service_active_connections(cbm_daemon_runtime_service_
25782578
return count;
25792579
}
25802580

2581-
bool cbm_daemon_runtime_service_touch_listener(cbm_daemon_runtime_service_t *service) {
2582-
return service && service->listener && cbm_daemon_ipc_listener_touch(service->listener);
2581+
int cbm_daemon_runtime_service_touch_listener(cbm_daemon_runtime_service_t *service) {
2582+
return service && service->listener
2583+
? cbm_daemon_ipc_listener_touch(service->listener, service->owned_participant_guard)
2584+
: 0;
25832585
}
25842586

25852587
void cbm_daemon_runtime_service_reconcile_lifetime(cbm_daemon_runtime_service_t *service) {

‎src/daemon/runtime.h‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,12 @@ size_t cbm_daemon_runtime_service_active_connections(cbm_daemon_runtime_service_
336336
* an unbounded idle hang. The host lifetime loop calls this every tick; it is a
337337
* no-op unless a last-committed-client linger is armed. */
338338
void cbm_daemon_runtime_service_reconcile_lifetime(cbm_daemon_runtime_service_t *service);
339-
/* #2178 heartbeat over the listener's runtime artifacts; see
340-
* cbm_daemon_ipc_listener_touch. Call only from the thread that stops and
341-
* frees the service, which is the only thread that closes the listener. */
342-
bool cbm_daemon_runtime_service_touch_listener(cbm_daemon_runtime_service_t *service);
339+
/* #2178 heartbeat over the listener's runtime artifacts, plus the participant
340+
* guard the service owns when it started itself; see
341+
* cbm_daemon_ipc_listener_touch for the 1/0/-1 result. Call only from the
342+
* thread that stops and frees the service, which is the only thread that
343+
* closes the listener. */
344+
int cbm_daemon_runtime_service_touch_listener(cbm_daemon_runtime_service_t *service);
343345
size_t cbm_daemon_runtime_service_job_subscribers(cbm_daemon_runtime_service_t *service,
344346
const char *project_key);
345347
uint64_t cbm_daemon_runtime_service_client_process_id(cbm_daemon_runtime_service_t *service,

‎src/daemon/version_cohort.c‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -363,23 +363,35 @@ cbm_private_file_lock_status_t cbm_version_cohort_lease_release(
363363
return result;
364364
}
365365

366-
static bool version_cohort_lock_touch(cbm_private_file_lock_t *lock) {
367-
return !lock || cbm_private_file_lock_touch(lock) == CBM_PRIVATE_FILE_LOCK_OK;
366+
/* Folds one lock's touch into an accumulated result: a lost file (UNSAFE)
367+
* outranks a transient refresh failure (IO), which outranks OK. A lease holds
368+
* only lifetime after admission, so absent locks are skipped. */
369+
static cbm_version_cohort_status_t version_cohort_lock_touch(cbm_private_file_lock_t *lock,
370+
cbm_version_cohort_status_t result) {
371+
if (!lock || result == CBM_VERSION_COHORT_UNSAFE) {
372+
return result;
373+
}
374+
cbm_private_file_lock_status_t status = cbm_private_file_lock_touch(lock);
375+
if (status == CBM_PRIVATE_FILE_LOCK_UNSAFE) {
376+
return CBM_VERSION_COHORT_UNSAFE;
377+
}
378+
return status == CBM_PRIVATE_FILE_LOCK_OK ? result : CBM_VERSION_COHORT_IO;
368379
}
369380

370-
bool cbm_version_cohort_lease_touch(cbm_version_cohort_lease_t *lease) {
371-
if (!lease) {
372-
return false;
381+
cbm_version_cohort_status_t cbm_version_cohort_lease_touch(cbm_version_cohort_lease_t *lease) {
382+
if (!lease || !lease->lifetime) {
383+
return CBM_VERSION_COHORT_UNSAFE;
373384
}
374-
/* Evaluate all three so one lost file does not leave the others to age. */
375-
bool lifetime = version_cohort_lock_touch(lease->lifetime);
376-
bool admission = version_cohort_lock_touch(lease->admission);
377-
bool maintenance = version_cohort_lock_touch(lease->maintenance);
378-
return lifetime && admission && maintenance;
385+
cbm_version_cohort_status_t result =
386+
version_cohort_lock_touch(lease->lifetime, CBM_VERSION_COHORT_OK);
387+
result = version_cohort_lock_touch(lease->admission, result);
388+
return version_cohort_lock_touch(lease->maintenance, result);
379389
}
380390

381-
bool cbm_version_cohort_daemon_claim_touch(cbm_version_cohort_daemon_claim_t *claim) {
382-
return claim && claim->marker && version_cohort_lock_touch(claim->marker);
391+
cbm_version_cohort_status_t cbm_version_cohort_daemon_claim_touch(
392+
cbm_version_cohort_daemon_claim_t *claim) {
393+
return claim && claim->marker ? version_cohort_lock_touch(claim->marker, CBM_VERSION_COHORT_OK)
394+
: CBM_VERSION_COHORT_UNSAFE;
383395
}
384396

385397
static cbm_version_cohort_status_t version_cohort_failed(cbm_version_cohort_lease_t *lease,

0 commit comments

Comments
 (0)