Skip to content

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

fix: an installed instance lookup short-circuited the pinchtab.pid check, so a profile held by a second pinchtab was reported idle and deleted (PIN-250)

Luigi Agosti434fbaddc60e
diff --git a/internal/profiles/profiles.go b/internal/profiles/profiles.go
index 151f3afe..fb03f999 100644
--- a/internal/profiles/profiles.go
+++ b/internal/profiles/profiles.go
@@ -75,12 +75,19 @@ func (pm *ProfileManager) SetInstanceLookup(lookup func(profileID string) (insta
// profileHolder is the single owner of the in-use rule: the Delete and Reset
// guards and the published running flag must agree, so all of them consult
// this and nothing else. Callers hold pm.mu.
+//
+// The two sources are ORed, not tried in order. An orchestrator only knows the
+// instances IT started, so a profile held by a second pinchtab — another server,
+// a `pinchtab bridge`, an always-on instance outside this map — makes the lookup
+// answer not-running with full confidence. Returning there would delete a live
+// profile on the very surface this guard was written for. The pid lock is
+// per-directory truth and sees that holder; it reads not-owned for our own pid,
+// which is what keeps the orchestrator's own temp-profile cleanup working.
func (pm *ProfileManager) profileHolder(id, dir string) (holder string, held bool) {
if pm.instanceLookup != nil {
if instanceID, running := pm.instanceLookup(id); running {
return instanceID, true
}
- return "", false
}
if pm.lockOwner != nil {
if owned, pid := pm.lockOwner(dir); owned {
diff --git a/internal/profiles/profiles_test.go b/internal/profiles/profiles_test.go
index 4e0d0227..dc257817 100644
--- a/internal/profiles/profiles_test.go
+++ b/internal/profiles/profiles_test.go
@@ -1567,3 +1567,77 @@ func TestDefaultLockOwnerTreatsAPidFreeDirectoryAsIdle(t *testing.T) {
t.Fatalf("expected 200 for a directory with no pinchtab.pid, got %d: %s", w.Code, w.Body.String())
}
}
+
+// An orchestrator only knows the instances IT started. A profile held by a SECOND pinchtab
+// — another server, a `pinchtab bridge`, an always-on instance outside this map — makes the
+// lookup answer not-running with full confidence, so consulting it alone deletes a live
+// profile on exactly the surface this card was filed against: the one where a lookup IS
+// installed. The two sources are therefore ORed, and this is the direction that proves it.
+func TestAProfileHeldOnlyByThePidLockIsRefusedEvenWhenTheLookupSaysIdle(t *testing.T) {
+ pm := NewProfileManager(t.TempDir())
+ id, cookies := newHeldProfile(t, pm, "held-by-another-pinchtab")
+ pm.lockOwner = func(string) (bool, int) { return true, 4242 }
+ pm.SetInstanceLookup(func(string) (string, bool) { return "", false })
+
+ mux := http.NewServeMux()
+ pm.RegisterHandlers(mux)
+
+ for _, req := range []*http.Request{
+ httptest.NewRequest("DELETE", "/profiles/"+id, nil),
+ httptest.NewRequest("POST", "/profiles/"+id+"/reset", nil),
+ } {
+ w := httptest.NewRecorder()
+ mux.ServeHTTP(w, req)
+ if w.Code != 409 {
+ t.Errorf("%s %s: got %d, want 409 — an installed lookup that answers not-running must not short-circuit the per-directory lock: %s",
+ req.Method, req.URL.Path, w.Code, w.Body.String())
+ }
+ if !strings.Contains(w.Body.String(), "4242") {
+ t.Errorf("%s %s: refusal does not name the pid-lock holder: %s", req.Method, req.URL.Path, w.Body.String())
+ }
+ }
+ requireCookiesIntact(t, cookies)
+
+ // The flag half of this card fails in the same case, from the same line.
+ list, err := pm.List()
+ if err != nil {
+ t.Fatal(err)
+ }
+ found := false
+ for _, p := range list {
+ if p.Name != "held-by-another-pinchtab" {
+ continue
+ }
+ found = true
+ if !p.Running {
+ t.Errorf("running = false for a profile the pid lock says is held; the published flag and the guard must agree, since a client greys the button out from this field")
+ }
+ }
+ if !found {
+ t.Fatal("the held profile is absent from List, so this assertion would pass vacuously")
+ }
+}
+
+// The other direction, and the one the suite could not express before: the OR must not
+// refuse forever on a stale file. Nothing in the tree ever REMOVES pinchtab.pid — there is
+// no release path — so the file outlives its process and only the liveness and is-pinchtab
+// checks stop a dead pid reading as held. This is what stops a later reader simplifying the
+// OR back out on the grounds that it can only over-refuse.
+func TestAStaleLockWithNoLiveHolderStillDeletes(t *testing.T) {
+ pm := NewProfileManager(t.TempDir())
+ id, _ := newHeldProfile(t, pm, "stale-lock")
+ // What the real check answers for a pid that is dead, or alive but not pinchtab: the
+ // file is present and readable, and ownership is still refused.
+ pm.lockOwner = func(string) (bool, int) { return false, 999999 }
+ pm.SetInstanceLookup(func(string) (string, bool) { return "", false })
+
+ mux := http.NewServeMux()
+ pm.RegisterHandlers(mux)
+
+ w := httptest.NewRecorder()
+ mux.ServeHTTP(w, httptest.NewRequest("DELETE", "/profiles/"+id, nil))
+ if w.Code != 200 {
+ t.Fatalf("got %d, want 200 — a lock whose holder is gone must not refuse forever; there is no release path, so the file always outlives the process: %s",
+ w.Code, w.Body.String())
+ }
+}
diff --git a/internal/server/profile_holder_composition_test.go b/internal/server/profile_holder_composition_test.go
new file mode 100644
index 00000000..ef6da5be
--- /dev/null
+++ b/internal/server/profile_holder_composition_test.go
@@ -0,0 +1,64 @@
+package server
+
+import (
+ "testing"
+
+ "github.com/pinchtab/pinchtab/internal/bridge"
+)
+
+// This closes the gap carried on the card: the profile guard's safety depends on WHICH
+// instance states count as holding a directory, and that set lived inside a closure in
+// RunDashboard where no test could reach it. RunDashboard binds a listener and launches a
+// browser, so it is not drivable — extracting the derivation is what makes it assertable at
+// all, and this is the composition the running server actually installs.
+func TestProfileInstanceHolderCountsEveryStateThatHoldsTheDirectory(t *testing.T) {
+ const profileID = "prof_held"
+
+ for _, tc := range []struct {
+ status string
+ wantHeld bool
+ why string
+ }{
+ {"running", true, "the obvious case"},
+ {"starting", true, "the profile is claimed before the process reports running, so deleting here races the launch"},
+ {"stopping", true, "the browser still has the directory open while it winds down; deleting is the same loss as deleting while it runs"},
+ {"stopped", false, "nothing holds the directory"},
+ {"failed", false, "nothing holds the directory"},
+ {"", false, "an unset status must not read as held, or every idle profile becomes undeletable"},
+ } {
+ t.Run(tc.status, func(t *testing.T) {
+ instances := []bridge.Instance{{ID: "inst_1", ProfileID: profileID, Status: tc.status}}
+
+ holder, held := profileInstanceHolder(instances, profileID)
+ if held != tc.wantHeld {
+ t.Errorf("status %q: held = %v, want %v — %s", tc.status, held, tc.wantHeld, tc.why)
+ }
+ if held && holder != "inst_1" {
+ t.Errorf("status %q: holder = %q, want the instance id so the 409 can name it", tc.status, holder)
+ }
+ })
+ }
+}
+
+// The match is on ProfileID. bridge.Instance also carries ProfileName, which its own comment
+// marks display-only, and matching on that would make the guard miss a rename and collide
+// two profiles resolving to one display string.
+func TestProfileInstanceHolderMatchesOnIDNotDisplayName(t *testing.T) {
+ instances := []bridge.Instance{
+ {ID: "inst_other", ProfileID: "prof_other", ProfileName: "default", Status: "running"},
+ }
+
+ if holder, held := profileInstanceHolder(instances, "prof_default"); held {
+ t.Errorf("a different profile with the same display name reads as the holder (%q); the guard must key on the id the delete route resolves", holder)
+ }
+ if _, held := profileInstanceHolder(instances, "prof_other"); !held {
+ t.Error("the instance's own profile id is not matched, so the lookup finds nothing at all")
+ }
+}
+
+// An empty instance list is the fresh-server case and must not refuse everything.
+func TestProfileInstanceHolderReportsNothingHeldWithNoInstances(t *testing.T) {
+ if holder, held := profileInstanceHolder(nil, "prof_any"); held {
+ t.Errorf("held = true with no instances running (holder %q)", holder)
+ }
+}
diff --git a/internal/server/server.go b/internal/server/server.go
index eb04accd..560ed470 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -16,6 +16,7 @@ import (
"github.com/pinchtab/pinchtab/internal/activity"
"github.com/pinchtab/pinchtab/internal/authn"
+ "github.com/pinchtab/pinchtab/internal/bridge"
_ "github.com/pinchtab/pinchtab/internal/browsers/all"
"github.com/pinchtab/pinchtab/internal/browsers/providerhooks"
"github.com/pinchtab/pinchtab/internal/browsersession"
@@ -74,16 +75,7 @@ func RunDashboard(cfg *config.RuntimeConfig, version string) {
orch.ApplyRuntimeConfig(cfg)
orch.SetProfileManager(profMgr)
profMgr.SetInstanceLookup(func(profileID string) (string, bool) {
- for _, inst := range orch.List() {
- if inst.ProfileID != profileID {
- continue
- }
- switch inst.Status {
- case "starting", "running", "stopping":
- return inst.ID, true
- }
- }
- return "", false
+ return profileInstanceHolder(orch.List(), profileID)
})
dash.SetInstanceLister(orch)
dash.SetMonitoringSource(orch)
@@ -404,3 +396,27 @@ func gracefulShutdownWithCap(orch *orchestrator.Orchestrator, cap time.Duration)
return false
}
}
+
+// heldInstanceStatuses are the instance states that count as holding a profile. STOPPING is
+// in the set deliberately: the browser still has the directory open while it winds down, so
+// deleting then is the same loss as deleting while it runs. STARTING likewise — the profile
+// is claimed before the process reports running.
+//
+// This is the safety-critical half of the profile guard, so it is a named function rather
+// than a closure inside RunDashboard: as a closure nothing could reach it, and the states it
+// matches were the one part of the guard no test could see.
+var heldInstanceStatuses = map[string]bool{"starting": true, "running": true, "stopping": true}
+
+// profileInstanceHolder reports the instance holding profileID, if any. It is the exact
+// derivation handed to ProfileManager.SetInstanceLookup at composition.
+func profileInstanceHolder(instances []bridge.Instance, profileID string) (string, bool) {
+ for _, inst := range instances {
+ if inst.ProfileID != profileID {
+ continue
+ }
+ if heldInstanceStatuses[inst.Status] {
+ return inst.ID, true
+ }
+ }
+ return "", false
+}