Skip to content

Commit 712a032

Browse files
committed
fix: serialize lifetime reservation probes and acquisitions
1 parent 96e44e2 commit 712a032

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

‎src/daemon/ipc.c‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,11 @@ static void posix_lifetime_lock_release(int fd, process_lock_entry_t *process_en
14001400
process_lock_unclaim(process_entry);
14011401
}
14021402

1403+
/* Serialize lifetime probe, claim and release within this process. Without this
1404+
* gate, a probe can observe an empty registry, then open/close the inode
1405+
* after another thread acquires its process-scoped fcntl lock. */
1406+
static pthread_mutex_t lifetime_lock_operations_mutex = PTHREAD_MUTEX_INITIALIZER;
1407+
14031408
int cbm_daemon_ipc_lifetime_reservation_try_acquire(
14041409
const cbm_daemon_ipc_endpoint_t *endpoint,
14051410
cbm_daemon_ipc_lifetime_reservation_t **reservation_out) {
@@ -1409,18 +1414,24 @@ int cbm_daemon_ipc_lifetime_reservation_try_acquire(
14091414
if (!endpoint || !reservation_out) {
14101415
return -1;
14111416
}
1417+
if (pthread_mutex_lock(&lifetime_lock_operations_mutex) != 0) {
1418+
return -1;
1419+
}
14121420
int fd = -1;
14131421
process_lock_entry_t *process_entry = NULL;
14141422
int result = posix_lifetime_lock_try_acquire(endpoint, endpoint->lifetime_lock_name, &fd,
14151423
&process_entry);
14161424
if (result != 1) {
1425+
(void)pthread_mutex_unlock(&lifetime_lock_operations_mutex);
14171426
return result;
14181427
}
14191428
cbm_daemon_ipc_lifetime_reservation_t *reservation = malloc(sizeof(*reservation));
14201429
if (!reservation) {
14211430
posix_lifetime_lock_release(fd, process_entry);
1431+
(void)pthread_mutex_unlock(&lifetime_lock_operations_mutex);
14221432
return -1;
14231433
}
1434+
(void)pthread_mutex_unlock(&lifetime_lock_operations_mutex);
14241435
reservation->fd = fd;
14251436
reservation->process_entry = process_entry;
14261437
reservation->owner_pid = getpid();
@@ -1433,7 +1444,11 @@ void cbm_daemon_ipc_lifetime_reservation_release(
14331444
if (!reservation) {
14341445
return;
14351446
}
1447+
if (pthread_mutex_lock(&lifetime_lock_operations_mutex) != 0) {
1448+
ipc_coordination_cleanup_fail_stop("lifetime_lock_mutex");
1449+
}
14361450
posix_lifetime_lock_release(reservation->fd, reservation->process_entry);
1451+
(void)pthread_mutex_unlock(&lifetime_lock_operations_mutex);
14371452
free(reservation);
14381453
}
14391454

@@ -1459,7 +1474,12 @@ static bool lifetime_reservation_matches_endpoint(
14591474
}
14601475

14611476
int cbm_daemon_ipc_lifetime_reservation_probe(const cbm_daemon_ipc_endpoint_t *endpoint) {
1462-
return endpoint ? posix_lifetime_lock_probe(endpoint, endpoint->lifetime_lock_name) : -1;
1477+
if (!endpoint || pthread_mutex_lock(&lifetime_lock_operations_mutex) != 0) {
1478+
return -1;
1479+
}
1480+
int result = posix_lifetime_lock_probe(endpoint, endpoint->lifetime_lock_name);
1481+
(void)pthread_mutex_unlock(&lifetime_lock_operations_mutex);
1482+
return result;
14631483
}
14641484

14651485
static int posix_startup_lock_probe(const cbm_daemon_ipc_endpoint_t *endpoint) {

0 commit comments

Comments
 (0)