Make sure we can't hang a process on exit.
@@ -990,11 +990,24 @@ handleExit(void)
g_exitdone = TRUE;
if (!atomicCasU64(&reentrancy_guard, 0ULL, 1ULL)) {
+
+ // Regardless of whether TLS is being used, we need an upper
+ // bound for how long we'll hold off a process's exit.
+ // This exists as a safeguard to prevent a hang or crash
+ // of our periodic thread from hanging a process at exit.
+ struct timespec expiration_time, current_time = {0};
+ clock_gettime(CLOCK_MONOTONIC, &expiration_time);
+ expiration_time.tv_sec += MAX_TLS_CONNECT_SECONDS + 1;
+
struct timespec ts = {.tv_sec = 0, .tv_nsec = 10000}; // 10 us
// let the periodic thread finish
while (!atomicCasU64(&reentrancy_guard, 0ULL, 1ULL)) {
sigSafeNanosleep(&ts);
+ clock_gettime(CLOCK_MONOTONIC, &current_time);
+ if (current_time.tv_sec > expiration_time.tv_sec) {
+ return; // We can't safely do anything else (below)
+ }
}
}
