Skip to content

Commit 82ae8a3

Browse files
junk151516claude
authored andcommitted
fix(graph-buffer): log why an atomic publish failed instead of a successful dump
cbm_gbuf_dump_to_sqlite() emitted gbuf.dump regardless of the writer's result, so a run that published nothing still logged node and edge counts as if it had worked, and the failure reached the user only as the generic "Pipeline failed. Check repo_path exists and contains source files." The writer's temp -> final rename in publish_writer_output() returned a bare ERR_WRITE_FAILED. DeusData#1628 taught cbm_rename_replace() to translate the platform error into errno so callers could report why, and wired up the stage -> final rename in pipeline.c. This is the other rename on that path, and it is the one that runs first. Preserve errno across the cleanup in every publish error branch. That includes cbm_writer_open(), where the cleanup unlinks a file that was never created: without the save it leaves ENOENT behind, so a directory that denied the create is reported as a missing path — the DeusData#1620 case, stated wrongly. Read the value in graph_buffer.c before the profiling macro can clobber it, and report gbuf.dump_failed with the return code and that errno. A sticky append failure clears errno rather than attach a reason it does not have, and the field is omitted when there is none. No logging is added inside internal/cbm/, which has none today. Tests cover both halves: the dump reports a failure instead of a summary, the writer names its truncation reason, and the publish rename's errno survives a cleanup that itself fails. Refs DeusData#1620, DeusData#2001. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012FxkVF773gDGNmBmsLnzN7 Signed-off-by: juan Carlos Estrada Montoya <[email protected]>
1 parent fd42155 commit 82ae8a3

4 files changed

Lines changed: 233 additions & 1 deletion

File tree

‎internal/cbm/sqlite_writer.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <string.h>
2929
#include <stdint.h>
3030
#include <stdbool.h>
31+
#include <errno.h>
3132

3233
#ifdef _WIN32
3334
#include <io.h>
@@ -1777,13 +1778,18 @@ static int sync_writer_output(FILE *fp) {
17771778
}
17781779

17791780
static int discard_writer_output(write_db_ctx_t *w, int rc) {
1781+
/* Cleanup runs after the failure that brought us here, and a library
1782+
* call may set errno even when it succeeds. Carry the reason across it
1783+
* so the caller can report WHY the publish failed. */
1784+
int failure_errno = errno;
17801785
if (w->fp) {
17811786
(void)fclose(w->fp);
17821787
w->fp = NULL;
17831788
}
17841789
if (w->temp_path[0]) {
17851790
(void)cbm_unlink(w->temp_path);
17861791
}
1792+
errno = failure_errno;
17871793
return rc;
17881794
}
17891795

