Skip to content

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

fix(tabs): a tab's lease outlived the tab

Darshan Jainae90f0751113
diff --git a/internal/bridge/bridge.go b/internal/bridge/bridge.go
index 56acc8d0..6c64d16f 100644
--- a/internal/bridge/bridge.go
+++ b/internal/bridge/bridge.go
@@ -147,6 +147,24 @@ func (b *Bridge) dropFetchPauseSuppression(tabID string) {
delete(b.fetchPauseFlags, tabID)
}
+// releaseTabLock drops a closed tab's lease.
+//
+// The lock manager hangs off the Bridge rather than the TabManager, so it was
+// missed by purgeTrackedTabState, which clears that type's own per-tab maps and
+// notifies the dialog, executor, log and route managers. A tab that was locked
+// and then closed therefore left its entry behind for the life of the process.
+//
+// Registered beside dropFetchPauseSuppression as built-in cleanup, so it is
+// re-applied whenever a launch, restart or remote-CDP path swaps the TabManager.
+// Locks is nil-checked because wireTabManager runs before the constructor
+// assigns it on the very first wire.
+func (b *Bridge) releaseTabLock(tabID string) {
+ if b.Locks == nil || tabID == "" {
+ return
+ }
+ b.Locks.Release(tabID)
+}
+
// AddTabRemovedHook registers an external per-tab cleanup that must survive a
// TabManager swap. It records the hook on the bridge and applies it to the
// current TabManager; wireTabManager re-applies all recorded hooks after a
@@ -214,6 +232,7 @@ func (b *Bridge) wireTabManager(browserCtx context.Context) {
// wire, so no cross-reinit duplication). External hooks recorded on the
// bridge are re-applied so they survive the TabManager swap.
b.TabManager.AddTabRemovedHook(b.dropFetchPauseSuppression)
+ b.TabManager.AddTabRemovedHook(b.releaseTabLock)
b.SetFreezeVeto(b.tabHandoffPaused)
b.tabRemovedHooksMu.Lock()
hooks := make([]func(string), len(b.externalTabRemovedHooks))
diff --git a/internal/bridge/tab_removed_hook_test.go b/internal/bridge/tab_removed_hook_test.go
index 48f5a941..24ad639a 100644
--- a/internal/bridge/tab_removed_hook_test.go
+++ b/internal/bridge/tab_removed_hook_test.go
@@ -3,12 +3,14 @@ package bridge
import (
"context"
"testing"
+ "time"
)
// TestExternalTabRemovedHookSurvivesRewire verifies that a hook registered via
// Bridge.AddTabRemovedHook is applied to the current TabManager, re-applied when
// wireTabManager swaps the TabManager (launch/reinit/remote-CDP), and not
-// duplicated across rewires — alongside the built-in dropFetchPauseSuppression.
+// duplicated across rewires — alongside the built-in dropFetchPauseSuppression
+// and releaseTabLock.
func TestExternalTabRemovedHookSurvivesRewire(t *testing.T) {
b := &Bridge{}
@@ -18,9 +20,9 @@ func TestExternalTabRemovedHookSurvivesRewire(t *testing.T) {
ctx := context.Background()
b.wireTabManager(ctx)
- // External hook + built-in dropFetchPauseSuppression.
- if got := len(b.onTabRemovedHooks); got != 2 {
- t.Fatalf("hooks after first wire = %d, want 2", got)
+ // External hook + the built-ins: dropFetchPauseSuppression and releaseTabLock.
+ if got := len(b.onTabRemovedHooks); got != builtInTabRemovedHooks+1 {
+ t.Fatalf("hooks after first wire = %d, want %d", got, builtInTabRemovedHooks+1)
}
for _, h := range b.onTabRemovedHooks {
h("tab1")
@@ -32,7 +34,50 @@ func TestExternalTabRemovedHookSurvivesRewire(t *testing.T) {
// A reinit swaps the TabManager; the external hook must persist without
// duplicating (built-in is freshly re-added, not accumulated).
b.wireTabManager(ctx)
- if got := len(b.onTabRemovedHooks); got != 2 {
- t.Fatalf("hooks after rewire = %d, want 2 (no duplication)", got)
+ if got := len(b.onTabRemovedHooks); got != builtInTabRemovedHooks+1 {
+ t.Fatalf("hooks after rewire = %d, want %d (no duplication)", got, builtInTabRemovedHooks+1)
+ }
+}
+
+// builtInTabRemovedHooks is how many cleanups wireTabManager adds by itself.
+// Naming it keeps the count above about duplication across a rewire, which is
+// what that test is for, rather than about how many built-ins happen to exist.
+const builtInTabRemovedHooks = 2
+
+// The lease of a closed tab is dropped because releaseTabLock is WIRED, not
+// merely because LockManager.Release exists. This drives the hook list the tab
+// teardown runs, so a future wire-up that forgets it reds here rather than
+// leaking quietly.
+func TestClosingATabDropsItsLease(t *testing.T) {
+ b := &Bridge{}
+ b.Locks = NewLockManager()
+ b.wireTabManager(context.Background())
+
+ if err := b.Locks.TryLock("tab1", "agent-a", time.Minute); err != nil {
+ t.Fatal(err)
+ }
+ if err := b.Locks.TryLock("tab2", "agent-b", time.Minute); err != nil {
+ t.Fatal(err)
+ }
+
+ for _, h := range b.onTabRemovedHooks {
+ h("tab1")
+ }
+
+ if b.Locks.Get("tab1") != nil {
+ t.Error("the closed tab kept its lease; releaseTabLock is not reaching the teardown")
+ }
+ if b.Locks.Get("tab2") == nil {
+ t.Error("closing one tab dropped another tab's lease")
+ }
+}
+
+// wireTabManager runs before the constructor assigns Locks on the first wire, so
+// the hook has to tolerate a nil manager rather than panic during startup.
+func TestReleaseTabLockBeforeLocksExist(t *testing.T) {
+ b := &Bridge{}
+ b.wireTabManager(context.Background())
+ for _, h := range b.onTabRemovedHooks {
+ h("tab1")
}
}
diff --git a/internal/bridge/tabs/lock.go b/internal/bridge/tabs/lock.go
index cea6513d..4bc3d01c 100644
--- a/internal/bridge/tabs/lock.go
+++ b/internal/bridge/tabs/lock.go
@@ -63,6 +63,25 @@ func (m *LockManager) Unlock(tabID, owner string) error {
return nil
}
+// Release drops a tab's lease without asking who held it, for a tab that no
+// longer exists.
+//
+// Unlock is the owner's operation and refuses anyone else, which is right while
+// there is still a tab to protect. A closed tab has no work left to serialize,
+// so there is nobody the lease could still be protecting it from, and requiring
+// the owner would mean an agent that closed a tab without unlocking it left an
+// entry no call could ever remove.
+//
+// Nothing did remove them: purgeTrackedTabState clears the four per-tab maps on
+// the TabManager and notifies the dialog, executor, log and route managers, but
+// the lock manager hangs off the Bridge and was not among them. Every tab locked
+// and then closed left an entry behind for the life of the process.
+func (m *LockManager) Release(tabID string) {
+ m.mu.Lock()
+ defer m.mu.Unlock()
+ delete(m.locks, tabID)
+}
+
func (m *LockManager) Get(tabID string) *LockInfo {
m.mu.Lock()
defer m.mu.Unlock()
@@ -77,3 +96,13 @@ func (m *LockManager) Get(tabID string) *LockInfo {
ExpiresAt: l.expires,
}
}
+
+// trackedForTest reports how many leases the manager is holding, including any
+// that have expired but not yet been dropped. Tests assert on retention, which
+// is not observable through Get: Get reports an expired lease as absent while
+// its entry is still in the map.
+func (m *LockManager) trackedForTest() int {
+ m.mu.Lock()
+ defer m.mu.Unlock()
+ return len(m.locks)
+}
diff --git a/internal/bridge/tabs/lock_test.go b/internal/bridge/tabs/lock_test.go
new file mode 100644
index 00000000..ccbe3fdc
--- /dev/null
+++ b/internal/bridge/tabs/lock_test.go
@@ -0,0 +1,101 @@
+package tabs
+
+import (
+ "fmt"
+ "testing"
+ "time"
+)
+
+// A lease is a promise about a tab. When the tab is gone the promise has nobody
+// left to keep, and the entry holding it is only memory.
+//
+// Nothing dropped it: purgeTrackedTabState clears the TabManager's own per-tab
+// maps and notifies the dialog, executor, log and route managers, but the lock
+// manager hangs off the Bridge and was not among them. Measured before Release
+// existed: 1000 tabs locked and closed left 1000 entries.
+func TestReleaseDropsTheLeaseOfATabThatIsGone(t *testing.T) {
+ m := NewLockManager()
+ const tabs = 1000
+
+ for i := 0; i < tabs; i++ {
+ if err := m.TryLock(fmt.Sprintf("tab-%d", i), "agent-a", time.Minute); err != nil {
+ t.Fatalf("lock tab-%d: %v", i, err)
+ }
+ }
+ if got := m.trackedForTest(); got != tabs {
+ t.Fatalf("held %d leases after locking %d tabs", got, tabs)
+ }
+
+ for i := 0; i < tabs; i++ {
+ m.Release(fmt.Sprintf("tab-%d", i))
+ }
+ if got := m.trackedForTest(); got != 0 {
+ t.Errorf("%d leases survive tabs that no longer exist", got)
+ }
+}
+
+// Release answers to the tab, not to the owner. Requiring the owner would leave
+// an entry no call could remove whenever an agent closed a tab it had locked
+// without unlocking it first — which is the case that produced the leak.
+func TestReleaseDoesNotRequireTheOwner(t *testing.T) {
+ m := NewLockManager()
+ if err := m.TryLock("tab1", "agent-a", time.Minute); err != nil {
+ t.Fatal(err)
+ }
+ if err := m.Unlock("tab1", "agent-b"); err == nil {
+ t.Fatal("Unlock accepted the wrong owner; that refusal is what makes Release necessary")
+ }
+
+ m.Release("tab1")
+ if m.Get("tab1") != nil {
+ t.Error("the lease survived Release")
+ }
+ if got := m.trackedForTest(); got != 0 {
+ t.Errorf("%d entries retained after Release", got)
+ }
+}
+
+// Releasing a tab that holds no lease is what a close after an unlock looks
+// like, so it has to be a no-op rather than a panic or a resurrected entry.
+func TestReleaseOfAnUnlockedTabIsHarmless(t *testing.T) {
+ m := NewLockManager()
+ m.Release("never-locked")
+ if got := m.trackedForTest(); got != 0 {
+ t.Errorf("Release created %d entries for a tab that held no lease", got)
+ }
+
+ if err := m.TryLock("tab1", "agent-a", time.Minute); err != nil {
+ t.Fatal(err)
+ }
+ if err := m.Unlock("tab1", "agent-a"); err != nil {
+ t.Fatal(err)
+ }
+ m.Release("tab1")
+ if got := m.trackedForTest(); got != 0 {
+ t.Errorf("%d entries after unlock followed by release", got)
+ }
+}
+
+// The lease a live tab still holds must be unaffected: Release names one tab.
+func TestReleaseTouchesOnlyTheTabItNames(t *testing.T) {
+ m := NewLockManager()
+ if err := m.TryLock("closing", "agent-a", time.Minute); err != nil {
+ t.Fatal(err)
+ }
+ if err := m.TryLock("still-open", "agent-b", time.Minute); err != nil {
+ t.Fatal(err)
+ }
+
+ m.Release("closing")
+
+ if m.Get("closing") != nil {
+ t.Error("the closed tab kept its lease")
+ }
+ live := m.Get("still-open")
+ if live == nil {
+ t.Fatal("releasing one tab dropped another tab's lease")
+ }
+ if live.Owner != "agent-b" {
+ t.Errorf("surviving lease owner = %q, want agent-b", live.Owner)
+ }
+}