Skip to content

Commit 7a1bee2

Browse files
committed
fix(index): keep the publication fence off raw allocator sites
The memory-core ratchet (lint-memory-core) failed on the publication fence commit: it added six raw free() calls, each releasing a block that existing code allocates with raw calloc, malloc or strdup. cbm_free cannot release such a block without the core class counters drifting, so every new exit now reuses an existing release point instead: - index_supervisor.c: worker_artifacts_create() creates the response, log and publish fence files and names the one that failed, so all three failures share the single artifact_create_failed exit. - pipeline.c: a refused publication gate takes the existing source-changed abort exit (same CBM_PIPELINE_ABORT_PRESERVE_DB result and cleanup). - mcp.c: index_request_names_free() is the one release point for the request name strings, used by index_worker_json_result() and index_args_serving_index_servable(). Raw-site counts return to the origin/main values (index_supervisor.c 8, pipeline.c 129, mcp.c 765) with no baseline change. No behaviour change. Signed-off-by: Martin Vogel <[email protected]>
1 parent 140e299 commit 7a1bee2

3 files changed

Lines changed: 42 additions & 33 deletions

File tree

‎src/mcp/index_supervisor.c‎

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,25 @@ static void worker_publish_fence_remove(cbm_index_worker_handle_t *handle) {
10401040
}
10411041
}
10421042

