Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(pipeline): sweep the orphaned stages of a dead writer before each run

Martin Vogelb6458f924da7
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.c
index 59ff88837..3d600034e 100644
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@ -2741,6 +2741,190 @@ static int export_after_publish(cbm_pipeline_t *p, const char *final_path) {
return 0;
}
+/* ── Orphan sweep (#1839) ────────────────────────────────────────
+ *
+ * Nothing on the worker-death path ever cleaned a stage up, so the sweep
+ * runs at the start of every run, before this run mints its own stage. It
+ * considers ONLY names of the exact minted shape for THIS database --
+ * "<basename>.stage.<6 alphanumerics>" plus that stage's -wal/-shm/-journal
+ * and .lock sidecars -- never the live database, a quarantined .corrupt, or
+ * another project's files. A stage is removed when its ownership lock can be
+ * taken (its writer is dead, or the stage predates ownership) and kept when
+ * a live writer holds the lock. The pre-ownership case is the one honest
+ * gap: a stage an OLDER binary is still writing against this database has
+ * no lock and is swept; that writer's final rename then fails and it
+ * discards. The live database is never named here on either path. */
+
+static const char *const cbm_stage_sidecar_tails[] = {"", "-wal", "-shm", "-journal", ".lock"};
+
+/* If `name` is "<base>.stage.<6 alphanumerics><known tail>", return the
+ * length of the stage name proper (without the tail); 0 otherwise. */
+static size_t stage_entry_stage_length(const char *name, const char *base, size_t base_len) {
+ if (strncmp(name, base, base_len) != 0) {
+ return 0;
+ }
+ const char *at = name + base_len;
+ if (strncmp(at, cbm_stage_marker, sizeof(cbm_stage_marker) - 1) != 0) {
+ return 0;
+ }
+ at += sizeof(cbm_stage_marker) - 1;
+ for (int i = 0; i < CBM_STAGE_SUFFIX_RANDOM_CHARS; i++) {
+ /* NUL is not alphanumeric, so a short name fails here too. */
+ if (!isalnum((unsigned char)at[i])) {
+ return 0;
+ }
+ }
+ at += CBM_STAGE_SUFFIX_RANDOM_CHARS;
+ for (size_t i = 0; i < sizeof(cbm_stage_sidecar_tails) / sizeof(cbm_stage_sidecar_tails[0]);
+ i++) {
+ if (strcmp(at, cbm_stage_sidecar_tails[i]) == 0) {
+ return (size_t)(at - name);
+ }
+ }
+ return 0;
+}
+
+typedef struct {
+ char **names;
+ int count;
+ int cap;
+} stage_name_list_t;
+
+/* Add a stage name once, however many of its files were listed. */
+static void stage_name_list_add(stage_name_list_t *list, const char *name, size_t len) {
+ for (int i = 0; i < list->count; i++) {
+ if (strlen(list->names[i]) == len && strncmp(list->names[i], name, len) == 0) {
+ return;
+ }
+ }
+ if (list->count == list->cap) {
+ int cap = list->cap ? list->cap * 2 : 8;
+ char **grown = (char **)realloc(list->names, (size_t)cap * sizeof(*grown));
+ if (!grown) {
+ return;
+ }
+ list->names = grown;
+ list->cap = cap;
+ }
+ char *copy = (char *)malloc(len + 1);
+ if (!copy) {
+ return;
+ }
+ memcpy(copy, name, len);
+ copy[len] = '\0';
+ list->names[list->count++] = copy;
+}
+
+static int64_t stage_bytes_on_disk(const char *stage_path) {
+ static const char *const files[] = {"", "-wal", "-shm", "-journal"};
+ int64_t total = 0;
+ char side[CBM_SZ_4K];
+ for (size_t i = 0; i < sizeof(files) / sizeof(files[0]); i++) {
+ int n = snprintf(side, sizeof(side), "%s%s", stage_path, files[i]);
+ if (n <= 0 || (size_t)n >= sizeof(side)) {
+ continue;
+ }
+ cbm_path_info_t info;
+ if (cbm_path_info_utf8(side, &info) == CBM_PATH_INFO_OK && info.is_regular) {
+ total += info.size;
+ }
+ }
+ return total;
+}
+
+static void sweep_one_stage(const char *stage_path) {
+ char *sidecar = stage_lock_sidecar_path(stage_path);
+ if (!sidecar) {
+ return;
+ }
+ errno = 0;
+ int lock_fd = cbm_lockfile_open(sidecar, false);
+ int probe_errno = errno;
+ free(sidecar);
+ if (lock_fd < 0 && probe_errno != ENOENT) {
+ bool live = probe_errno == EAGAIN || probe_errno == EACCES;
+#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
+ live = live || probe_errno == EWOULDBLOCK;
+#endif
+ if (live) {
+ cbm_log_info("pipeline.stage", "action", "orphan_kept", "reason", "live_writer", "path",
+ stage_path);
+ return;
+ }
+ char errno_text[16];
+ (void)snprintf(errno_text, sizeof(errno_text), "%d", probe_errno);
+ cbm_log_warn("pipeline.stage", "action", "orphan_kept", "reason", "lock_probe_failed",
+ "errno", errno_text, "path", stage_path);
+ return;
+ }
+ /* No owner: absent sidecar (pre-ownership stage, or dropped) or a lock
+ * the kernel released with its writer. Ours now, from the lock down. */
+ int64_t bytes = stage_bytes_on_disk(stage_path);
+ remove_stage_files(stage_path);
+ if (lock_fd >= 0) {
+ cbm_pipeline_stage_lock_drop(stage_path, lock_fd);
+ }
+ char bytes_text[32];
+ (void)snprintf(bytes_text, sizeof(bytes_text), "%lld", (long long)bytes);
+ cbm_log_info("pipeline.stage", "action", "orphan_removed", "bytes", bytes_text, "path",
+ stage_path);
+}
+
+static void sweep_orphan_stages(const char *final_path) {
+ /* Directory part INCLUDING its trailing separator, so the stage paths
+ * are joined exactly as the final path was spelled. */
+ size_t prefix_len = 0;
+ for (const char *c = final_path; *c; c++) {
+ if (*c == '/'
+#ifdef _WIN32
+ || *c == '\\'
+#endif
+ ) {
+ prefix_len = (size_t)(c - final_path) + 1;
+ }
+ }
+ const char *base = final_path + prefix_len;
+ size_t base_len = strlen(base);
+ if (base_len == 0) {
+ return;
+ }
+ char *dir_path = prefix_len ? (char *)malloc(prefix_len + 1) : strdup(".");
+ if (!dir_path) {
+ return;
+ }
+ if (prefix_len) {
+ memcpy(dir_path, final_path, prefix_len);
+ dir_path[prefix_len] = '\0';
+ }
+ cbm_dir_t *dir = cbm_opendir(dir_path);
+ if (!dir) {
+ free(dir_path);
+ return;
+ }
+ stage_name_list_t list = {0};
+ cbm_dirent_t *entry;
+ while ((entry = cbm_readdir(dir)) != NULL) {
+ size_t stage_len = stage_entry_stage_length(entry->name, base, base_len);
+ if (stage_len) {
+ stage_name_list_add(&list, entry->name, stage_len);
+ }
+ }
+ cbm_closedir(dir);
+ for (int i = 0; i < list.count; i++) {
+ size_t name_len = strlen(list.names[i]);
+ char *stage_path = (char *)malloc(prefix_len + name_len + 1);
+ if (stage_path) {
+ memcpy(stage_path, final_path, prefix_len);
+ memcpy(stage_path + prefix_len, list.names[i], name_len + 1);
+ sweep_one_stage(stage_path);
+ free(stage_path);
+ }
+ free(list.names[i]);
+ }
+ free(list.names);
+ free(dir_path);
+}
+
int cbm_pipeline_run(cbm_pipeline_t *p) {
if (!p) {
return CBM_NOT_FOUND;
@@ -2752,6 +2936,7 @@ int cbm_pipeline_run(cbm_pipeline_t *p) {
}
struct stat final_st;
bool final_existed = stat(final_path, &final_st) == 0;
+ sweep_orphan_stages(final_path);
char *staging_path = create_staging_path(final_path);
if (!staging_path) {
free(final_path);
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index 96857efb8..9ee54b41b 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -3549,6 +3549,164 @@ TEST(pipeline_minted_stage_is_owned_until_released) {
PASS();
}
+static bool file_has_content(const char *path, const char *expected) {
+ FILE *f = cbm_fopen(path, "rb");
+ if (!f) {
+ return false;
+ }
+ char buf[256] = {0};
+ size_t n = fread(buf, 1, sizeof(buf) - 1, f);
+ (void)fclose(f);
+ return n == strlen(expected) && memcmp(buf, expected, n) == 0;
+}
+
+/* #1839 / #1864: a stage a dead writer left beside a VALID database is swept
+ * by the next run and never influences its route. The 0-byte shape is what a
+ * worker killed before its backup wrote a page leaves behind; a stale -shm
+ * beside it is swept with it. */
+TEST(pipeline_stale_zero_byte_stage_beside_valid_db_routes_incremental_and_is_swept) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_stale_stage_XXXXXX");
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+ write_temp_file(tmp, "generation.py", "def StableGeneration():\n return 1\n");
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/generation.db", tmp);
+
+ cbm_pipeline_incremental_test_reset_faults();
+ cbm_pipeline_t *baseline = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(baseline);
+ ASSERT_EQ(cbm_pipeline_run(baseline), 0);
+ char project[256];
+ snprintf(project, sizeof(project), "%s", cbm_pipeline_project_name(baseline));
+ cbm_pipeline_free(baseline);
+
+ char stale_stage[600];
+ char stale_shm[600];
+ snprintf(stale_stage, sizeof(stale_stage), "%s.stage.deadbe", db_path);
+ snprintf(stale_shm, sizeof(stale_shm), "%s.stage.deadbe-shm", db_path);
+ ASSERT_EQ(th_write_file(stale_stage, ""), 0);
+ ASSERT_EQ(th_write_file(stale_shm, "stale-shm"), 0);
+
+ /* A body-only change: no added names, so the planner may repair. */
+ write_temp_file(tmp, "generation.py", "def StableGeneration():\n return 2\n");
+ cbm_pipeline_incremental_test_reset_faults();
+ cbm_pipeline_t *incr = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(incr);
+ int incr_rc = cbm_pipeline_run(incr);
+ cbm_incremental_route_t route = cbm_pipeline_incremental_test_last_route();
+ cbm_pipeline_free(incr);
+
+ bool stale_stage_gone = !path_exists(stale_stage);
+ bool stale_shm_gone = !path_exists(stale_shm);
+ int stage_count = count_generation_stage_artifacts(tmp, "generation.db");
+ int stable_count = -1;
+ int absent_count = -1;
+ observe_named_generation(db_path, project, "StableGeneration", "NeverDefined", &stable_count,
+ &absent_count);
+ cbm_pipeline_incremental_test_reset_faults();
+ th_rmtree(tmp);
+
+ ASSERT_EQ(incr_rc, 0);
+ ASSERT_TRUE(route != CBM_INCREMENTAL_ROUTE_FORCED_FULL);
+ ASSERT_TRUE(route != CBM_INCREMENTAL_ROUTE_NONE);
+ ASSERT_TRUE(stale_stage_gone);
+ ASSERT_TRUE(stale_shm_gone);
+ ASSERT_EQ(stage_count, 0);
+ ASSERT_EQ(stable_count, 1);
+ ASSERT_EQ(absent_count, 0);
+ PASS();
+}
+
+/* #1839: the sweep removes exactly the stages nobody owns. A dead writer's
+ * stage (main, -wal, and the unlocked .lock sidecar its death left) goes; a
+ * stage whose writer is LIVE -- here the test, holding its lock -- is kept
+ * byte for byte, and goes only once that lock is dropped. No timing: liveness
+ * is the kernel lock, nothing else. */
+TEST(pipeline_sweep_removes_dead_writer_stage_keeps_locked_stage) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_stage_sweep_XXXXXX");
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+ write_temp_file(tmp, "generation.py", "def StableGeneration():\n return 1\n");
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/generation.db", tmp);
+
+ cbm_pipeline_incremental_test_reset_faults();
+ cbm_pipeline_t *baseline = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(baseline);
+ ASSERT_EQ(cbm_pipeline_run(baseline), 0);
+ char project[256];
+ snprintf(project, sizeof(project), "%s", cbm_pipeline_project_name(baseline));
+ cbm_pipeline_free(baseline);
+
+ char dead_stage[600];
+ char dead_wal[600];
+ char dead_lock[600];
+ char live_stage[600];
+ char live_lock[600];
+ snprintf(dead_stage, sizeof(dead_stage), "%s.stage.aaaaaa", db_path);
+ snprintf(dead_wal, sizeof(dead_wal), "%s.stage.aaaaaa-wal", db_path);
+ snprintf(dead_lock, sizeof(dead_lock), "%s.stage.aaaaaa.lock", db_path);
+ snprintf(live_stage, sizeof(live_stage), "%s.stage.bbbbbb", db_path);
+ snprintf(live_lock, sizeof(live_lock), "%s.stage.bbbbbb.lock", db_path);
+ static const char live_bytes[] = "live-stage-bytes";
+ ASSERT_EQ(th_write_file(dead_stage, "dead-stage-bytes"), 0);
+ ASSERT_EQ(th_write_file(dead_wal, "dead-wal"), 0);
+ ASSERT_EQ(th_write_file(dead_lock, ""), 0);
+ ASSERT_EQ(th_write_file(live_stage, live_bytes), 0);
+ int live_fd = cbm_pipeline_stage_lock_hold(live_stage);
+ ASSERT_TRUE(live_fd >= 0);
+
+ write_temp_file(tmp, "generation.py", "def StableGeneration():\n return 2\n");
+ cbm_pipeline_incremental_test_reset_faults();
+ cbm_pipeline_t *first = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(first);
+ int first_rc = cbm_pipeline_run(first);
+ cbm_pipeline_free(first);
+
+ bool dead_stage_gone = !path_exists(dead_stage);
+ bool dead_wal_gone = !path_exists(dead_wal);
+ bool dead_lock_gone = !path_exists(dead_lock);
+ bool live_kept = file_has_content(live_stage, live_bytes);
+ bool live_lock_kept = path_exists(live_lock);
+ int stable_after_first = -1;
+ int absent_after_first = -1;
+ observe_named_generation(db_path, project, "StableGeneration", "NeverDefined",
+ &stable_after_first, &absent_after_first);
+
+ cbm_pipeline_stage_lock_drop(live_stage, live_fd);
+ cbm_pipeline_incremental_test_reset_faults();
+ cbm_pipeline_t *second = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(second);
+ int second_rc = cbm_pipeline_run(second);
+ cbm_pipeline_free(second);
+
+ bool live_gone = !path_exists(live_stage);
+ bool live_lock_gone = !path_exists(live_lock);
+ int stage_count = count_generation_stage_artifacts(tmp, "generation.db");
+ int stable_after_second = -1;
+ int absent_after_second = -1;
+ observe_named_generation(db_path, project, "StableGeneration", "NeverDefined",
+ &stable_after_second, &absent_after_second);
+ cbm_pipeline_incremental_test_reset_faults();
+ th_rmtree(tmp);
+
+ ASSERT_EQ(first_rc, 0);
+ ASSERT_TRUE(dead_stage_gone);
+ ASSERT_TRUE(dead_wal_gone);
+ ASSERT_TRUE(dead_lock_gone);
+ ASSERT_TRUE(live_kept);
+ ASSERT_TRUE(live_lock_kept);
+ ASSERT_EQ(stable_after_first, 1);
+ ASSERT_EQ(absent_after_first, 0);
+ ASSERT_EQ(second_rc, 0);
+ ASSERT_TRUE(live_gone);
+ ASSERT_TRUE(live_lock_gone);
+ ASSERT_EQ(stage_count, 0);
+ ASSERT_EQ(stable_after_second, 1);
+ ASSERT_EQ(absent_after_second, 0);
+ PASS();
+}
+
/* Discovery and extraction must describe the same immutable generation. A
* source file created after extraction is not present in the original file
* list, so merely re-hashing that list cannot detect the race. Publication
@@ -14253,6 +14411,8 @@ SUITE(pipeline_semantic_manifest_repro) {
RUN_TEST(pipeline_publication_never_uses_a_predictable_staging_path);
RUN_TEST(pipeline_stage_names_never_nest);
RUN_TEST(pipeline_minted_stage_is_owned_until_released);
+ RUN_TEST(pipeline_stale_zero_byte_stage_beside_valid_db_routes_incremental_and_is_swept);
+ RUN_TEST(pipeline_sweep_removes_dead_writer_stage_keeps_locked_stage);
RUN_TEST(pipeline_source_mutation_before_publication_preserves_previous_generation);
RUN_TEST(pipeline_source_addition_before_publication_preserves_previous_generation);
RUN_TEST(pipeline_tsconfig_mutation_before_publication_preserves_previous_generation);