From 3b8b14b06c21c97c190a888a939848056702b51a Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Mon, 23 Nov 2026 10:27:47 -0500 Subject: [PATCH] fix(session): FEX and AppImage extraction no longer hang at start-up (#3070) The clock pause preload opened its control file on the first monotonic clock read. A program whose allocator reads the clock while holding its own lock, as FEX does while it starts, re-entered that allocator through the open wrappers and deadlocked. The control file is now opened only in the preload's constructor, so a clock read never opens a file. --- tools/linuxfs/preload/clockpause.c | 16 +------ tools/tests/test_clock_pause_preload.py | 57 +++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/tools/linuxfs/preload/clockpause.c b/tools/linuxfs/preload/clockpause.c index 8fe0b7ce77..70b4179329 100644 --- a/tools/linuxfs/preload/clockpause.c +++ b/tools/linuxfs/preload/clockpause.c @@ -25,7 +25,6 @@ #define CP_FDS 65536 static const int64_t *cp_shift; -static int cp_state; static size_t cp_cond_byte; static unsigned char cp_cond_mask; static unsigned char cp_fds[CP_FDS / 8]; @@ -94,25 +93,14 @@ static void cp_init(void) { } static int64_t cp_offset(void) { - const int64_t *shift; - int state = __atomic_load_n(&cp_state, __ATOMIC_ACQUIRE); - if (state == 0) { - int expected = 0; - if (__atomic_compare_exchange_n(&cp_state, &expected, 1, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) { - cp_init(); - __atomic_store_n(&cp_state, 2, __ATOMIC_RELEASE); - } - return 0; - } - if (state != 2) return 0; - shift = __atomic_load_n(&cp_shift, __ATOMIC_ACQUIRE); + const int64_t *shift = __atomic_load_n(&cp_shift, __ATOMIC_ACQUIRE); if (!shift) return 0; int64_t value = __atomic_load_n(shift, __ATOMIC_RELAXED); return value > 0 ? value : 0; } __attribute__((constructor)) static void cp_start(void) { - cp_offset(); + cp_init(); } static int cp_mono(clockid_t clock) { diff --git a/tools/tests/test_clock_pause_preload.py b/tools/tests/test_clock_pause_preload.py index 3d3f0dd76b..7b92a444bc 100644 --- a/tools/tests/test_clock_pause_preload.py +++ b/tools/tests/test_clock_pause_preload.py @@ -107,6 +107,51 @@ int main(void) { } """ +EARLY_LIBRARY = r""" +#include +#include + +pthread_mutex_t early_lock = PTHREAD_MUTEX_INITIALIZER; + +__attribute__((constructor)) static void early_start(void) { + struct timespec ts; + pthread_mutex_lock(&early_lock); + clock_gettime(CLOCK_MONOTONIC, &ts); + pthread_mutex_unlock(&early_lock); +} +""" + +EARLY_PROGRAM = r""" +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +extern pthread_mutex_t early_lock; + +int open(const char *path, int flags, ...) { + va_list args; + va_start(args, flags); + mode_t mode = (mode_t)va_arg(args, int); + va_end(args); + pthread_mutex_lock(&early_lock); + pthread_mutex_unlock(&early_lock); + return (int)syscall(SYS_openat, AT_FDCWD, path, flags, mode); +} + +int main(void) { + struct timespec mono, boot; + clock_gettime(CLOCK_MONOTONIC, &mono); + clock_gettime(CLOCK_BOOTTIME, &boot); + printf("gap %lld\n", (long long)(((boot.tv_sec - mono.tv_sec) * 1000000000LL + boot.tv_nsec - mono.tv_nsec) / 1000000LL)); + return 0; +} +""" + SHIFT_MS = 10000 @@ -128,6 +173,13 @@ class ClockPausePreloadTest(unittest.TestCase): subprocess.run(["cc", "-O2", "-pthread", "-o", str(cls.shell), str(build / "program.c")], check=True) cls.other = build / "kwin_wayland" shutil.copy(cls.shell, cls.other) + (build / "early.c").write_text(EARLY_LIBRARY) + (build / "early_program.c").write_text(EARLY_PROGRAM) + subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-pthread", "-o", str(build / "libearly.so"), + str(build / "early.c")], check=True) + cls.early = build / "early_program" + subprocess.run(["cc", "-O2", "-pthread", "-o", str(cls.early), str(build / "early_program.c"), + "-L", str(build), "-learly", "-Wl,-rpath," + str(build)], check=True) @classmethod def tearDownClass(cls): @@ -153,6 +205,11 @@ class ClockPausePreloadTest(unittest.TestCase): self.assertGreaterEqual(paused[name], 50, name) self.assertLess(paused[name], 1000, name) + def test_clock_read_inside_a_library_constructor_does_not_reenter_the_program(self): + plain = self.run_program(self.early, 0) + paused = self.run_program(self.early, SHIFT_MS) + self.assertAlmostEqual(paused["gap"] - plain["gap"], SHIFT_MS, delta=50) + def test_program_without_preload_keeps_the_real_clock(self): plain = self.run_program(self.other, 0, preload=False) paused = self.run_program(self.other, SHIFT_MS, preload=False)