@@ -203,6 +203,32 @@ void cbm_runtime_settings_note_http_port(int port) {
203203 atomic_store_explicit (& active_http_port , port , memory_order_relaxed );
204204}
205205
206+ /* A commit holds _config.db's lock for milliseconds, so its release is an event
207+ * that always comes; waiting for it is not a race. Failing on the lock instead
208+ * (SQLite's default, or startup's former 50 ms window) let one process's routine
209+ * commit fail another: a daemon saving its UI configuration made unrelated CLI
210+ * starts exit with "could not load saved runtime settings", and a concurrent
211+ * commit made the daemon's own UI save fail. The bound only catches a wedged
212+ * holder. Each busy wait is logged once (SQLite restarts the attempt count for
213+ * every lock attempt), so a test that holds the lock can observe the waiter
214+ * instead of guessing a duration. */
215+ enum { SETTINGS_BUSY_TIMEOUT_MS = 3000 , SETTINGS_BUSY_STEP_MS = 10 };
216+
217+ static int settings_busy_wait (void * context , int attempts ) {
218+ (void )context ;
219+ if (attempts == 0 )
220+ cbm_log_warn ("config_db.busy_wait" , "action" , "waiting for a settings writer" );
221+ if ((long long )attempts * SETTINGS_BUSY_STEP_MS >= SETTINGS_BUSY_TIMEOUT_MS )
222+ return 0 ;
223+ sqlite3_sleep (SETTINGS_BUSY_STEP_MS );
224+ return 1 ;
225+ }
226+
227+ void cbm_runtime_settings_wait_for_writers (sqlite3 * db ) {
228+ if (db )
229+ sqlite3_busy_handler (db , settings_busy_wait , NULL );
230+ }
231+
206232static bool canonical_cache_path (const char * cache , char out [SETTINGS_PATH_CAP ], bool create ) {
207233 if (!cache || !cache [0 ])
208234 return false;
@@ -234,7 +260,7 @@ static sqlite3 *settings_open(const char *cache) {
234260 sqlite3_close (db );
235261 return NULL ;
236262 }
237- sqlite3_busy_timeout (db , 3000 );
263+ cbm_runtime_settings_wait_for_writers (db );
238264 const char * schema =
239265 "BEGIN IMMEDIATE;"
240266 "CREATE TABLE IF NOT EXISTS runtime_settings(key TEXT PRIMARY KEY,value TEXT NOT NULL);"
@@ -442,6 +468,65 @@ void cbm_runtime_settings_reset_for_tests(void) {
442468}
443469#endif
444470
471+ /* A process killed mid-write leaves a hot rollback journal, and a read-only
472+ * connection cannot roll it back (SQLITE_READONLY_ROLLBACK): every later start
473+ * failed with "could not load saved runtime settings" until something opened
474+ * the database read-write, and nothing did. One read-write open that reads the
475+ * schema makes SQLite roll the interrupted write back. A journal is hot only
476+ * while no live writer holds its lock, so this never undoes a write in
477+ * progress; a live writer takes the busy path instead. */
478+ static bool settings_recover_hot_journal (const char * path ) {
479+ sqlite3 * db = NULL ;
480+ bool recovered = sqlite3_open_v2 (path , & db , SQLITE_OPEN_READWRITE , NULL ) == SQLITE_OK ;
481+ if (recovered ) {
482+ cbm_runtime_settings_wait_for_writers (db );
483+ recovered =
484+ sqlite3_exec (db , "SELECT count(*) FROM sqlite_master" , NULL , NULL , NULL ) == SQLITE_OK ;
485+ }
486+ if (db )
487+ sqlite3_close (db );
488+ char journal [SETTINGS_PATH_CAP + 16 ];
489+ (void )snprintf (journal , sizeof (journal ), "%s-journal" , path );
490+ if (recovered )
491+ cbm_log_warn ("config_db.hot_journal_recovered" , "journal" , journal , "action" ,
492+ "rolled back an interrupted settings write" );
493+ else
494+ /* Still fatal for the caller: without the rollback the saved settings
495+ * cannot be read consistently, and guessing them is worse. */
496+ cbm_log_error ("config_db.hot_journal_unrecovered" , "journal" , journal , "hint" ,
497+ "an interrupted settings write could not be rolled back; make the "
498+ "settings directory writable and retry" );
499+ return recovered ;
500+ }
501+
502+ /* Read-only connection holding one read snapshot (BEGIN + a schema read), or
503+ * NULL. A writer mid-commit is waited out; a database still locked after the
504+ * bound is an error, not an absent override. */
505+ static sqlite3 * settings_open_snapshot (const char * path , bool * hot_journal ) {
506+ * hot_journal = false;
507+ sqlite3 * db = NULL ;
508+ if (sqlite3_open_v2 (path , & db , SQLITE_OPEN_READONLY , NULL ) != SQLITE_OK ) {
509+ if (db )
510+ sqlite3_close (db );
511+ return NULL ;
512+ }
513+ cbm_runtime_settings_wait_for_writers (db );
514+ if (sqlite3_exec (db , "BEGIN; SELECT count(*) FROM sqlite_master" , NULL , NULL , NULL ) ==
515+ SQLITE_OK )
516+ return db ;
517+ * hot_journal = sqlite3_extended_errcode (db ) == SQLITE_READONLY_ROLLBACK ;
518+ sqlite3_close (db );
519+ return NULL ;
520+ }
521+
522+ static sqlite3 * settings_read_snapshot (const char * path ) {
523+ bool hot_journal = false;
524+ sqlite3 * db = settings_open_snapshot (path , & hot_journal );
525+ if (!db && hot_journal && settings_recover_hot_journal (path ))
526+ db = settings_open_snapshot (path , & hot_journal );
527+ return db ;
528+ }
529+
445530bool cbm_runtime_settings_init (const char * cache ) {
446531 char canonical [SETTINGS_PATH_CAP ];
447532 if (!canonical_cache_path (cache , canonical , true))
@@ -453,20 +538,12 @@ bool cbm_runtime_settings_init(const char *cache) {
453538 return false;
454539 sqlite3 * db = NULL ;
455540 if (cbm_file_exists (path )) {
456- if (sqlite3_open_v2 (path , & db , SQLITE_OPEN_READONLY , NULL ) != SQLITE_OK ) {
457- if (db )
458- sqlite3_close (db );
541+ /* The snapshot read happens before individual optional tables are
542+ * queried; failing here exits the whole process. Hook clients skip
543+ * this init. */
544+ db = settings_read_snapshot (path );
545+ if (!db )
459546 return false;
460- }
461- /* Startup, especially hook clients, must never wait on a schema writer.
462- * This read forces the shared snapshot before individual optional tables
463- * are queried; a busy database is an error, not an absent override. */
464- sqlite3_busy_timeout (db , 50 );
465- if (sqlite3_exec (db , "BEGIN; SELECT count(*) FROM sqlite_master" , NULL , NULL , NULL ) !=
466- SQLITE_OK ) {
467- sqlite3_close (db );
468- return false;
469- }
470547 }
471548 for (size_t i = 0 ; i < SETTINGS_COUNT ; i ++ ) {
472549 bool saved = false;
@@ -640,15 +717,18 @@ static char *error_json(const char *message, int code, int *status) {
640717 return json ;
641718}
642719
720+ /* An unchanged value is not rewritten: the UPDATE would still fire the revision
721+ * trigger, so every `daemon start` on a UI build committed (journal + fsync) and
722+ * locked out concurrent readers although nothing changed. */
643723static bool write_value (sqlite3 * db , const setting_t * setting , const char * value ) {
644724 const char * sql ;
645725 if (setting -> storage == SET_CONFIG )
646726 sql = value ? "INSERT INTO config(key,value) VALUES(?,?) ON CONFLICT(key) DO UPDATE SET "
647- "value=excluded.value"
727+ "value=excluded.value WHERE value IS NOT excluded.value "
648728 : "DELETE FROM config WHERE key=?" ;
649729 else
650730 sql = value ? "INSERT INTO runtime_settings(key,value) VALUES(?,?) ON CONFLICT(key) DO "
651- "UPDATE SET value=excluded.value"
731+ "UPDATE SET value=excluded.value WHERE value IS NOT excluded.value "
652732 : "DELETE FROM runtime_settings WHERE key=?" ;
653733 sqlite3_stmt * statement = NULL ;
654734 if (sqlite3_prepare_v2 (db , sql , -1 , & statement , NULL ) != SQLITE_OK )
@@ -759,13 +839,12 @@ void cbm_runtime_settings_load_ui(bool *enabled, int *port) {
759839 char path [SETTINGS_PATH_CAP ];
760840 if (snprintf (path , sizeof (path ), "%s/_config.db" , canonical ) >= (int )sizeof (path ))
761841 return ;
762- sqlite3 * db = NULL ;
763- if (sqlite3_open_v2 (path , & db , SQLITE_OPEN_READONLY , NULL ) != SQLITE_OK ) {
764- if (db )
765- sqlite3_close (db );
842+ /* Without the wait and the hot-journal recovery, a concurrent commit or an
843+ * interrupted write made every read fail and the caller silently kept its
844+ * defaults. */
845+ sqlite3 * db = settings_read_snapshot (path );
846+ if (!db )
766847 return ;
767- }
768- sqlite3_exec (db , "BEGIN" , NULL , NULL , NULL );
769848 for (size_t i = 0 ; i < SETTINGS_COUNT ; i ++ )
770849 if (settings [i ].storage == SET_UI ) {
771850 char value [SETTINGS_VALUE_CAP ];
0 commit comments