Skip to content

Commit 94f952d

Browse files
authored
Merge pull request DeusData#2599 from pcristin/fix/windows-local-transition-cleanup
fix(daemon): wait out Windows local teardown contention
2 parents 205343e + af312fb commit 94f952d

5 files changed

Lines changed: 276 additions & 47 deletions

File tree

‎src/daemon/ipc.c‎

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ enum {
3131
CBM_DAEMON_IPC_RETRY_INTERVAL_MS = 10,
3232
CBM_DAEMON_IPC_COORDINATION_CLEANUP_MS = 500,
3333
CBM_DAEMON_IPC_WINDOWS_RENDEZVOUS_RETRY_MS = 250,
34+
CBM_DAEMON_IPC_RELEASE_ERROR = -1,
35+
CBM_DAEMON_IPC_RELEASE_COMPLETE = 1,
3436
};
3537

3638
/* Last private-namespace validation refusal, set at the exact failing check.
@@ -3988,27 +3990,27 @@ bool cbm_daemon_ipc_local_transition_begin_work(cbm_daemon_ipc_local_transition_
39883990
return true;
39893991
}
39903992

3991-
bool cbm_daemon_ipc_local_transition_release(cbm_daemon_ipc_local_transition_t **transition_io) {
3993+
int cbm_daemon_ipc_local_transition_try_release(cbm_daemon_ipc_local_transition_t **transition_io) {
39923994
if (!transition_io) {
3993-
return false;
3995+
return CBM_DAEMON_IPC_RELEASE_ERROR;
39943996
}
39953997
cbm_daemon_ipc_local_transition_t *transition = *transition_io;
39963998
if (!transition) {
3997-
return true;
3999+
return CBM_DAEMON_IPC_RELEASE_COMPLETE;
39984000
}
39994001
if (transition->legacy_fd < 0 || !transition->legacy_process_entry ||
40004002
transition->owner_pid != getpid() ||
40014003
(!transition->work_begun &&
40024004
(transition->startup_v2_fd < 0 || !transition->startup_v2_process_entry))) {
4003-
return false;
4005+
return CBM_DAEMON_IPC_RELEASE_ERROR;
40044006
}
40054007
posix_named_lock_release(transition->legacy_fd, transition->legacy_process_entry);
40064008
if (!transition->work_begun) {
40074009
posix_named_lock_release(transition->startup_v2_fd, transition->startup_v2_process_entry);
40084010
}
40094011
free(transition);
40104012
*transition_io = NULL;
4011-
return true;
4013+
return CBM_DAEMON_IPC_RELEASE_COMPLETE;
40124014
}
40134015

40144016
#else /* _WIN32 */
@@ -6203,35 +6205,39 @@ static void win_participant_group_release_complete(cbm_private_file_lock_t **gro
62036205
}
62046206
}
62056207