1043+
/* Create the private files of one attempt. NULL on success; otherwise the name
1044+
* of the artifact that could not be created, with errno as that step left it
1045+
* and every earlier artifact still in place for the caller to remove. */
1046+
static const char *worker_artifacts_create(cbm_index_worker_handle_t *handle) {
1047+
if (!worker_unique_file(handle->response_path, sizeof(handle->response_path), "response")) {
1048+
return "response";
1049+
}
1050+
if (!worker_unique_file(handle->log_path, sizeof(handle->log_path), "log")) {
1051+
return "log";
1052+
}
1053+
/* Opt-in: only an enabled worker limit can stop a worker, so only then is
1054+
* there a publication to coordinate. */
1055+
if (cbm_index_policy_worker_enabled(&handle->resource_policy) &&
1056+
!worker_publish_fence_create(handle)) {
1057+
return "publish_fence";
1058+
}
1059+
return NULL;
1060+
}
1061+
10431062
/* Settle who held the fence once the attempt is terminal. Claiming an unowned
10441063
* fence now also bars any later claim, so a stray member of an unquiesced tree
10451064
* can never publish after this report. */
@@ -1109,25 +1128,12 @@ static int worker_start_internal(const char *args_json, size_t memory_budget_byt
11091128
handle->log_callback = log_callback;
11101129
handle->log_context = log_context;
11111130
worker_result_init(&handle->result);
1112-
if (!worker_unique_file(handle->response_path, sizeof(handle->response_path), "response") ||
1113-
!worker_unique_file(handle->log_path, sizeof(handle->log_path), "log")) {
1131+
const char *failed_artifact = worker_artifacts_create(handle);
1132+
if (failed_artifact) {
11141133
int saved_errno = errno;
11151134
char error_text[CBM_SZ_32];
11161135
(void)snprintf(error_text, sizeof(error_text), "%d", saved_errno);
1117-
cbm_log_error("index.supervisor.artifact_create_failed", "artifact",
1118-
handle->response_path[0] ? "log" : "response", "errno", error_text);
1119-
(void)cbm_unlink(handle->response_path);
1120-
(void)cbm_unlink(handle->log_path);
1121-
free(handle);
1122-
return -1;
1123-
}
1124-
/* Opt-in: only an enabled worker limit can stop a worker, so only then is
1125-
* there a publication to coordinate. */
1126-
if (cbm_index_policy_worker_enabled(&handle->resource_policy) &&
1127-
!worker_publish_fence_create(handle)) {
1128-
char error_text[CBM_SZ_32];
1129-
(void)snprintf(error_text, sizeof(error_text), "%d", errno);
1130-
cbm_log_error("index.supervisor.artifact_create_failed", "artifact", "publish_fence",
1136+
cbm_log_error("index.supervisor.artifact_create_failed", "artifact", failed_artifact,
11311137
"errno", error_text);
11321138
(void)cbm_unlink(handle->response_path);
11331139
(void)cbm_unlink(handle->log_path);

‎src/mcp/mcp.c‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11952,15 +11952,23 @@ static bool build_index_success_response(cbm_mcp_server_t *srv, yyjson_mut_doc *
1195211952

1195311953
static bool project_db_is_servable(const char *project, const char *db_path);
1195411954

11955+
/* The heap strings that name the project of an internal worker request
11956+
* (repo_path and name from cbm_mcp_get_string_arg, the project name derived from
11957+
* them). One release point for every helper that reads them; free(NULL) is a
11958+
* no-op, so any of them may be NULL. */
11959+
static void index_request_names_free(char *repo_path, char *name_override, char *project_name) {
11960+
free(repo_path);
11961+
free(name_override);
11962+
free(project_name);
11963+
}
11964+
1195511965
/* Serialize a worker JSON document and release the heap strings it borrowed.
11956-
* Extra arguments may be NULL; free(NULL) is a no-op. */
11966+
* Extra arguments may be NULL. */
1195711967
static char *index_worker_json_result(yyjson_mut_doc *doc, char *repo_path, char *name_override,
1195811968
char *project_name) {
1195911969
char *json = yy_doc_to_str(doc);
1196011970
yyjson_mut_doc_free(doc);
11961-
free(repo_path);
11962-
free(name_override);
11963-
free(project_name);
11971+
index_request_names_free(repo_path, name_override, project_name);
1196411972
char *result = cbm_mcp_text_result(json, true);
1196511973
free(json);
1196611974
return result;
@@ -12034,9 +12042,7 @@ static bool index_args_serving_index_servable(const char *args) {
1203412042
project_db_path(project_name, db_path, sizeof(db_path));
1203512043
servable = project_db_is_servable(project_name, db_path);
1203612044
}
12037-
free(repo_path);
12038-
free(name_override);
12039-
free(project_name);
12045+
index_request_names_free(repo_path, name_override, project_name);
1204012046
return servable;
1204112047
}
1204212048

‎src/pipeline/pipeline.c‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5240,19 +5240,16 @@ int cbm_pipeline_run(cbm_pipeline_t *p) {
52405240
cbm_userconfig_t *fresh_config = cbm_userconfig_load_with_source(p->repo_path);
52415241
bool source_matches = cbm_pipeline_project_source_matches(p, fresh_config);
52425242
cbm_userconfig_free(fresh_config);
5243+
bool publish_allowed = source_matches;
52435244
if (!source_matches) {
52445245
cbm_log_error("pipeline.err", "phase", "test_declarations_source_changed");
5245-
cleanup_staging_db(staging_path);
5246-
free(staging_path);
5247-
free(final_path);
5248-
pipeline_release_test_config(p);
5249-
return CBM_PIPELINE_ABORT_PRESERVE_DB;
5250-
}
5251-
5252-
/* Last point at which the serving database is still untouched: the
5253-
* destination preparation below may already move it aside. */
5254-
if (p->publish_gate && !p->publish_gate(p->publish_gate_ctx)) {
5246+
} else if (p->publish_gate && !p->publish_gate(p->publish_gate_ctx)) {
5247+
/* Last point at which the serving database is still untouched: the
5248+
* destination preparation below may already move it aside. */
52555249
cbm_log_warn("pipeline.publish_refused", "path", final_path);
5250+
publish_allowed = false;
5251+
}
5252+
if (!publish_allowed) {
52565253
cleanup_staging_db(staging_path);
52575254
free(staging_path);
52585255
free(final_path);

0 commit comments

Comments
 (0)