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(daemon): stop the lifetime-lock probe from dropping a held lock

Amir Fathi3aec07ea3705
diff --git a/src/daemon/ipc.c b/src/daemon/ipc.c
index 3917a7a05..e3dbe0aff 100644
--- a/src/daemon/ipc.c
+++ b/src/daemon/ipc.c
@@ -1205,6 +1205,15 @@ static int posix_lifetime_lock_probe(const cbm_daemon_ipc_endpoint_t *endpoint,
if (process_claimed < 0) {
return -1;
}
+ if (process_claimed == 1) {
+ /* Already held by this process per the in-process registry: trust
+ * that alone. Opening a throwaway fd on the lock file here and then
+ * closing it would release the real fcntl(2) record lock, which is
+ * scoped to (process, inode) rather than (fd, inode): closing any
+ * fd on this inode drops every lock this process holds on it, even
+ * one held via a different, still-open fd. */
+ return endpoint_runtime_still_valid(endpoint) ? 1 : -1;
+ }
int fd = openat(endpoint->dir_fd, lock_name, O_RDWR | O_CLOEXEC | O_NOFOLLOW);
if (fd < 0) {
@@ -1216,10 +1225,6 @@ static int posix_lifetime_lock_probe(const cbm_daemon_ipc_endpoint_t *endpoint,
(void)close(fd);
return -1;
}
- if (process_claimed == 1) {
- bool still_private = endpoint_runtime_still_valid(endpoint);
- return close(fd) == 0 && still_private ? 1 : -1;
- }
struct flock record_lock = {
.l_type = F_WRLCK,
.l_whence = SEEK_SET,
diff --git a/tests/test_daemon_ipc.c b/tests/test_daemon_ipc.c
index 815f630a6..ce034dd11 100644
--- a/tests/test_daemon_ipc.c
+++ b/tests/test_daemon_ipc.c
@@ -3365,6 +3365,109 @@ TEST(daemon_ipc_posix_lifetime_reservation_rejects_fork_inheritance) {
PASS();
}
+static bool ipc_test_lifetime_lock_path(char out[TEST_PATH_CAP], const char *runtime_dir,
+ const char *key) {
+ int written = runtime_dir && key
+ ? snprintf(out, TEST_PATH_CAP, "%s/cbm-%s.lifetime.lock", runtime_dir, key)
+ : -1;
+ return written > 0 && written < TEST_PATH_CAP;
+}
+
+TEST(daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock) {
+ static const char key[] = "c1c1d2d2e3e3f4f4";
+ char parent[TEST_PATH_CAP] = {0};
+ char runtime_dir[TEST_PATH_CAP] = {0};
+ char lock_path[TEST_PATH_CAP] = {0};
+ cbm_daemon_ipc_endpoint_t *endpoint = NULL;
+ cbm_daemon_ipc_lifetime_reservation_t *reservation = NULL;
+ int result_pipe[2] = {-1, -1};
+ pid_t child = -1;
+ uint8_t child_result = 0;
+ int child_status = -1;
+ int acquired = -1;
+ int held_after_first_probe = -1;
+ int held_after_second_probe = -1;
+ int free_after_release = -1;
+ bool lock_path_ok = false;
+
+ if (ipc_test_parent_new(parent, "probe-holds-lock")) {
+ endpoint = cbm_daemon_ipc_endpoint_new(key, parent);
+ }
+ if (endpoint) {
+ ipc_test_copy_path(runtime_dir, cbm_daemon_ipc_endpoint_runtime_dir(endpoint));
+ lock_path_ok = ipc_test_lifetime_lock_path(lock_path, runtime_dir, key);
+ acquired = cbm_daemon_ipc_lifetime_reservation_try_acquire(endpoint, &reservation);
+ }
+ if (reservation) {
+ /* Two probes, matching how a real daemon polls its own reservation
+ * repeatedly while running. Each probe of an already-held lock used
+ * to open a throwaway fd on the lock file and close it. Since
+ * fcntl(2) record locks are scoped to (process, inode), that close
+ * silently released the real lock every time, regardless of the
+ * still-open fd the reservation itself is holding. */
+ held_after_first_probe = cbm_daemon_ipc_lifetime_reservation_probe(endpoint);
+ held_after_second_probe = cbm_daemon_ipc_lifetime_reservation_probe(endpoint);
+ }
+ if (reservation && lock_path_ok && pipe(result_pipe) == 0) {
+ child = fork();
+ }
+ if (child == 0) {
+ /* A genuinely independent process, not going through any of this
+ * codebase's APIs so it cannot inherit the parent's in-process
+ * claim registry: try to take the real OS-level lock directly. If
+ * the parent's fcntl lock survived the probes above, this fails. */
+ (void)close(result_pipe[0]);
+ int fd = open(lock_path, O_RDWR);
+ struct flock record_lock = {
+ .l_type = F_WRLCK,
+ .l_whence = SEEK_SET,
+ .l_start = 0,
+ .l_len = 0,
+ };
+ int lock_result = fd >= 0 ? fcntl(fd, F_SETLK, &record_lock) : -1;
+ child_result = lock_result == 0 ? 1 : 0;
+ if (fd >= 0) {
+ (void)close(fd);
+ }
+ bool reported = ipc_test_fd_write_all(result_pipe[1], &child_result, sizeof(child_result));
+ (void)close(result_pipe[1]);
+ _exit(reported ? 0 : 1);
+ }
+ if (child > 0) {
+ (void)close(result_pipe[1]);
+ result_pipe[1] = -1;
+ bool received = ipc_test_fd_read_all(result_pipe[0], &child_result, sizeof(child_result));
+ (void)close(result_pipe[0]);
+ result_pipe[0] = -1;
+ if (!received) {
+ child_result = 2;
+ }
+ while (waitpid(child, &child_status, 0) < 0 && errno == EINTR) {}
+ }
+ cbm_daemon_ipc_lifetime_reservation_release(reservation);
+ if (endpoint) {
+ free_after_release = cbm_daemon_ipc_lifetime_reservation_probe(endpoint);
+ }
+ for (size_t index = 0; index < 2; index++) {
+ if (result_pipe[index] >= 0) {
+ (void)close(result_pipe[index]);
+ }
+ }
+ cbm_daemon_ipc_endpoint_free(endpoint);
+ ipc_test_remove_tree(runtime_dir, parent);
+
+ ASSERT_EQ(acquired, 1);
+ ASSERT_TRUE(lock_path_ok);
+ ASSERT_EQ(held_after_first_probe, 1);
+ ASSERT_EQ(held_after_second_probe, 1);
+ ASSERT_GT(child, 0);
+ ASSERT_TRUE(WIFEXITED(child_status));
+ ASSERT_EQ(WEXITSTATUS(child_status), 0);
+ ASSERT_EQ(child_result, 0);
+ ASSERT_EQ(free_after_release, 0);
+ PASS();
+}
+
TEST(daemon_ipc_posix_child_participant_handoff_retains_legacy_bridge) {
#ifdef _WIN32
PASS();
@@ -5524,6 +5627,7 @@ SUITE(daemon_ipc) {
#endif
RUN_TEST(daemon_ipc_posix_startup_lock_is_cross_process);
RUN_TEST(daemon_ipc_posix_lifetime_reservation_rejects_fork_inheritance);
+ RUN_TEST(daemon_ipc_posix_lifetime_reservation_probe_does_not_drop_held_lock);
RUN_TEST(daemon_ipc_posix_child_participant_handoff_retains_legacy_bridge);
RUN_TEST(daemon_ipc_posix_publication_boundaries_recover_from_crash);
RUN_TEST(daemon_ipc_posix_record_publication_windows_recover_from_crash);