@@ -1792,18 +1798,25 @@ static int publish_writer_output(write_db_ctx_t *w) {
17921798
return discard_writer_output(w, ERR_WRITE_FAILED);
17931799
}
17941800
if (fclose(w->fp) != 0) {
1801+
int failure_errno = errno;
17951802
w->fp = NULL;
17961803
if (w->temp_path[0]) {
17971804
(void)cbm_unlink(w->temp_path);
17981805
}
1806+
errno = failure_errno;
17991807
return ERR_WRITE_FAILED;
18001808
}
18011809
w->fp = NULL;
18021810
if (!w->temp_path[0] || !w->final_path[0]) {
18031811
return 0;
18041812
}
18051813
if (cbm_rename_replace(w->temp_path, w->final_path) != 0) {
1814+
/* cbm_rename_replace translated the platform error into errno so the
1815+
* caller can say what denied the publish (#1620). Preserve it across
1816+
* the cleanup unlink. */
1817+
int rename_errno = errno;
18061818
(void)cbm_unlink(w->temp_path);
1819+
errno = rename_errno;
18071820
return ERR_WRITE_FAILED;
18081821
}
18091822
/* Sidecars are removed only after the replacement succeeds. On POSIX,
@@ -2303,13 +2316,22 @@ cbm_db_writer_t *cbm_writer_open(const char *path) {
23032316
int n = snprintf(w->wc.final_path, sizeof(w->wc.final_path), "%s", path);
23042317
if (n < 0 || (size_t)n >= sizeof(w->wc.final_path) ||
23052318
make_writer_temp_path(path, w, w->wc.temp_path, sizeof(w->wc.temp_path)) != 0) {
2319+
/* Both conditions are truncation and neither sets errno; name the
2320+
* reason rather than let the caller report a stale one. */
23062321
free(w);
2322+
errno = ENAMETOOLONG;
23072323
return NULL;
23082324
}
23092325
FILE *fp = cbm_fopen(w->wc.temp_path, "wb");
23102326
if (!fp) {
2327+
/* The cleanup below unlinks a file that was never created, so it
2328+
* fails and leaves ENOENT behind — which reads as a missing path
2329+
* when the real answer is that the directory refused the create.
2330+
* That is the #1620 case, so carry the open's reason across it. */
2331+
int open_errno = errno;
23112332
(void)cbm_unlink(w->wc.temp_path);
23122333
free(w);
2334+
errno = open_errno;
23132335
return NULL;
23142336
}
23152337
w->wc.fp = fp;
@@ -2377,6 +2399,10 @@ int cbm_writer_finalize(cbm_db_writer_t *w, const char *project, const char *roo
23772399
write_db_ctx_t wc = w->wc; /* value copy survives free(w) */
23782400
free(w);
23792401
if (err != 0) {
2402+
/* A sticky append failure: errno belongs to whatever call failed
2403+
* many calls ago, not to the publish. Clear it so the caller does
2404+
* not attach a reason this path does not have. */
2405+
errno = 0;
23802406
return discard_writer_output(&wc, err);
23812407
}
23822408
return write_db_after_nodes(&wc, nodes_root);

‎src/graph_buffer/graph_buffer.c‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ enum {
3939
#include "foundation/mem_core.h"
4040
#include <sqlite3.h>
4141

42+
#include <errno.h>
4243
#include <stdatomic.h>
4344
#include <stdint.h> // int64_t
4445
#include <stdio.h>
@@ -1841,6 +1842,32 @@ static void log_dump_summary(int node_count, int edge_count) {
18411842
cbm_log_info("gbuf.dump", "nodes", b1, "edges", b2);
18421843
}
18431844

1845+
/* A dump that published nothing gets exactly one signature in the log.
1846+
* Without it the writer's return code is the only evidence, and it reaches
1847+
* the user as a generic pipeline failure that blames their repository
1848+
* (#1620, #2001).
1849+
*
1850+
* failure_errno is read by the caller at the point of failure: the value is
1851+
* only meaningful before the next library call. Zero means the failure did
1852+
* not originate at the publish boundary — an append that failed many calls
1853+
* earlier — and the field is omitted rather than claim a reason this record
1854+
* does not have. */
1855+
static void log_dump_failed(int rc, int failure_errno, int node_count, int edge_count) {
1856+
char b1[CBM_SZ_16];
1857+
char b3[CBM_SZ_16];
1858+
char b4[CBM_SZ_16];
1859+
snprintf(b1, sizeof(b1), "%d", rc);
1860+
snprintf(b3, sizeof(b3), "%d", node_count);
1861+
snprintf(b4, sizeof(b4), "%d", edge_count);
1862+
if (failure_errno == 0) {
1863+
cbm_log_error("gbuf.dump_failed", "rc", b1, "nodes", b3, "edges", b4);
1864+
return;
1865+
}
1866+
char b2[CBM_SZ_16];
1867+
snprintf(b2, sizeof(b2), "%d", failure_errno);
1868+
cbm_log_error("gbuf.dump_failed", "rc", b1, "errno", b2, "nodes", b3, "edges", b4);
1869+
}
1870+
18441871
static void free_dump_resources(char **url_paths, char **local_names, int edge_count,
18451872
CBMDumpEdge *dump_edges, CBMDumpNode *dump_nodes,
18461873
int64_t *temp_to_final) {
@@ -1896,6 +1923,7 @@ int cbm_gbuf_dump_to_sqlite(cbm_gbuf_t *gb, const char *path) {
18961923
int64_t max_temp_id = gb->next_id;
18971924
int64_t *temp_to_final = cbm_calloc(CBM_MEM_CLASS_DUMP, (size_t)max_temp_id * sizeof(int64_t));
18981925
if (!temp_to_final) {
1926+
log_dump_failed(CBM_NOT_FOUND, errno, 0, 0);
18991927
return CBM_NOT_FOUND;
19001928
}
19011929

@@ -1926,6 +1954,7 @@ int cbm_gbuf_dump_to_sqlite(cbm_gbuf_t *gb, const char *path) {
19261954
* uninitialized budget from ever triggering the free). */
19271955
cbm_db_writer_t *w = cbm_writer_open(path);
19281956
if (!w) {
1957+
log_dump_failed(CBM_NOT_FOUND, errno, node_idx, 0);
19291958
cbm_free(CBM_MEM_CLASS_DUMP, src_nodes);
19301959
cbm_free(CBM_MEM_CLASS_DUMP, dump_nodes);
19311960
cbm_free(CBM_MEM_CLASS_DUMP, temp_to_final);
@@ -1980,12 +2009,19 @@ int cbm_gbuf_dump_to_sqlite(cbm_gbuf_t *gb, const char *path) {
19802009
int frc = cbm_writer_finalize(w, gb->project, gb->root_path, indexed_at, dump_nodes, node_idx,
19812010
dump_edges, edge_idx, gb->dump_vectors, gb->dump_vector_count,
19822011
gb->dump_token_vecs, gb->dump_token_vec_count);
2012+
/* Read errno before anything else can touch it: the profiling macro
2013+
* below is already one library call away from losing the reason. */
2014+
int finalize_errno = errno;
19832015
CBM_PROF_END_N("dump", "6_write_db_finalize", t_finalize, node_idx + edge_idx);
19842016
if (rc == 0) {
19852017
rc = frc;
19862018
}
19872019

1988-
log_dump_summary(node_idx, edge_idx);
2020+
if (rc != 0) {
2021+
log_dump_failed(rc, finalize_errno, node_idx, edge_idx);
2022+
} else {
2023+
log_dump_summary(node_idx, edge_idx);
2024+
}
19892025
free_dump_resources(url_paths, local_names, edge_idx, dump_edges, dump_nodes, temp_to_final);
19902026
cbm_free(CBM_MEM_CLASS_DUMP, src_nodes);
19912027
return rc;

‎tests/test_graph_buffer.c‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include "foundation/mem_core.h"
1010
#include <stdatomic.h>
1111
#include "store/store.h"
12+
#include "../src/foundation/compat.h"
13+
#include "foundation/compat_fs.h"
14+
#include "foundation/log.h"
15+
#include <stdio.h>
1216
#include <string.h>
1317

1418
/* ── Node operations ───────────────────────────────────────────── */
@@ -1120,6 +1124,72 @@ TEST(gbuf_flush_skips_orphan_edges) {
11201124
PASS();
11211125
}
11221126

1127+
/* ── Publish failure reporting ───────────────────────────────── */
1128+
1129+
static char g_log_capture[4096];
1130+
static CBMLogLevel g_prev_log_level;
1131+
static CBMLogFormat g_prev_log_format;
1132+
1133+
static void capture_log_sink(const char *line) {
1134+
size_t used = strlen(g_log_capture);
1135+
size_t avail = sizeof(g_log_capture) - used;
1136+
if (avail <= 1) {
1137+
return;
1138+
}
1139+
int n = snprintf(g_log_capture + used, avail, "%s\n", line);
1140+
if (n < 0 || (size_t)n >= avail) {
1141+
g_log_capture[sizeof(g_log_capture) - 1] = '\0';
1142+
}
1143+
}
1144+
1145+
static void capture_logs_start(void) {
1146+
g_log_capture[0] = '\0';
1147+
g_prev_log_level = cbm_log_get_level();
1148+
g_prev_log_format = cbm_log_get_format();
1149+
cbm_log_set_level(CBM_LOG_DEBUG);
1150+
/* The assertions below read the text encoding, so pin it rather than
1151+
* inherit whatever CBM_LOG_FORMAT left set. */
1152+
cbm_log_set_format(CBM_LOG_FORMAT_TEXT);
1153+
cbm_log_set_sink(capture_log_sink);
1154+
}
1155+
1156+
static const char *capture_logs_end(void) {
1157+
cbm_log_set_sink(NULL);
1158+
cbm_log_set_level(g_prev_log_level);
1159+
cbm_log_set_format(g_prev_log_format);
1160+
return g_log_capture;
1161+
}
1162+
1163+
/* A dump that publishes nothing has to say so, and say why. Renaming onto an
1164+
* existing directory is how test_sqlite_writer already forces the publish to
1165+
* fail; here it stands in for any host that denies the rename (#1620). */
1166+
TEST(gbuf_dump_failure_logs_reason) {
1167+
char dir[256];
1168+
snprintf(dir, sizeof(dir), "/tmp/cbm_gbuf_pub_XXXXXX");
1169+
ASSERT_NOT_NULL(cbm_mkdtemp(dir));
1170+
1171+
cbm_gbuf_t *gb = cbm_gbuf_new("test", "/tmp/repo");
1172+
ASSERT_NOT_NULL(gb);
1173+
int64_t id = cbm_gbuf_upsert_node(gb, "Function", "main", "pkg.main", "main.go", 1, 10, "{}");
1174+
ASSERT_GT(id, 0);
1175+
1176+
capture_logs_start();
1177+
int rc = cbm_gbuf_dump_to_sqlite(gb, dir);
1178+
const char *logs = capture_logs_end();
1179+
1180+
ASSERT(rc != 0);
1181+
ASSERT_NOT_NULL(strstr(logs, "gbuf.dump_failed"));
1182+
/* The reason survived the cleanup unlink. */
1183+
ASSERT_NOT_NULL(strstr(logs, "errno="));
1184+
ASSERT(strstr(logs, "errno=0 ") == NULL);
1185+
/* And the run is not also reported as a successful dump. */
1186+
ASSERT(strstr(logs, "msg=gbuf.dump ") == NULL);
1187+
1188+
cbm_gbuf_free(gb);
1189+
cbm_rmdir(dir);
1190+
PASS();
1191+
}
1192+
11231193
/* ── Suite ─────────────────────────────────────────────────────── */
11241194

11251195
/* A worker buffer draws ids from the shared counter, so a dense id -> node
@@ -1221,4 +1291,7 @@ SUITE(graph_buffer) {
12211291
RUN_TEST(gbuf_shared_ids_null_fallback);
12221292
RUN_TEST(gbuf_next_id_set_next_id_roundtrip);
12231293
RUN_TEST(gbuf_next_id_null_safe);
1294+
1295+
/* Publish failure reporting */
1296+
RUN_TEST(gbuf_dump_failure_logs_reason);
12241297
}

‎tests/test_sqlite_writer.c‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
/* sqlite_writer.h is at internal/cbm/ — Makefile adds -Iinternal/cbm */
1414
#include "sqlite_writer.h" /* CBMDumpNode, CBMDumpEdge, cbm_write_db */
1515
#include "sqlite3.h" /* vendored/sqlite3/ via -Ivendored/sqlite3 */
16+
#include <errno.h>
1617
#include <unistd.h>
1718

1819
/* ── Helper: create temp file path ─────────────────────────────── */
@@ -101,6 +102,45 @@ static int count_temp_outputs_for(const char *path) {
101102
return count;
102103
}
103104

105+
/* Locate the writer's temp output for `path`. Same naming rule as
106+
* count_temp_outputs_for, but returns the full path of the first match. */
107+
static int find_temp_output_for(const char *path, char *out, size_t out_size) {
108+
char dir[256];
109+
char base[256];
110+
const char *slash = strrchr(path, '/');
111+
if (slash) {
112+
size_t dir_len = (size_t)(slash - path);
113+
if (dir_len == 0 || dir_len >= sizeof(dir)) {
114+
return -1;
115+
}
116+
memcpy(dir, path, dir_len);
117+
dir[dir_len] = '\0';
118+
snprintf(base, sizeof(base), "%s", slash + 1);
119+
} else {
120+
snprintf(dir, sizeof(dir), ".");
121+
snprintf(base, sizeof(base), "%s", path);
122+
}
123+
124+
cbm_dir_t *d = cbm_opendir(dir);
125+
if (!d) {
126+
return -1;
127+
}
128+
size_t base_len = strlen(base);
129+
int found = -1;
130+
cbm_dirent_t *ent;
131+
while ((ent = cbm_readdir(d)) != NULL) {
132+
size_t name_len = strlen(ent->name);
133+
if (name_len > base_len + 5 && strncmp(ent->name, base, base_len) == 0 &&
134+
strncmp(ent->name + base_len, ".tmp.", 5) == 0) {
135+
snprintf(out, out_size, "%s/%s", dir, ent->name);
136+
found = 0;
137+
break;
138+
}
139+
}
140+
cbm_closedir(d);
141+
return found;
142+
}
143+
104144
/* ── Tests ─────────────────────────────────────────────────────── */
105145

106146
TEST(sw_minimal_data) {
@@ -877,6 +917,61 @@ TEST(sw_publish_preserves_live_reader) {
877917

878918
/* ── Suite ─────────────────────────────────────────────────────── */
879919

920+
TEST(sw_open_truncated_path_names_its_reason) {
921+
/* final_path is a fixed 4K buffer and make_writer_temp_path only fails by
922+
* truncation. Neither sets an errno of its own, so without naming one the
923+
* caller reports whatever was left behind by an unrelated call. */
924+
char longpath[5000];
925+
memset(longpath, 'a', sizeof(longpath) - 1);
926+
longpath[sizeof(longpath) - 1] = '\0';
927+
longpath[0] = '/';
928+
929+
errno = 0;
930+
cbm_db_writer_t *w = cbm_writer_open(longpath);
931+
ASSERT(w == NULL);
932+
ASSERT_EQ(errno, ENAMETOOLONG);
933+
PASS();
934+
}
935+
936+
TEST(sw_publish_failure_reports_the_rename_not_the_cleanup) {
937+
#ifdef _WIN32
938+
SKIP_PLATFORM("removing a still-open file to make the cleanup unlink fail");
939+
#endif
940+
char dir[256];
941+
snprintf(dir, sizeof(dir), "/tmp/cbm_sw_cleanup_XXXXXX");
942+
ASSERT(cbm_mkdtemp(dir) != NULL);
943+
944+
char final_path[320];
945+
snprintf(final_path, sizeof(final_path), "%s/db.sqlite", dir);
946+
ASSERT_EQ(write_fixture_file(final_path, "destination"), 0);
947+
948+
cbm_db_writer_t *w = cbm_writer_open(final_path);
949+
ASSERT(w != NULL);
950+
951+
/* Turn the still-open temp into a directory. The publish rename then fails
952+
* with ENOTDIR (directory onto a file), and the cleanup unlink fails too
953+
* (EISDIR on Linux, EPERM on macOS) — so only a saved errno still carries
954+
* the rename's reason out to the caller. */
955+
char temp[512];
956+
ASSERT_EQ(find_temp_output_for(final_path, temp, sizeof(temp)), 0);
957+
ASSERT_EQ(cbm_unlink(temp), 0);
958+
ASSERT(cbm_mkdir_p(temp, 0700));
959+
960+
errno = 0;
961+
int rc = cbm_writer_finalize(w, "test", "/tmp/test", "2026-07-07T00:00:00Z", NULL, 0, NULL, 0,
962+
NULL, 0, NULL, 0);
963+
int publish_errno = errno;
964+
ASSERT(rc != 0);
965+
ASSERT_EQ(publish_errno, ENOTDIR);
966+
/* The destination is intact: a failed publish never replaces it. */
967+
ASSERT(fixture_file_equals(final_path, "destination"));
968+
969+
cbm_rmdir(temp);
970+
cbm_unlink(final_path);
971+
cbm_rmdir(dir);
972+
PASS();
973+
}
974+
880975
SUITE(sqlite_writer) {
881976
RUN_TEST(sw_minimal_data);
882977
RUN_TEST(sw_imports_local_name_unique);
@@ -890,4 +985,6 @@ SUITE(sqlite_writer) {
890985
RUN_TEST(sw_publish_failure_preserves_destination_sidecars);
891986
RUN_TEST(sw_publish_supports_non_ascii_path);
892987
RUN_TEST(sw_publish_preserves_live_reader);
988+
RUN_TEST(sw_open_truncated_path_names_its_reason);
989+
RUN_TEST(sw_publish_failure_reports_the_rename_not_the_cleanup);
893990
}

0 commit comments

Comments
 (0)