Skip to content

Commit 91bee4f

Browse files
authored
Merge pull request DeusData#2113 from DeusData/fix/1828-enospc-daemon-start-diagnostics
fix(daemon): name errno/path on listen failure and fail the client fast instead of a 30s timeout (DeusData#1828)
2 parents 1b067e3 + ca86fdb commit 91bee4f

10 files changed

Lines changed: 996 additions & 20 deletions

File tree

‎src/daemon/bootstrap.c‎

Lines changed: 338 additions & 2 deletions
Large diffs are not rendered by default.

‎src/daemon/bootstrap.h‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,47 @@ cbm_daemon_bootstrap_probe_status_t cbm_daemon_bootstrap_classify_failed_connect
119119
typedef void *cbm_daemon_bootstrap_lock_t;
120120
typedef void *cbm_daemon_bootstrap_cohort_t;
121121

122+
/* Durable daemon start-failure record (#1828). A detached daemon has no
123+
* stderr and no channel back to the client that launched it; when its
124+
* listener publication fails (a full /tmp, for instance) the client used to
125+
* respawn it for the whole startup deadline and then report "active or
126+
* starting". The host now appends one record per failed start to
127+
* <cache>/logs/cbm-daemon.start-failures.log -- the same owner-only log
128+
* directory as cbm-daemon.log, deliberately NOT the runtime directory: that
129+
* one is exactly what a full /tmp cannot write, and a new artifact there
130+
* would entangle stale-generation cleanup. A waiting client reads the newest
131+
* record for its endpoint that is not older than its own spawn and fails
132+
* immediately with the cause. Diagnostic only: policy never reads it. */
133+
enum { CBM_DAEMON_BOOTSTRAP_COMPONENT_CAP = 32 };
134+
typedef struct {
135+
uint64_t recorded_at_s; /* wall clock, seconds */
136+
uint64_t pid;
137+
char component[CBM_DAEMON_BOOTSTRAP_COMPONENT_CAP]; /* host component that refused */
138+
char stage[CBM_DAEMON_IPC_LISTEN_FAILURE_STAGE_CAP]; /* IPC stage, "" when none */
139+
int errno_value; /* 0 when unknown */
140+
char path[CBM_DAEMON_IPC_LISTEN_FAILURE_PATH_CAP]; /* "" when none */
141+
} cbm_daemon_bootstrap_start_failure_t;
142+
143+
/* <cache>/logs, the owner-only directory that holds every daemon log. */
144+
bool cbm_daemon_bootstrap_log_directory(char *out, size_t capacity);
145+
/* Append one record for this process's most recent listener failure detail
146+
* (cbm_daemon_ipc_listen_failure_detail) under `component`. */
147+
bool cbm_daemon_bootstrap_start_failure_record(const char *log_directory,
148+
const cbm_daemon_ipc_endpoint_t *endpoint,
149+
const char *component);
150+
/* Newest record for `endpoint` recorded at or after not_before_s. Returns 1
151+
* and fills out_failure, 0 when none matches, -1 when the log directory or
152+
* file cannot be validated. */
153+
int cbm_daemon_bootstrap_start_failure_read(const char *log_directory,
154+
const cbm_daemon_ipc_endpoint_t *endpoint,
155+
uint64_t not_before_s,
156+
cbm_daemon_bootstrap_start_failure_t *out_failure);
157+
/* The client-facing sentence: "CBM daemon failed to start: <stage> failed
158+
* with ENOSPC (No space left on device) at <path>; see <log>". */
159+
void cbm_daemon_bootstrap_start_failure_format(const cbm_daemon_bootstrap_start_failure_t *failure,
160+
const char *log_directory, char *out,
161+
size_t capacity);
162+
122163
/* Injectable OS/runtime boundary used by the deterministic unit contract.
123164
* Production callers use cbm_daemon_bootstrap_execute(), whose built-in
124165
* operations delegate to daemon IPC/runtime and write visible diagnostics to
@@ -149,13 +190,30 @@ typedef struct {
149190
bool (*startup_lock_release)(void *context, cbm_daemon_bootstrap_lock_t *lock_io);
150191
bool (*spawn_daemon)(void *context, const cbm_daemon_bootstrap_launch_spec_t *spec);
151192
void (*visible_diagnostic)(void *context, const char *message);
193+
/* Optional. After this attempt spawned a daemon, report a start-failure
194+
* record for the endpoint not older than not_before_s (1 found, 0 none,
195+
* -1 unreadable). A found record ends the wait immediately (#1828). */
196+
int (*start_failure_probe)(void *context, const cbm_daemon_ipc_endpoint_t *endpoint,
197+
uint64_t not_before_s,
198+
cbm_daemon_bootstrap_start_failure_t *out_failure);
152199
} cbm_daemon_bootstrap_ops_t;
153200