6206-
static bool win_participant_state_release(const cbm_daemon_ipc_endpoint_t *endpoint,
6207-
cbm_private_lock_directory_t *directory,
6208-
cbm_private_file_lock_t **startup_v2_io,
6209-
win_legacy_mutex_guard_t **legacy_guard_io,
6210-
cbm_private_file_lock_t **group_io, HANDLE *sentinel_io) {
6208+
static int win_participant_state_release(const cbm_daemon_ipc_endpoint_t *endpoint,
6209+
cbm_private_lock_directory_t *directory,
6210+
cbm_private_file_lock_t **startup_v2_io,
6211+
win_legacy_mutex_guard_t **legacy_guard_io,
6212+
cbm_private_file_lock_t **group_io, HANDLE *sentinel_io) {
62116213
if (!endpoint || !directory || !startup_v2_io || !legacy_guard_io || !group_io ||
62126214
!sentinel_io) {
6213-
return false;
6215+
return CBM_DAEMON_IPC_RELEASE_ERROR;
62146216
}
62156217
if (!*startup_v2_io) {
62166218
int startup = win_startup_v2_try_acquire(directory, startup_v2_io);
62176219
if (startup != 1) {
6218-
return false;
6220+
return startup;
62196221
}
62206222
}
62216223
if (!*legacy_guard_io) {
62226224
int legacy = win_legacy_mutex_guard_try_acquire(endpoint, legacy_guard_io);
62236225
if (legacy != 1) {
6224-
(void)cbm_private_file_lock_release(startup_v2_io);
6225-
return false;
6226+
if (cbm_private_file_lock_release(startup_v2_io) != CBM_PRIVATE_FILE_LOCK_OK) {
6227+
return CBM_DAEMON_IPC_RELEASE_ERROR;
6228+
}
6229+
return legacy;
62266230
}
62276231
}
62286232
if (!win_participant_group_release_locked(group_io, sentinel_io)) {
6229-
return false;
6233+
return CBM_DAEMON_IPC_RELEASE_ERROR;
62306234
}
62316235
if (!win_legacy_mutex_guard_release(legacy_guard_io)) {
6232-
return false;
6236+
return CBM_DAEMON_IPC_RELEASE_ERROR;
62336237
}
6234-
return cbm_private_file_lock_release(startup_v2_io) == CBM_PRIVATE_FILE_LOCK_OK;
6238+
return cbm_private_file_lock_release(startup_v2_io) == CBM_PRIVATE_FILE_LOCK_OK
6239+
? CBM_DAEMON_IPC_RELEASE_COMPLETE
6240+
: CBM_DAEMON_IPC_RELEASE_ERROR;
62356241
}
62366242

62376243
cbm_daemon_ipc_listener_t *cbm_daemon_ipc_listen_reserved(
@@ -7007,9 +7013,9 @@ bool cbm_daemon_ipc_startup_lock_release(cbm_daemon_ipc_startup_lock_t **lock_io
70077013
if (!lock) {
70087014
return true;
70097015
}
7010-
if (!win_participant_state_release(lock->endpoint, lock->directory, &lock->startup_v2_lock,
7011-
&lock->legacy_guard, &lock->group_lock,
7012-
&lock->legacy_sentinel)) {
7016+
if (win_participant_state_release(lock->endpoint, lock->directory, &lock->startup_v2_lock,
7017+
&lock->legacy_guard, &lock->group_lock,
7018+
&lock->legacy_sentinel) != CBM_DAEMON_IPC_RELEASE_COMPLETE) {
70137019
return false;
70147020
}
70157021
cbm_private_lock_directory_close(lock->directory);
@@ -7072,9 +7078,10 @@ bool cbm_daemon_ipc_participant_guard_release(cbm_daemon_ipc_participant_guard_t
70727078
if (!guard) {
70737079
return true;
70747080
}
7075-
if (!win_participant_state_release(
7076-
guard->endpoint, guard->directory, &guard->teardown_startup_v2_lock,
7077-
&guard->teardown_legacy_guard, &guard->group_lock, &guard->legacy_sentinel)) {
7081+
if (win_participant_state_release(guard->endpoint, guard->directory,
7082+
&guard->teardown_startup_v2_lock,
7083+
&guard->teardown_legacy_guard, &guard->group_lock,
7084+
&guard->legacy_sentinel) != CBM_DAEMON_IPC_RELEASE_COMPLETE) {
70787085
return false;
70797086
}
70807087
cbm_private_lock_directory_close(guard->directory);
@@ -7169,39 +7176,45 @@ bool cbm_daemon_ipc_local_transition_begin_work(cbm_daemon_ipc_local_transition_
71697176
return true;
71707177
}
71717178

7172-
bool cbm_daemon_ipc_local_transition_release(cbm_daemon_ipc_local_transition_t **transition_io) {
7179+
int cbm_daemon_ipc_local_transition_try_release(cbm_daemon_ipc_local_transition_t **transition_io) {
71737180
if (!transition_io) {
7174-
return false;
7181+
return CBM_DAEMON_IPC_RELEASE_ERROR;
71757182
}
71767183
cbm_daemon_ipc_local_transition_t *transition = *transition_io;
71777184
if (!transition) {
7178-
return true;
7185+
return CBM_DAEMON_IPC_RELEASE_COMPLETE;
71797186
}
71807187
if (!transition->endpoint || !transition->directory) {
7181-
return false;
7188+
return CBM_DAEMON_IPC_RELEASE_ERROR;
71827189
}
71837190
if (transition->group_lock || transition->legacy_sentinel != INVALID_HANDLE_VALUE ||
71847191
transition->teardown_legacy_guard) {
7185-
if (!win_participant_state_release(transition->endpoint, transition->directory,
7186-
&transition->startup_v2_lock,
7187-
&transition->teardown_legacy_guard,
7188-
&transition->group_lock, &transition->legacy_sentinel)) {
7189-
return false;
7192+
int released = win_participant_state_release(
7193+
transition->endpoint, transition->directory, &transition->startup_v2_lock,
7194+
&transition->teardown_legacy_guard, &transition->group_lock,
7195+
&transition->legacy_sentinel);
7196+
if (released != CBM_DAEMON_IPC_RELEASE_COMPLETE) {
7197+
return released;
71907198
}
71917199
} else if (transition->startup_v2_lock &&
71927200
cbm_private_file_lock_release(&transition->startup_v2_lock) !=
71937201
CBM_PRIVATE_FILE_LOCK_OK) {
7194-
return false;
7202+
return CBM_DAEMON_IPC_RELEASE_ERROR;
71957203
}
71967204
cbm_private_lock_directory_close(transition->directory);
71977205
transition->directory = NULL;
71987206
free(transition);
71997207
*transition_io = NULL;
7200-
return true;
7208+
return CBM_DAEMON_IPC_RELEASE_COMPLETE;
72017209
}
72027210

72037211
#endif /* _WIN32 */
72047212

7213+
bool cbm_daemon_ipc_local_transition_release(cbm_daemon_ipc_local_transition_t **transition_io) {
7214+
return cbm_daemon_ipc_local_transition_try_release(transition_io) ==
7215+
CBM_DAEMON_IPC_RELEASE_COMPLETE;
7216+
}
7217+
72057218
bool cbm_daemon_ipc_send_frame(cbm_daemon_ipc_connection_t *connection,
72067219
cbm_daemon_frame_type_t type, uint16_t flags, const void *payload,
72077220
uint32_t length) {

‎src/daemon/ipc.h‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,13 @@ bool cbm_daemon_ipc_local_transition_begin_work(cbm_daemon_ipc_local_transition_
320320
* Windows teardown reacquires startup-v2 and the legacy mutex, releases group
321321
* SH, closes this participant's sentinel last, then releases mutex and v2.
322322
* Success consumes and clears *transition_io; a retryable native cleanup
323-
* failure returns false and retains it. */
323+
* failure or startup-gate contention returns false and retains it. */
324324
bool cbm_daemon_ipc_local_transition_release(cbm_daemon_ipc_local_transition_t **transition_io);
325+
/* Nonblocking release: 1 consumes and clears *transition_io (also when already
326+
* empty), 0 retains it while another startup owns a required gate, and -1
327+
* retains it on an unsafe/native I/O failure. A NULL transition_io is invalid.
328+
* Callers can give healthy contention its own wait policy without extending
329+
* the deadline for repeated native cleanup failures. */
330+
int cbm_daemon_ipc_local_transition_try_release(cbm_daemon_ipc_local_transition_t **transition_io);
325331

326332
#endif /* CBM_DAEMON_IPC_H */

‎src/main.c‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,20 +1645,24 @@ static bool main_project_lock_manager_close(cbm_project_lock_manager_t **manager
16451645
}
16461646

16471647
static bool main_local_transition_close(cbm_daemon_ipc_local_transition_t **transition) {
1648-
uint64_t deadline = main_deadline_after(MAIN_COORDINATION_CLEANUP_MS);
1648+
uint64_t ceiling = main_deadline_after(MAIN_STARTUP_CONTENTION_CEILING_MS);
1649+
uint64_t error_deadline = main_deadline_after(MAIN_COORDINATION_CLEANUP_MS);
16491650
while (transition && *transition) {
1650-
/* A failed release always RETAINS the transition and is retriable by
1651-
* contract: the Windows release transition must briefly try-hold the
1652-
* shared startup-v2/legacy gates, so concurrent one-shot teardowns
1653-
* legitimately collide and succeed on retry. Success consumes the
1654-
* transition; only never-finishing cleanup is a failure, and the
1655-
* deadline escalation below owns that. */
1656-
(void)cbm_daemon_ipc_local_transition_release(transition);
1651+
/* Windows teardown reacquires the startup-v2/legacy gates before
1652+
* releasing its participant claim. A live startup owner can hold them
1653+
* beyond the native cleanup deadline. Retain protection and use the
1654+
* acquisition backstop for contention; repeated native errors still
1655+
* get only the short cleanup deadline. Success consumes the handle. */
1656+
int status = cbm_daemon_ipc_local_transition_try_release(transition);
16571657
if (*transition) {
1658-
if (cbm_now_ms() >= deadline) {
1658+
uint64_t now = cbm_now_ms();
1659+
if (now >= ceiling || (status < 0 && now >= error_deadline)) {
16591660
main_coordination_cleanup_fail_stop("local_transition_cleanup");
16601661
}
1661-
cbm_usleep(1000);
1662+
if (status == 0) {
1663+
error_deadline = main_deadline_after(MAIN_COORDINATION_CLEANUP_MS);
1664+
}
1665+
cbm_usleep(status == 0 ? 10000 : 1000);
16621666
}
16631667
}
16641668
return true;

‎tests/test_daemon_ipc.c‎

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2892,6 +2892,91 @@ TEST(daemon_ipc_local_participants_overlap_and_allow_modern_startup) {
28922892
PASS();
28932893
}
28942894

2895+
TEST(daemon_ipc_windows_local_transition_release_reports_live_gate_contention) {
2896+
#ifndef _WIN32
2897+
PASS();
2898+
#else
2899+
static const char key[] = "6060717182829393";
2900+
char parent[TEST_PATH_CAP] = {0};
2901+
char canonical_parent[TEST_PATH_CAP] = {0};
2902+
char runtime_dir[TEST_PATH_CAP] = {0};
2903+
char legacy_pipe[CBM_DAEMON_IPC_WINDOWS_NAME_CAP] = {0};
2904+
char legacy_startup[CBM_DAEMON_IPC_WINDOWS_NAME_CAP] = {0};
2905+
cbm_daemon_ipc_endpoint_t *endpoint = NULL;
2906+
cbm_daemon_ipc_local_transition_t *transition = NULL;
2907+
cbm_daemon_ipc_startup_lock_t *startup = NULL;
2908+
2909+
bool parent_ok = ipc_test_parent_new(parent, "local-release-busy") &&
2910+
ipc_test_full_path(canonical_parent, parent);
2911+
bool names_ok = parent_ok && cbm_daemon_ipc_windows_legacy_names(canonical_parent, key,
2912+
legacy_pipe, legacy_startup);
2913+
if (names_ok) {
2914+
endpoint = cbm_daemon_ipc_endpoint_new(key, parent);
2915+
}
2916+
if (endpoint) {
2917+
ipc_test_copy_path(runtime_dir, cbm_daemon_ipc_endpoint_runtime_dir(endpoint));
2918+
}
2919+
int acquired =
2920+
endpoint ? cbm_daemon_ipc_local_transition_try_acquire(endpoint, &transition) : -1;
2921+
int sealed = acquired == 1 ? cbm_daemon_ipc_local_transition_seal_legacy(transition) : -1;
2922+
int lifetime =
2923+
sealed == 1 ? cbm_daemon_ipc_local_transition_lifetime_probe(endpoint, transition) : -1;
2924+
bool begun = lifetime == 0 && cbm_daemon_ipc_local_transition_begin_work(transition);
2925+
int startup_acquired = begun ? cbm_daemon_ipc_startup_lock_try_acquire(endpoint, &startup) : -1;
2926+
int blocked = cbm_daemon_ipc_local_transition_try_release(&transition);
2927+
bool retained_while_busy = transition != NULL;
2928+
bool boolean_blocked = cbm_daemon_ipc_local_transition_release(&transition);
2929+
/* Keep the already-validated named mutex alive after the startup owner
2930+
* exits. Holding just this mutex exercises the second contention path. */
2931+
wchar_t *legacy_name = names_ok ? cbm_utf8_to_wide(legacy_startup) : NULL;
2932+
HANDLE legacy_mutex = startup_acquired == 1 && legacy_name
2933+
? OpenMutexW(SYNCHRONIZE | MUTEX_MODIFY_STATE, FALSE, legacy_name)
2934+
: NULL;
2935+
free(legacy_name);
2936+
bool startup_released = cbm_daemon_ipc_startup_lock_release(&startup);
2937+
DWORD mutex_wait =
2938+
startup_released && legacy_mutex ? WaitForSingleObject(legacy_mutex, 0) : WAIT_FAILED;
2939+
bool mutex_owned = mutex_wait == WAIT_OBJECT_0 || mutex_wait == WAIT_ABANDONED;
2940+
int legacy_blocked =
2941+
mutex_owned ? cbm_daemon_ipc_local_transition_try_release(&transition) : -1;
2942+
bool retained_while_legacy_busy = transition != NULL;
2943+
bool mutex_released = mutex_owned && ReleaseMutex(legacy_mutex);
2944+
if (legacy_mutex) {
2945+
(void)CloseHandle(legacy_mutex);
2946+
}
2947+
int legacy_retained = endpoint ? cbm_daemon_ipc_legacy_generation_probe(endpoint) : -1;
2948+
int released = cbm_daemon_ipc_local_transition_try_release(&transition);
2949+
bool consumed = transition == NULL;
2950+
int legacy_after = endpoint ? cbm_daemon_ipc_legacy_generation_probe(endpoint) : -1;
2951+
2952+
(void)cbm_daemon_ipc_startup_lock_release(&startup);
2953+
(void)cbm_daemon_ipc_local_transition_release(&transition);
2954+
cbm_daemon_ipc_endpoint_free(endpoint);
2955+
ipc_test_remove_tree(runtime_dir, parent);
2956+
2957+
ASSERT_TRUE(parent_ok);
2958+
ASSERT_TRUE(names_ok);
2959+
ASSERT_EQ(acquired, 1);
2960+
ASSERT_EQ(sealed, 1);
2961+
ASSERT_EQ(lifetime, 0);
2962+
ASSERT_TRUE(begun);
2963+
ASSERT_EQ(startup_acquired, 1);
2964+
ASSERT_EQ(blocked, 0);
2965+
ASSERT_TRUE(retained_while_busy);
2966+
ASSERT_FALSE(boolean_blocked);
2967+
ASSERT_TRUE(startup_released);
2968+
ASSERT_EQ(mutex_wait, WAIT_OBJECT_0);
2969+
ASSERT_EQ(legacy_blocked, 0);
2970+
ASSERT_TRUE(retained_while_legacy_busy);
2971+
ASSERT_TRUE(mutex_released);
2972+
ASSERT_EQ(legacy_retained, 1);
2973+
ASSERT_EQ(released, 1);
2974+
ASSERT_TRUE(consumed);
2975+
ASSERT_EQ(legacy_after, 0);
2976+
PASS();
2977+
#endif
2978+
}
2979+
28952980
TEST(daemon_ipc_windows_local_transition_release_retries_retained_mutex) {
28962981
#ifndef _WIN32
28972982
PASS();
@@ -2919,7 +3004,7 @@ TEST(daemon_ipc_windows_local_transition_release_retries_retained_mutex) {
29193004
if (begun) {
29203005
cbm_daemon_ipc_windows_legacy_guard_release_failures_set_for_test(1);
29213006
}
2922-
bool first_release = cbm_daemon_ipc_local_transition_release(&transition);
3007+
int first_release = cbm_daemon_ipc_local_transition_try_release(&transition);
29233008
bool retained_after_failure = transition != NULL;
29243009
bool retry_release = cbm_daemon_ipc_local_transition_release(&transition);
29253010
cbm_daemon_ipc_windows_legacy_guard_release_failures_set_for_test(0);
@@ -2935,7 +3020,7 @@ TEST(daemon_ipc_windows_local_transition_release_retries_retained_mutex) {
29353020
ASSERT_EQ(sealed, 1);
29363021
ASSERT_EQ(lifetime, 0);
29373022
ASSERT_TRUE(begun);
2938-
ASSERT_FALSE(first_release);
3023+
ASSERT_EQ(first_release, -1);
29393024
ASSERT_TRUE(retained_after_failure);
29403025
ASSERT_TRUE(retry_release);
29413026
ASSERT_NULL(transition);
@@ -5792,6 +5877,7 @@ SUITE(daemon_ipc) {
57925877
RUN_TEST(daemon_ipc_activation_probe_ignores_matching_startup_claim_only);
57935878
RUN_TEST(daemon_ipc_local_transition_coexists_with_active_daemon_lifetime);
57945879
RUN_TEST(daemon_ipc_local_participants_overlap_and_allow_modern_startup);
5880+
RUN_TEST(daemon_ipc_windows_local_transition_release_reports_live_gate_contention);
57955881
RUN_TEST(daemon_ipc_windows_local_transition_release_retries_retained_mutex);
57965882
RUN_TEST(daemon_ipc_windows_startup_release_retains_retry_authority);
57975883
RUN_TEST(daemon_ipc_frame_timeout_poisons_connection);

0 commit comments

Comments
 (0)