From 5d2acbe1fb78c5618523ccd6c390f5ac0516ffae Mon Sep 17 00:00:00 2001 From: Loic Coenen Date: Sun, 3 May 2026 21:14:30 +0000 Subject: [PATCH] fix: make autosave_running atomic to fix data race in thread shutdown Co-authored-by: aider (deepseek/deepseek-coder) --- fs.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/fs.c b/fs.c index 8a77692..8d8262f 100644 --- a/fs.c +++ b/fs.c @@ -10,6 +10,7 @@ #include #include #include +#include // ============================================================ // Auto-save thread @@ -18,7 +19,7 @@ static pthread_t autosave_thread; static pthread_mutex_t autosave_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t autosave_cond = PTHREAD_COND_INITIALIZER; -static volatile bool autosave_running = false; +static atomic_bool autosave_running = false; static volatile bool autosave_pending = false; static time_t last_autosave_time = 0; @@ -35,11 +36,11 @@ static void ensure_autosave_dir(void) { static void* autosave_thread_func(void *arg) { (void)arg; - while (autosave_running) { + while (atomic_load(&autosave_running)) { pthread_mutex_lock(&autosave_mutex); // Wait for trigger or shutdown - while (autosave_running && !autosave_pending) { + while (atomic_load(&autosave_running) && !autosave_pending) { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); ts.tv_sec += 5; // Wake up every 5 seconds to check @@ -101,14 +102,14 @@ static void* autosave_thread_func(void *arg) { } void fs_init(void) { - autosave_running = true; + atomic_store(&autosave_running, true); autosave_pending = false; ensure_autosave_dir(); pthread_create(&autosave_thread, NULL, autosave_thread_func, NULL); } void fs_cleanup(void) { - autosave_running = false; + atomic_store(&autosave_running, false); pthread_cond_signal(&autosave_cond); pthread_join(autosave_thread, NULL); }