High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix(orchestrator): POST /instances/start serializes an instance the monitor is writing
Darshan Jainb46ce400ef06
diff --git a/internal/orchestrator/instance_launch.go b/internal/orchestrator/instance_launch.goindex c655985b..e5c8b5ba 100644--- a/internal/orchestrator/instance_launch.go+++ b/internal/orchestrator/instance_launch.go@@ -223,12 +223,22 @@ func (o *Orchestrator) LaunchWithOptions(name, port string, headless bool, optseffectiveBinary: effectiveBinaryFromCfg(effectiveCfg),}+ // Snapshot under the same lock that guards the monitor's writes, and before+ // the monitor exists. monitor -> applyStartupOutcome writes Status, URL and+ // Error on this very struct; returning &inst.Instance handed the caller a+ // live pointer into it, and POST /instances/start serializes that pointer to+ // the response with no lock held. The race detector reports it as+ // applyStartupOutcome (health.go) against bridge.Instance.MarshalJSON.+ //+ // snapshotWithFallbackMetadata already returns a detached copy for the+ // fallback path; this makes the plain path agree.o.mu.Lock()o.instances[instanceID] = inst+ snapshot := inst.Instanceo.mu.Unlock()reservedPorts = nilgo o.monitor(inst)- return &inst.Instance, nil+ return &snapshot, nil}diff --git a/internal/orchestrator/instance_launch_race_test.go b/internal/orchestrator/instance_launch_race_test.gonew file mode 100644index 00000000..107c8abd--- /dev/null+++ b/internal/orchestrator/instance_launch_race_test.go@@ -0,0 +1,62 @@+package orchestrator++import (+ "encoding/json"+ "sync"+ "testing"+ "time"+)++// LaunchWithOptions starts o.monitor on the instance it just registered, and+// monitor -> applyStartupOutcome writes Status, URL and Error on that struct.+// Returning &inst.Instance handed the caller a live pointer into it, and+// POST /instances/start serializes exactly that pointer into the response with+// no lock held (handlers_instances.go, httpx.JSON(w, 201, inst)).+//+// The detector reports it as applyStartupOutcome against+// bridge.Instance.MarshalJSON. It is timing-dependent enough that a plain+// package run only surfaces it under load, so this reproduces the shape+// directly: marshal the returned value while the monitor is still writing.+//+// Fails under -race before the snapshot in LaunchWithOptions; passes after.+func TestLaunchReturnsAValueTheMonitorCannotWrite(t *testing.T) {+ if !raceDetectorEnabled {+ t.Skip("verifies nothing without the race detector; run with -race")+ }++ old := processAliveFunc+ processAliveFunc = func(pid int) bool { return pid > 0 }+ t.Cleanup(func() { processAliveFunc = old })+ stubPortAvailability(t, func(int) bool { return true })++ o := NewOrchestratorWithRunner(t.TempDir(), &mockRunner{portAvail: true})+ t.Cleanup(func() {+ for _, inst := range o.List() {+ _ = o.Stop(inst.ID)+ }+ })++ inst, err := o.LaunchWithOptions("race-probe", "9061", true, LaunchOptions{})+ if err != nil {+ t.Fatalf("LaunchWithOptions: %v", err)+ }++ // The response path: serialize what the caller was handed, continuously,+ // across the window in which the monitor writes. probeStartupHealth sleeps+ // instanceHealthPollInterval before its first probe, so the earliest write+ // lands ~500ms in; marshalling for a few multiples of that spans it without+ // depending on the exact scheduling.+ deadline := time.Now().Add(4 * instanceHealthPollInterval)+ var wg sync.WaitGroup+ wg.Add(1)+ go func() {+ defer wg.Done()+ for time.Now().Before(deadline) {+ if _, err := json.Marshal(inst); err != nil {+ t.Errorf("Marshal: %v", err)+ return+ }+ }+ }()+ wg.Wait()+}diff --git a/internal/orchestrator/race_detector_off_test.go b/internal/orchestrator/race_detector_off_test.gonew file mode 100644index 00000000..022a3283--- /dev/null+++ b/internal/orchestrator/race_detector_off_test.go@@ -0,0 +1,7 @@+//go:build !race++package orchestrator++// raceDetectorEnabled reports whether this test binary was built with -race.+// See race_detector_on_test.go.+const raceDetectorEnabled = falsediff --git a/internal/orchestrator/race_detector_on_test.go b/internal/orchestrator/race_detector_on_test.gonew file mode 100644index 00000000..fcfca36a--- /dev/null+++ b/internal/orchestrator/race_detector_on_test.go@@ -0,0 +1,8 @@+//go:build race++package orchestrator++// raceDetectorEnabled reports whether this test binary was built with -race.+// The launch-response test asserts nothing itself — the detector is its+// assertion — so it skips rather than passing vacuously when this is false.+const raceDetectorEnabled = true