154201
cbm_daemon_bootstrap_status_t cbm_daemon_bootstrap_execute(
155202
const cbm_daemon_bootstrap_config_t *config, cbm_daemon_bootstrap_result_t *result_out);
156203

157204
/* Test seam for the same state machine. All callbacks are synchronous and
158205
* borrowed for the duration of the call. */
206+
#ifdef CBM_ENABLE_TEST_SEAMS
207+
/* Replace ONLY the production spawn while every other production operation
208+
* (cohort, probe, startup lock, handoff) stays real. A test forks a real
209+
* daemon host in place of exec'ing the product binary, so the complete
210+
* client/daemon rendezvous runs in one deterministic process tree (#1828). */
211+
typedef bool (*cbm_daemon_bootstrap_spawn_fn)(void *context,
212+
const cbm_daemon_bootstrap_launch_spec_t *spec);
213+
void cbm_daemon_bootstrap_spawn_override_set_for_test(cbm_daemon_bootstrap_spawn_fn spawn,
214+
void *context);
215+
#endif
216+
159217
cbm_daemon_bootstrap_status_t cbm_daemon_bootstrap_execute_with_ops(
160218
const cbm_daemon_bootstrap_config_t *config, const cbm_daemon_bootstrap_ops_t *ops,
161219
cbm_daemon_bootstrap_result_t *result_out);

‎src/daemon/host.c‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "daemon/host_internal.h"
66

77
#include "daemon/application.h"
8+
#include "daemon/bootstrap.h"
89
#include "daemon/runtime.h"
910
#include "daemon/project_lock.h"
1011
#include "daemon/version_cohort.h"
@@ -146,6 +147,18 @@ static bool host_log_open(char conflict_log_out[HOST_PATH_CAP]) {
146147
return true;
147148
}
148149

150+
/* #1828: a detached daemon has no stderr; its start failure must reach the
151+
* client that is waiting for it, or that client burns its whole deadline and
152+
* reports the opposite of the truth. */
153+
static void host_start_failure_record(const cbm_daemon_ipc_endpoint_t *endpoint,
154+
const char *component) {
155+
char logs[HOST_PATH_CAP];
156+
if (!cbm_daemon_bootstrap_log_directory(logs, sizeof(logs)) ||
157+
!cbm_daemon_bootstrap_start_failure_record(logs, endpoint, component)) {
158+
cbm_log_error("daemon.start_failure_record_failed", "component", component);
159+
}
160+
}
161+
149162
static void host_log_close(void) {
150163
cbm_log_set_sink(NULL);
151164
if (g_host_log_mutex_initialized) {
@@ -1055,10 +1068,16 @@ int cbm_daemon_host_run(const cbm_daemon_host_config_t *config) {
10551068
};
10561069
cbm_daemon_runtime_service_t *service =
10571070
cbm_daemon_runtime_service_start_reserved(&runtime_config, &lifetime_reservation);
1071+
if (!service) {
1072+
cbm_log_error("daemon.start_failed", "component", "runtime");
1073+
/* Recorded while the lifetime reservation is still held: a client
1074+
* that watches this generation vanish finds the cause already there
1075+
* and never launches a doomed replacement. */
1076+
host_start_failure_record(config->endpoint, "runtime");
1077+
}
10581078
cbm_daemon_ipc_lifetime_reservation_release(lifetime_reservation);
10591079
lifetime_reservation = NULL;
10601080
if (!service) {
1061-
cbm_log_error("daemon.start_failed", "component", "runtime");
10621081
host_state_free(&host);
10631082
host_log_close();
10641083
host_participant_guard_close(&participant_guard);

0 commit comments

Comments
 (0)