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(mcp): survive atomic binary replacement

sunmingqiang11bbf0d84772
diff --git a/src/ui/http_server.c b/src/ui/http_server.c
index 18589d8d8..e3b5c8b29 100644
--- a/src/ui/http_server.c
+++ b/src/ui/http_server.c
@@ -1052,9 +1052,18 @@ bool cbm_http_server_resolve_binary_path(const char *argv0, char *out, size_t ou
}
#endif
+#ifndef _WIN32
+ if (g_binary_path[0] && is_executable_file(g_binary_path)) {
+ return copy_path(out, outsz, g_binary_path);
+ }
+ if (resolve_self_executable(out, outsz) && is_executable_file(out)) {
+ return true;
+ }
+#else
if (resolve_self_executable(out, outsz)) {
return true;
}
+#endif
return copy_path(out, outsz, argv0);
}
diff --git a/tests/test_httpd.c b/tests/test_httpd.c
index 0d2f82402..d71cb6518 100644
--- a/tests/test_httpd.c
+++ b/tests/test_httpd.c
@@ -457,6 +457,69 @@ TEST(httpd_resolves_bare_binary_path_from_path) {
#endif
}
+TEST(httpd_resolves_deleted_self_to_replaced_launch_path) {
+#if defined(__linux__)
+ char tmpdir[256];
+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_httpd_deleted_self_XXXXXX");
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmpdir));
+
+ char current[1024];
+ ssize_t current_len = readlink("/proc/self/exe", current, sizeof(current) - 1);
+ ASSERT_GT(current_len, 0);
+ ASSERT_LT(current_len, (ssize_t)(sizeof(current) - 1));
+ current[current_len] = '\0';
+
+ char launch_path[512];
+ char replacement_path[512];
+ snprintf(launch_path, sizeof(launch_path), "%s/test-runner", tmpdir);
+ snprintf(replacement_path, sizeof(replacement_path), "%s/test-runner.new", tmpdir);
+ ASSERT_EQ(cbm_copy_file(current, launch_path), 0);
+ ASSERT_EQ(chmod(launch_path, 0755), 0);
+
+ int ready_pipe[2];
+ int continue_pipe[2];
+ ASSERT_EQ(pipe(ready_pipe), 0);
+ ASSERT_EQ(pipe(continue_pipe), 0);
+
+ pid_t child = fork();
+ if (child == 0) {
+ close(ready_pipe[0]);
+ close(continue_pipe[1]);
+ char ready_fd[32];
+ char continue_fd[32];
+ snprintf(ready_fd, sizeof(ready_fd), "%d", ready_pipe[1]);
+ snprintf(continue_fd, sizeof(continue_fd), "%d", continue_pipe[0]);
+ execl(launch_path, launch_path, "__cbm_deleted_self_probe", ready_fd, continue_fd,
+ launch_path, (char *)NULL);
+ _exit(127);
+ }
+ ASSERT_GT(child, 0);
+
+ close(ready_pipe[1]);
+ close(continue_pipe[0]);
+ char ready = '\0';
+ ASSERT_EQ(read(ready_pipe[0], &ready, 1), 1);
+ ASSERT_EQ(ready, 'R');
+
+ ASSERT_EQ(cbm_copy_file(current, replacement_path), 0);
+ ASSERT_EQ(chmod(replacement_path, 0755), 0);
+ ASSERT_EQ(rename(replacement_path, launch_path), 0);
+ ASSERT_EQ(write(continue_pipe[1], "G", 1), 1);
+ close(ready_pipe[0]);
+ close(continue_pipe[1]);
+
+ int status = 0;
+ ASSERT_EQ(waitpid(child, &status, 0), child);
+ ASSERT_TRUE(WIFEXITED(status));
+ ASSERT_EQ(WEXITSTATUS(status), 0);
+ unlink(launch_path);
+ rmdir(tmpdir);
+ PASS();
+#else
+ PASS();
+#endif
+}
+
/* ── Transport integration (listener only) ────────────────────── */
TEST(httpd_listen_ephemeral_port) {
@@ -2278,6 +2341,7 @@ SUITE(httpd) {
RUN_TEST(httpd_query_param_edge_cases);
RUN_TEST(httpd_path_match_matrix);
RUN_TEST(httpd_resolves_bare_binary_path_from_path);
+ RUN_TEST(httpd_resolves_deleted_self_to_replaced_launch_path);
RUN_TEST(repo_info_web_base_normalizes_to_https);
RUN_TEST(repo_info_strips_credentials_from_remote);
diff --git a/tests/test_main.c b/tests/test_main.c
index a8c2939e1..5ae295568 100644
--- a/tests/test_main.c
+++ b/tests/test_main.c
@@ -22,6 +22,7 @@ int tf_skip_count = 0;
#include "daemon/version_cohort.h" /* Windows crash-turnover re-exec probe */
#include "mcp/index_supervisor.h" /* cbm_index_set_worker_role */
#include "mcp/mcp.h" /* cbm_mcp_handle_tool — act as a real worker */
+#include "ui/http_server.h" /* deleted-self executable probe */
#include <sqlite3.h>
#include <errno.h>
#include <stdbool.h>
@@ -608,6 +609,26 @@ static int tf_maybe_run_mcp_idxfailclosed_probe(int argc, char **argv) {
const char *cache_dir);
alarm(20);
return mcp_test_idxfailclosed_supervisor_start_check(argv[2], argv[3]);
+static int tf_maybe_run_deleted_self_probe(int argc, char **argv) {
+#if defined(__linux__)
+ if (argc != 5 || strcmp(argv[1], "__cbm_deleted_self_probe") != 0) {
+ return -1;
+ }
+ int ready_fd = atoi(argv[2]);
+ int continue_fd = atoi(argv[3]);
+ cbm_http_server_set_binary_path(argv[4]);
+ if (write(ready_fd, "R", 1) != 1) {
+ return 41;
+ }
+ char go = '\0';
+ if (read(continue_fd, &go, 1) != 1) {
+ return 42;
+ }
+ char resolved[1024];
+ if (!cbm_http_server_resolve_binary_path(NULL, resolved, sizeof(resolved))) {
+ return 43;
+ }
+ return strcmp(resolved, argv[4]) == 0 && access(resolved, X_OK) == 0 ? 0 : 44;
#else
(void)argc;
(void)argv;
@@ -866,6 +887,9 @@ int main(int argc, char **argv) {
int daemon_ipc_probe_rc = tf_maybe_run_daemon_ipc_lock_probe(argc, argv);
if (daemon_ipc_probe_rc >= 0) {
return daemon_ipc_probe_rc;
+ int deleted_self_rc = tf_maybe_run_deleted_self_probe(argc, argv);
+ if (deleted_self_rc >= 0) {
+ return deleted_self_rc;
}
/* #798 follow-up: if spawned as the socket-isolation probe, report whether an