From e8b8ee17faa4742162aa682aa5abbcebc592854e Mon Sep 17 00:00:00 2001 From: Hikari Date: Fri, 6 Feb 2026 22:41:00 -0800 Subject: [PATCH] docs: update DEBUGGING.md with config race condition fix --- DEBUGGING.md | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/DEBUGGING.md b/DEBUGGING.md index 1edfc92..1106671 100644 --- a/DEBUGGING.md +++ b/DEBUGGING.md @@ -62,8 +62,37 @@ console.log("[Tauri Listener] message"); - Config file gets reset to defaults - Settings don't persist between sessions - Model selection is lost +- Config appears to work, then randomly resets -**POTENTIAL CAUSES:** +**ACTUAL ROOT CAUSE (FIXED!):** + +The config store had a **critical race condition bug** in `src/lib/stores/config.ts`. The pattern used everywhere was: + +```typescript +// ❌ BUGGY CODE +let currentConfig = defaultConfig; +config.subscribe((c) => (currentConfig = c))(); // <-- Immediate unsubscribe! +return currentConfig; +``` + +**Why this caused config loss:** +- The `()` at the end immediately unsubscribes from the store +- This creates a race condition - sometimes you get the value, sometimes you don't +- When it fails, it returns `defaultConfig` instead of your actual config +- This is why your settings would randomly disappear! + +**The fix:** +```typescript +// ✅ CORRECT CODE +function getCurrentConfig(): HikariConfig { + let currentConfig: HikariConfig = defaultConfig; + const unsubscribe = config.subscribe((c) => (currentConfig = c)); + unsubscribe(); // <-- Proper cleanup after getting value + return currentConfig; +} +``` + +**POTENTIAL CAUSES (if the above is fixed):** ### 1. Missing `#[serde(default)]` Attribute