Skip to content

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

fix(tabs): RemoveTab let a second task onto a tab that was still busy

Darshan Jain478e1d1901fa
diff --git a/internal/bridge/tab_executor_integration_test.go b/internal/bridge/tab_executor_integration_test.go
index 8ce3bad8..7890aa0b 100644
--- a/internal/bridge/tab_executor_integration_test.go
+++ b/internal/bridge/tab_executor_integration_test.go
@@ -311,3 +311,102 @@ func TestTabExecutor_StatsUnderLoad(t *testing.T) {
wg.Wait()
}
+
+// peakTracker records the highest number of tasks seen inside a tab at once.
+// One task at a time is the whole promise of a per-tab gate, so the peak is the
+// property worth measuring; a count of completions is not.
+type peakTracker struct {
+ inFlight int64
+ peak int64
+}
+
+func (p *peakTracker) enter() {
+ n := atomic.AddInt64(&p.inFlight, 1)
+ for {
+ old := atomic.LoadInt64(&p.peak)
+ if n <= old || atomic.CompareAndSwapInt64(&p.peak, old, n) {
+ return
+ }
+ }
+}
+
+func (p *peakTracker) leave() { atomic.AddInt64(&p.inFlight, -1) }
+
+func (p *peakTracker) Peak() int64 { return atomic.LoadInt64(&p.peak) }
+
+// RemoveTab must not let a second task onto a tab that is still busy.
+//
+// The entry used to be deleted before the drain, so a caller arriving during it
+// found nothing in the map, made a second mutex for the same tab, and ran
+// alongside the task the first mutex was holding. Measured before the fix: two
+// tasks inside one tab at once.
+//
+// TestTabExecutor_ConcurrentRemoveAndExecute already covers this shape and
+// passed throughout, because it asks whether the executor deadlocks or races
+// the map — liveness — and runs a single Execute per tab, so mutual exclusion
+// is not observable in it. This asks the safety question instead: how many
+// tasks were inside at the same time.
+func TestRemoveTabDoesNotAdmitASecondTaskToABusyTab(t *testing.T) {
+ te := NewTabExecutor(8)
+ var peak peakTracker
+
+ blocking := func(hold <-chan struct{}) func(context.Context) error {
+ return func(context.Context) error {
+ peak.enter()
+ defer peak.leave()
+ <-hold
+ return nil
+ }
+ }
+
+ holdFirst := make(chan struct{})
+ firstDone := make(chan struct{})
+ go func() {
+ _ = te.Execute(context.Background(), "tab1", blocking(holdFirst))
+ close(firstDone)
+ }()
+ waitFor(t, func() bool { return atomic.LoadInt64(&peak.inFlight) == 1 }, "first task to start")
+
+ // RemoveTab blocks draining the first task; the second arrives mid-drain.
+ removed := make(chan struct{})
+ go func() { te.RemoveTab("tab1"); close(removed) }()
+ // The second caller has to arrive AFTER the removal has done its map work
+ // and settled into the drain — that is the window the defect lived in, and
+ // racing the two starts hides it about as often as it shows it.
+ time.Sleep(25 * time.Millisecond)
+
+ holdSecond := make(chan struct{})
+ secondDone := make(chan struct{})
+ go func() {
+ _ = te.Execute(context.Background(), "tab1", blocking(holdSecond))
+ close(secondDone)
+ }()
+
+ // Give the second caller every chance to slip in beside the first.
+ time.Sleep(100 * time.Millisecond)
+ got := peak.Peak()
+
+ close(holdFirst)
+ close(holdSecond)
+ <-firstDone
+ <-secondDone
+ <-removed
+
+ if got > 1 {
+ t.Errorf("%d tasks were inside tab1 at once; RemoveTab admitted a second caller while the tab was still busy", got)
+ }
+}
+
+// waitFor polls a condition instead of sleeping a guessed interval, so the test
+// is not slower than it needs to be nor flaky on a loaded machine.
+func waitFor(t *testing.T, cond func() bool, what string) {
+ t.Helper()
+ deadline := time.Now().Add(5 * time.Second)
+ for time.Now().Before(deadline) {
+ if cond() {
+ return
+ }
+ time.Sleep(time.Millisecond)
+ }
+ t.Fatalf("timed out waiting for %s", what)
+}
diff --git a/internal/bridge/tabs/tab_executor.go b/internal/bridge/tabs/tab_executor.go
index 9691a9f7..b4ac38d8 100644
--- a/internal/bridge/tabs/tab_executor.go
+++ b/internal/bridge/tabs/tab_executor.go
@@ -9,10 +9,25 @@ import (
"time"
)
+// tabGate is one tab's turnstile: the mutex that serializes work on it, plus
+// the count of callers currently inside or waiting.
+//
+// The count exists so the gate cannot be removed from the map while anyone is
+// still using it. Without it a caller arriving mid-removal finds no entry,
+// makes a second mutex for the same tab, and runs beside the work the first
+// mutex was serializing — which is the one thing this type exists to prevent.
+type tabGate struct {
+ mu sync.Mutex
+ // users and removing are guarded by TabExecutor.mu, not by gate.mu: they
+ // describe who may delete the map entry, which is a decision about the map.
+ users int
+ removing bool
+}
+
// TabExecutor provides safe parallel execution across tabs.
type TabExecutor struct {
semaphore chan struct{}
- tabLocks map[string]*sync.Mutex
+ tabLocks map[string]*tabGate
mu sync.Mutex
maxParallel int
}
@@ -23,7 +38,7 @@ func NewTabExecutor(maxParallel int) *TabExecutor {
}
return &TabExecutor{
semaphore: make(chan struct{}, maxParallel),
- tabLocks: make(map[string]*sync.Mutex),
+ tabLocks: make(map[string]*tabGate),
maxParallel: maxParallel,
}
}
@@ -43,15 +58,35 @@ func (te *TabExecutor) MaxParallel() int {
return te.maxParallel
}
-func (te *TabExecutor) tabMutex(tabID string) *sync.Mutex {
+// enterGate returns this tab's gate and registers the caller against it, so the
+// entry cannot be deleted while the caller is still inside.
+func (te *TabExecutor) enterGate(tabID string) *tabGate {
te.mu.Lock()
defer te.mu.Unlock()
- m, ok := te.tabLocks[tabID]
+ g, ok := te.tabLocks[tabID]
if !ok {
- m = &sync.Mutex{}
- te.tabLocks[tabID] = m
+ g = &tabGate{}
+ te.tabLocks[tabID] = g
+ }
+ g.users++
+ return g
+}
+
+// leaveGate deregisters the caller, and drops the entry once the last user of a
+// gate a RemoveTab asked for is gone.
+//
+// A gate no removal has asked for stays in the map after its last user leaves:
+// ActiveTabs counts tabs this executor has run and not been told to forget, and
+// reclaiming on idle would turn it into a count of tabs running right now.
+func (te *TabExecutor) leaveGate(tabID string, g *tabGate) {
+ te.mu.Lock()
+ defer te.mu.Unlock()
+ g.users--
+ if g.users == 0 && g.removing {
+ if cur, ok := te.tabLocks[tabID]; ok && cur == g {
+ delete(te.tabLocks, tabID)
+ }
}
- return m
}
func (te *TabExecutor) Execute(ctx context.Context, tabID string, task func(ctx context.Context) error) error {
@@ -69,7 +104,8 @@ func (te *TabExecutor) Execute(ctx context.Context, tabID string, task func(ctx
return fmt.Errorf("tab %s: waiting for execution slot: %w", tabID, ctx.Err())
}
- tabMu := te.tabMutex(tabID)
+ gate := te.enterGate(tabID)
+ tabMu := &gate.mu
locked := make(chan struct{})
go func() {
tabMu.Lock()
@@ -78,11 +114,17 @@ func (te *TabExecutor) Execute(ctx context.Context, tabID string, task func(ctx
select {
case <-locked:
+ defer te.leaveGate(tabID, gate)
defer tabMu.Unlock()
case <-ctx.Done():
+ // The acquire is already in flight and cannot be cancelled, so the
+ // abandoning caller stays registered until it lands and releases —
+ // leaving earlier would let the entry be deleted while this goroutine
+ // still holds the mutex behind it.
go func() {
<-locked
tabMu.Unlock()
+ te.leaveGate(tabID, gate)
}()
return fmt.Errorf("tab %s: waiting for tab lock: %w", tabID, ctx.Err())
}
@@ -103,18 +145,31 @@ func (te *TabExecutor) safeRun(ctx context.Context, tabID string, task func(ctx
return task(ctx)
}
+// RemoveTab forgets a tab, once the work already running on it has finished.
+//
+// The entry used to be deleted first and drained second. In that order a caller
+// arriving during the drain found nothing in the map, created a second mutex for
+// the same tab, and ran beside the task still holding the first — two CDP
+// operations interleaved on one tab, which is exactly what this executor exists
+// to prevent. It is reachable whenever a tab is closed while an action on it is
+// in flight and another is still resolving.
+//
+// So the entry now stays until the last user of it is gone: the drain registers
+// as a user itself, and whoever leaves last does the deleting.
func (te *TabExecutor) RemoveTab(tabID string) {
te.mu.Lock()
- m, ok := te.tabLocks[tabID]
+ g, ok := te.tabLocks[tabID]
if !ok {
te.mu.Unlock()
return
}
- delete(te.tabLocks, tabID)
+ g.users++
+ g.removing = true
te.mu.Unlock()
- m.Lock()
- defer m.Unlock() //nolint:staticcheck
+ g.mu.Lock()
+ g.mu.Unlock() //nolint:staticcheck // taken solely to wait out the work in flight
+ te.leaveGate(tabID, g)
}
func (te *TabExecutor) ActiveTabs() int {