Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(daemon): stop concurrent tasks evicting each other's OpenClaw discovery cache (#7204)

Bohan Jiang728ba9c58e2f
diff --git a/server/internal/daemon/execenv/openclaw_config_cache.go b/server/internal/daemon/execenv/openclaw_config_cache.go
index 183f5b3388..09bff0c32b 100644
--- a/server/internal/daemon/execenv/openclaw_config_cache.go
+++ b/server/internal/daemon/execenv/openclaw_config_cache.go
@@ -51,6 +51,22 @@ const (
// config file. The TTL is the backstop that keeps those visible, at the
// cost of re-running discovery at most once a minute.
openclawDiscoveryCacheTTL = time.Minute
+
+ // openclawDiscoveryCacheFutureSkew is how far ahead of the reader's clock
+ // sample an entry may be dated and still count as fresh.
+ //
+ // Without it, tasks starting together on one daemon evict each other's
+ // work. Every caller samples time.Now() once and passes it down, so a peer
+ // that sampled later but committed its rename first leaves an entry dated
+ // microseconds "in the future" — and the reader, whose own store just
+ // succeeded, throws it away and re-runs discovery for nothing. The same
+ // interleaving is what made TestOpenclawDiscoveryCacheConcurrentPreparations
+ // flake in CI.
+ //
+ // One second is far above that window and far below what the guard below
+ // exists to catch. It also bounds the cost of being wrong: an entry dated
+ // into the future outlives its TTL by at most this much.
+ openclawDiscoveryCacheFutureSkew = time.Second
)
// openclawDiscoveryFingerprint is the "is this entry still true?" evidence
@@ -208,10 +224,13 @@ func loadOpenclawDiscoveryCache(cachePath, bin string, now time.Time) (openclawD
return openclawDiscoveryCacheEntry{}, false
}
age := now.Sub(time.Unix(0, entry.CachedAtNano))
- // A negative age means the entry was written in the future — a clock jump
- // or a hand-edited file. Treat it as a miss rather than trusting it for a
- // TTL that may never expire.
- if age < 0 || age > openclawDiscoveryCacheTTL {
+ // An age well below zero means the entry was written in the future — a
+ // clock jump or a hand-edited file. Treat it as a miss rather than trusting
+ // it for a TTL that may never expire. Sub-second future dating is not that:
+ // it is the ordinary result of a concurrent writer on this same daemon
+ // (see openclawDiscoveryCacheFutureSkew), and rejecting it only buys an
+ // unnecessary discovery run.
+ if age < -openclawDiscoveryCacheFutureSkew || age > openclawDiscoveryCacheTTL {
return openclawDiscoveryCacheEntry{}, false
}
current, err := buildOpenclawDiscoveryFingerprint(bin, entry.ActiveConfigPath)
diff --git a/server/internal/daemon/execenv/openclaw_config_cache_test.go b/server/internal/daemon/execenv/openclaw_config_cache_test.go
index d66c2e7d6f..49853bb74b 100644
--- a/server/internal/daemon/execenv/openclaw_config_cache_test.go
+++ b/server/internal/daemon/execenv/openclaw_config_cache_test.go
@@ -291,6 +291,38 @@ func TestOpenclawDiscoveryCacheSkipsFailedDiscovery(t *testing.T) {
}
}
+// TestOpenclawDiscoveryCacheFutureDatedEntry pins both edges of the
+// future-dating rule.
+//
+// Callers sample time.Now() once and pass it down, so on a daemon running
+// several tasks at once a peer routinely commits an entry stamped after the
+// reader's sample. That entry is completely valid — rejecting it just costs an
+// extra discovery run, and it is what made the concurrency test below flake.
+// A real clock jump still has to be caught, since an entry dated far ahead
+// would otherwise never age out of its TTL.
+func TestOpenclawDiscoveryCacheFutureDatedEntry(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ ahead time.Duration
+ want bool
+ }{
+ {"concurrent writer, microseconds ahead", 500 * time.Microsecond, true},
+ {"just inside the tolerance", openclawDiscoveryCacheFutureSkew - time.Millisecond, true},
+ {"clock jump, well past the tolerance", openclawDiscoveryCacheFutureSkew + time.Minute, false},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ f := newOpenclawCacheFixture(t)
+ readerNow := time.Now()
+ if err := storeOpenclawDiscoveryCache(f.cachePath(), f.bin, f.configPath, []any{map[string]any{"id": "scout"}}, false, readerNow.Add(tc.ahead)); err != nil {
+ t.Fatalf("store: %v", err)
+ }
+ if _, ok := loadOpenclawDiscoveryCache(f.cachePath(), f.bin, readerNow); ok != tc.want {
+ t.Fatalf("entry dated %v ahead of the reader: hit = %t, want %t", tc.ahead, ok, tc.want)
+ }
+ })
+ }
+}
+
// TestOpenclawDiscoveryCacheConcurrentPreparations covers several tasks
// starting at once on the same daemon: the writes are atomic, so every task
// either sees a complete old entry or a complete new one, and none of them