]> git.hungrycats.org Git - bees/commitdiff
roots: sleep before the first crawl state writeback
authorZygo Blaxell <bees@furryterror.org>
Sat, 10 Oct 2026 23:54:05 +0000 (19:54 -0400)
committerZygo Blaxell <bees@furryterror.org>
Sun, 11 Oct 2026 00:48:00 +0000 (20:48 -0400)
The writeback thread saved beespoint.ini as soon as it started, before
any crawler existed, and logged "Crawl state empty!" at every startup.
Sleep for the checkpoint interval first.  A stop during the sleep still
flushes the crawl state.

Assisted-by: Claude-Code:claude-opus-5-5
src/bees-roots.cc

index 75f1a4db8d360a9f87ff10c6da39808727a3e11b..332d81c02faf5c0f9c5af46a5bcec0a22dc1f78c 100644 (file)
@@ -2727,12 +2727,9 @@ BeesRoots::crawl_thread()
 void
 BeesRoots::writeback_thread()
 {
+       // Sleep before the first save.  At startup the crawlers are not
+       // created yet, so an immediate save has nothing to write.
        while (true) {
-               catch_all([&]() {
-                       BEESNOTE("saving crawler state");
-                       save_point();
-               });
-
                // Report the deferred-checkpoint backlog for the duration of the
                // sleep below.  A deep backlog is normal, not a fault: entries are
                // released one full hash table sweep after they are queued, and the
@@ -2746,16 +2743,25 @@ BeesRoots::writeback_thread()
                }
                BEESNOTE("idle, " << pending_count << " checkpoints pending");
 
-               unique_lock<mutex> lock(m_stop_mutex);
-               if (m_stop_requested) {
-                       BEESLOGDEBUG("Stop requested in writeback thread");
-                       catch_all([&]() {
-                               BEESNOTE("flushing crawler state");
-                               save_point();
-                       });
-                       return;
+               {
+                       unique_lock<mutex> lock(m_stop_mutex);
+                       if (!m_stop_requested) {
+                               m_stop_condvar.wait_for(lock, chrono::duration<double>(m_checkpoint_interval));
+                       }
+                       if (m_stop_requested) {
+                               BEESLOGDEBUG("Stop requested in writeback thread");
+                               catch_all([&]() {
+                                       BEESNOTE("flushing crawler state");
+                                       save_point();
+                               });
+                               return;
+                       }
                }
-               m_stop_condvar.wait_for(lock, chrono::duration<double>(m_checkpoint_interval));
+
+               catch_all([&]() {
+                       BEESNOTE("saving crawler state");
+                       save_point();
+               });
        }
 }