Skip to content

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

fix: instance repository aliases caller structs, racing orchestrator status writes

Luigi Agosti95ca8c833d10
diff --git a/internal/instance/repository.go b/internal/instance/repository.go
index 09bf6c69..a9741bee 100644
--- a/internal/instance/repository.go
+++ b/internal/instance/repository.go
@@ -30,11 +30,20 @@ func (r *Repository) Launch(name, port string, headless bool) (*bridge.Instance,
return nil, fmt.Errorf("launch instance: %w", err)
}
r.mu.Lock()
- r.instances[inst.ID] = inst
+ r.store(inst)
r.mu.Unlock()
return inst, nil
}
+// store snapshots inst so the repository owns every struct it serves. Callers
+// keep mutating the instance they handed over — the orchestrator does so under
+// its own lock — and that would otherwise be two unrelated mutexes guarding the
+// same memory. Caller must hold r.mu.
+func (r *Repository) store(inst *bridge.Instance) {
+ snapshot := *inst
+ r.instances[inst.ID] = &snapshot
+}
+
// Stop terminates an instance and removes it from the store.
func (r *Repository) Stop(id string) error {
if err := r.launcher.Stop(id); err != nil {
@@ -84,8 +93,11 @@ func (r *Repository) Get(id string) (*bridge.Instance, bool) {
// Add registers an existing instance (used for sync with external state).
func (r *Repository) Add(inst *bridge.Instance) {
+ if inst == nil {
+ return
+ }
r.mu.Lock()
- r.instances[inst.ID] = inst
+ r.store(inst)
r.mu.Unlock()
}
diff --git a/internal/instance/repository_ownership_test.go b/internal/instance/repository_ownership_test.go
new file mode 100644
index 00000000..f822d1b7
--- /dev/null
+++ b/internal/instance/repository_ownership_test.go
@@ -0,0 +1,56 @@
+package instance_test
+
+import (
+ "sync"
+ "testing"
+
+ bridgepkg "github.com/pinchtab/pinchtab/internal/bridge"
+ "github.com/pinchtab/pinchtab/internal/instance"
+)
+
+// Get/List/Running all return copies, so the repository is meant to own its
+// instances. Add and Launch stored the caller's pointer instead, and the
+// orchestrator keeps mutating that struct under its own lock — two unrelated
+// mutexes over the same memory.
+func TestRepositoryDoesNotAliasAddedInstance(t *testing.T) {
+ repo := instance.NewRepository(newMockLauncher())
+
+ inst := &bridgepkg.Instance{ID: "inst_1", Port: "9001", Status: "running"}
+ repo.Add(inst)
+
+ var wg sync.WaitGroup
+ wg.Add(2)
+ go func() {
+ defer wg.Done()
+ for i := 0; i < 500; i++ {
+ inst.Status = "stopped"
+ inst.Status = "running"
+ }
+ }()
+ go func() {
+ defer wg.Done()
+ for i := 0; i < 500; i++ {
+ _ = repo.Running()
+ _ = repo.List()
+ }
+ }()
+ wg.Wait()
+}
+
+// A snapshot taken through Add must not change when the caller mutates the
+// struct it handed over.
+func TestRepositoryAddSnapshotsInstance(t *testing.T) {
+ repo := instance.NewRepository(newMockLauncher())
+
+ inst := &bridgepkg.Instance{ID: "inst_1", Port: "9001", Status: "running"}
+ repo.Add(inst)
+ inst.Status = "stopped"
+
+ got, ok := repo.Get("inst_1")
+ if !ok {
+ t.Fatal("instance missing from repository")
+ }
+ if got.Status != "running" {
+ t.Errorf("Status = %q, want running — repository aliased the caller's struct", got.Status)
+ }
+}
diff --git a/internal/orchestrator/health.go b/internal/orchestrator/health.go
index 86be4998..c10ecbc6 100644
--- a/internal/orchestrator/health.go
+++ b/internal/orchestrator/health.go
@@ -91,7 +91,6 @@ func (o *Orchestrator) applyStartupOutcome(inst *InstanceInternal, p startupProb
inst.URL = p.resolvedURL
inst.Instance.URL = p.resolvedURL
}
- o.syncInstanceToManager(&inst.Instance)
eventType = "instance.started"
slog.Info("instance ready", "id", inst.ID, "port", inst.Port)
} else if p.exitedEarly {
@@ -118,6 +117,9 @@ func (o *Orchestrator) applyStartupOutcome(inst *InstanceInternal, p startupProb
slog.Error("instance failed to start", "id", inst.ID, "reason", string(inst.lastFailureReason))
}
}
+ // The repository holds a snapshot, so every status transition must re-sync;
+ // error and timeout outcomes used to reach it only by sharing this struct.
+ o.syncInstanceToManager(&inst.Instance)
instCopy := inst.Instance
o.mu.Unlock()
if eventType != "" {
@@ -134,6 +136,7 @@ func (o *Orchestrator) finalizeInstanceExit(inst *InstanceInternal, p startupPro
if inst.Status == "running" || inst.Status == "stopping" {
inst.Status = "stopped"
wasStopped = true
+ o.syncInstanceToManager(&inst.Instance)
}
instCopy := inst.Instance
o.mu.Unlock()