Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix(handlers): current-tab LRU evicted at random inside one clock tick

Darshan Jainb8731a28d07e
diff --git a/internal/handlers/current_tab.go b/internal/handlers/current_tab.go
index fad74870..48a02914 100644
--- a/internal/handlers/current_tab.go
+++ b/internal/handlers/current_tab.go
@@ -5,7 +5,6 @@ import (
"net/http"
"strings"
"sync"
- "time"
"github.com/pinchtab/pinchtab/internal/activity"
"github.com/pinchtab/pinchtab/internal/session"
@@ -42,12 +41,21 @@ func (s currentTabScope) Description() string {
// keeps that bounded.
const defaultCurrentTabCap = 5000
-// currentTabEntry holds a tab id plus a per-entry timestamp used as the
-// LRU recency signal. lastTouched is updated on Get, Set, and any access
-// that proves the entry is still in active use.
+// currentTabEntry holds a tab id plus a per-entry tick used as the LRU
+// recency signal. touchedAt is updated on Get, Set, and any access that
+// proves the entry is still in active use.
+//
+// It counts touches rather than reading a clock. Wall time cannot order two
+// touches that land inside the same clock tick, and the tick is coarse on
+// Windows — ~15.6ms, long enough to cover a burst of current-tab traffic.
+// Entries touched within one tick all compared equal, no entry was Before any
+// other, and eviction fell through to Go's randomized map iteration order:
+// the store stopped being an LRU and dropped an arbitrary agent's pointer
+// while a genuinely older one survived. A counter is exactly ordered by
+// construction and needs no clock at all.
type currentTabEntry struct {
- tabID string
- lastTouched time.Time
+ tabID string
+ touchedAt uint64
}
// CurrentTabStore tracks server-side current-tab pointers for identified
@@ -57,14 +65,13 @@ type CurrentTabStore struct {
mu sync.RWMutex
entries map[string]currentTabEntry
cap int
- now func() time.Time
+ touches uint64
}
func NewCurrentTabStore() *CurrentTabStore {
return &CurrentTabStore{
entries: make(map[string]currentTabEntry),
cap: defaultCurrentTabCap,
- now: time.Now,
}
}
@@ -90,7 +97,7 @@ func (s *CurrentTabStore) Get(scope currentTabScope) (string, bool) {
s.mu.Unlock()
return "", false
}
- entry.lastTouched = s.now()
+ entry.touchedAt = s.nextTouchLocked()
s.entries[scope.key] = entry
s.mu.Unlock()
return entry.tabID, true
@@ -105,7 +112,7 @@ func (s *CurrentTabStore) Set(scope currentTabScope, tabID string) {
return
}
s.mu.Lock()
- s.entries[scope.key] = currentTabEntry{tabID: tabID, lastTouched: s.now()}
+ s.entries[scope.key] = currentTabEntry{tabID: tabID, touchedAt: s.nextTouchLocked()}
s.evictExcessLocked()
s.mu.Unlock()
}
@@ -121,13 +128,13 @@ func (s *CurrentTabStore) evictExcessLocked() {
for len(s.entries) > s.cap {
var (
oldestKey string
- oldestAt time.Time
+ oldestAt uint64
first = true
)
for k, e := range s.entries {
- if first || e.lastTouched.Before(oldestAt) {
+ if first || e.touchedAt < oldestAt {
oldestKey = k
- oldestAt = e.lastTouched
+ oldestAt = e.touchedAt
first = false
}
}
@@ -135,6 +142,13 @@ func (s *CurrentTabStore) evictExcessLocked() {
}
}
+// nextTouchLocked returns a strictly increasing recency tick. Caller must hold
+// s.mu (write).
+func (s *CurrentTabStore) nextTouchLocked() uint64 {
+ s.touches++
+ return s.touches
+}
+
func (s *CurrentTabStore) Clear(scope currentTabScope) {
if s == nil || scope.IsGlobal() || scope.key == "" {
return
diff --git a/internal/handlers/current_tab_test.go b/internal/handlers/current_tab_test.go
index 3bfd743e..90c67f8b 100644
--- a/internal/handlers/current_tab_test.go
+++ b/internal/handlers/current_tab_test.go
@@ -304,3 +304,70 @@ func TestUntrustedSessionHeaderIgnored(t *testing.T) {
t.Fatalf("untrusted header must not resolve to %q", got)
}
}
+
+// TestCurrentTabStore_EvictsInTouchOrderWithoutSleeping pins the invariant the
+// wall-clock recency signal could not hold: eviction order must follow the
+// order entries were touched, not how fast they were touched.
+//
+// The old signal was time.Now(). Every touch here lands inside a single clock
+// tick — ~15.6ms on Windows, wide enough for far more traffic than this — so
+// every entry compared equal, no entry was Before any other, and eviction fell
+// through to Go's randomized map iteration. Entries survived or died at random
+// while the store still claimed to be an LRU.
+//
+// No sleeps: a test that has to pause to let a clock advance is measuring the
+// clock, and the store must not need one.
+func TestCurrentTabStore_EvictsInTouchOrderWithoutSleeping(t *testing.T) {
+ const (
+ capacity = 10
+ total = 50
+ )
+ store := NewCurrentTabStore()
+ store.SetCap(capacity)
+
+ scopes := make([]currentTabScope, total)
+ for i := range scopes {
+ scopes[i] = scopedCurrentTab(currentTabScopeAgent, fmt.Sprintf("agent-%02d", i))
+ store.Set(scopes[i], fmt.Sprintf("tab-%02d", i))
+ }
+
+ // Exactly the last `capacity` writes survive; everything older is gone.
+ for i := 0; i < total-capacity; i++ {
+ if _, ok := store.Get(scopes[i]); ok {
+ t.Fatalf("scope %d should have been evicted; only the newest %d survive", i, capacity)
+ }
+ }
+ for i := total - capacity; i < total; i++ {
+ if _, ok := store.Get(scopes[i]); !ok {
+ t.Fatalf("scope %d should have survived; only the oldest %d are evicted", i, total-capacity)
+ }
+ }
+}
+
+// Get is a touch, so reading an entry has to move it out of eviction's way even
+// when every read happens within one clock tick.
+func TestCurrentTabStore_GetBumpsRecencyWithoutSleeping(t *testing.T) {
+ const capacity = 4
+ store := NewCurrentTabStore()
+ store.SetCap(capacity)
+
+ scopes := make([]currentTabScope, capacity)
+ for i := range scopes {
+ scopes[i] = scopedCurrentTab(currentTabScopeAgent, fmt.Sprintf("agent-%d", i))
+ store.Set(scopes[i], fmt.Sprintf("tab-%d", i))
+ }
+
+ // Touch the oldest entry, then push the cap. The entry just read must
+ // outlive the one that has not been touched since it was written.
+ if _, ok := store.Get(scopes[0]); !ok {
+ t.Fatal("scope 0 should still be present before the bump")
+ }
+ store.Set(scopedCurrentTab(currentTabScopeAgent, "agent-new"), "tab-new")
+
+ if _, ok := store.Get(scopes[0]); !ok {
+ t.Error("the entry Get touched was evicted; Get did not bump recency")
+ }
+ if _, ok := store.Get(scopes[1]); ok {
+ t.Error("scope 1 was the least recently used and should have been evicted")
+ }
+}