Skip to content

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

fix: install the stable launch binary atomically instead of truncating it in place

Luigi Agosti9d09c8434cf6
diff --git a/internal/orchestrator/orchestrator.go b/internal/orchestrator/orchestrator.go
index 0d6ee12a..155132db 100644
--- a/internal/orchestrator/orchestrator.go
+++ b/internal/orchestrator/orchestrator.go
@@ -4,11 +4,8 @@ import (
"context"
"crypto/rand"
"encoding/hex"
- "io"
"log/slog"
"net/http"
- "os"
- "path/filepath"
"sync"
"time"
@@ -166,41 +163,6 @@ func NewOrchestratorWithRunner(baseDir string, runner HostRunner) *Orchestrator
return orch
}
-// resolveStableBinary discovers the running executable, installs it into the
-// sibling bin/ directory as the stable launch binary, and returns the path to
-// launch instances with — falling back to the installed stable copy if the
-// running path is gone. All steps are best-effort: filesystem failures are
-// logged, not fatal. This isolates construction's disk side effects.
-func resolveStableBinary(baseDir string) string {
- binDir := filepath.Join(filepath.Dir(baseDir), "bin")
- stableBin := filepath.Join(binDir, "pinchtab")
- exe, _ := os.Executable()
- binary := exe
- if binary == "" {
- binary = os.Args[0]
- }
-
- if err := os.MkdirAll(binDir, 0755); err != nil {
- slog.Warn("failed to create bin directory", "path", binDir, "err", err)
- }
-
- if exe != "" {
- if err := installStableBinary(exe, stableBin); err != nil {
- slog.Warn("failed to install pinchtab binary", "path", stableBin, "err", err)
- } else {
- slog.Debug("installed pinchtab binary", "path", stableBin)
- }
- }
-
- if _, err := os.Stat(binary); err != nil {
- if _, stableErr := os.Stat(stableBin); stableErr == nil {
- binary = stableBin
- }
- }
-
- return binary
-}
-
// registerInstanceCleanupHook drops identity → instance bindings and any cached
// tab snapshots when an instance stops or errors, so a restarted instance does
// not keep receiving routed traffic and dashboards do not show ghost tabs.
@@ -364,20 +326,3 @@ func (o *Orchestrator) AllowsNetworkIntercept() bool {
func (o *Orchestrator) SetPortRange(start, end int) {
o.portAllocator = NewPortAllocator(start, end)
}
-
-func installStableBinary(src, dst string) error {
- in, err := os.Open(src)
- if err != nil {
- return err
- }
- defer func() { _ = in.Close() }()
-
- out, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
- if err != nil {
- return err
- }
- defer func() { _ = out.Close() }()
-
- _, err = io.Copy(out, in)
- return err
-}
diff --git a/internal/orchestrator/stable_binary.go b/internal/orchestrator/stable_binary.go
new file mode 100644
index 00000000..88daf8bf
--- /dev/null
+++ b/internal/orchestrator/stable_binary.go
@@ -0,0 +1,123 @@
+package orchestrator
+
+import (
+ "io"
+ "log/slog"
+ "os"
+ "path/filepath"
+ "runtime"
+)
+
+func resolveStableBinary(baseDir string) string {
+ binDir := filepath.Join(filepath.Dir(baseDir), "bin")
+ stableBin := filepath.Join(binDir, "pinchtab")
+ exe, _ := os.Executable()
+ running := exe
+ if running == "" {
+ running = os.Args[0]
+ }
+
+ if err := os.MkdirAll(binDir, 0755); err != nil {
+ slog.Warn("failed to create bin directory", "path", binDir, "err", err)
+ }
+
+ if exe != "" {
+ if err := installStableBinary(exe, stableBin); err != nil {
+ slog.Warn("failed to install pinchtab binary", "path", stableBin, "err", err)
+ } else {
+ slog.Debug("installed pinchtab binary", "path", stableBin)
+ }
+ }
+
+ return selectLaunchBinary(running, stableBin)
+}
+
+func selectLaunchBinary(running, stable string) string {
+ if isUsableBinary(running) {
+ return running
+ }
+ if isUsableBinary(stable) {
+ return stable
+ }
+ slog.Warn("stable pinchtab binary is missing, empty or not executable; launching from the running path instead",
+ "path", stable, "running", running)
+ return running
+}
+
+func isUsableBinary(path string) bool {
+ info, err := os.Stat(path)
+ if err != nil || !info.Mode().IsRegular() || info.Size() == 0 {
+ return false
+ }
+ return runtime.GOOS == "windows" || info.Mode().Perm()&0111 != 0
+}
+
+func installStableBinary(src, dst string) error {
+ srcInfo, err := os.Stat(src)
+ if err != nil {
+ return err
+ }
+ if matchesInstalledBinary(srcInfo, dst) {
+ return nil
+ }
+
+ tmp, err := stageStableBinary(src, srcInfo, dst)
+ if err != nil {
+ return err
+ }
+ if err := os.Rename(tmp, dst); err != nil {
+ _ = os.Remove(tmp)
+ return err
+ }
+ return nil
+}
+
+func matchesInstalledBinary(srcInfo os.FileInfo, dst string) bool {
+ dstInfo, err := os.Stat(dst)
+ if err != nil {
+ return false
+ }
+ return dstInfo.Size() == srcInfo.Size() && dstInfo.ModTime().Equal(srcInfo.ModTime())
+}
+
+func stageStableBinary(src string, srcInfo os.FileInfo, dst string) (staged string, err error) {
+ in, err := os.Open(src)
+ if err != nil {
+ return "", err
+ }
+ defer func() { _ = in.Close() }()
+
+ out, err := os.CreateTemp(filepath.Dir(dst), filepath.Base(dst)+".tmp-*")
+ if err != nil {
+ return "", err
+ }
+ tmp := out.Name()
+ defer func() {
+ if err != nil {
+ _ = os.Remove(tmp)
+ }
+ }()
+
+ if err = copyAndSeal(out, in); err != nil {
+ return "", err
+ }
+ if err = os.Chmod(tmp, 0755); err != nil {
+ return "", err
+ }
+ if err = os.Chtimes(tmp, srcInfo.ModTime(), srcInfo.ModTime()); err != nil {
+ return "", err
+ }
+ return tmp, nil
+}
+
+func copyAndSeal(out *os.File, in io.Reader) (err error) {
+ defer func() {
+ if closeErr := out.Close(); err == nil {
+ err = closeErr
+ }
+ }()
+ if _, err = io.Copy(out, in); err != nil {
+ return err
+ }
+ return out.Sync()
+}
diff --git a/internal/orchestrator/stable_binary_test.go b/internal/orchestrator/stable_binary_test.go
new file mode 100644
index 00000000..42446970
--- /dev/null
+++ b/internal/orchestrator/stable_binary_test.go
@@ -0,0 +1,211 @@
+package orchestrator
+
+import (
+ "os"
+ "path/filepath"
+ "runtime"
+ "strings"
+ "testing"
+)
+
+func writeBinary(t *testing.T, path, content string, mode os.FileMode) string {
+ t.Helper()
+ if err := os.WriteFile(path, []byte(content), mode); err != nil {
+ t.Fatalf("cannot write %s: %v", path, err)
+ }
+ if err := os.Chmod(path, mode); err != nil {
+ t.Fatalf("cannot chmod %s: %v", path, err)
+ }
+ return path
+}
+
+func readBinary(t *testing.T, path string) string {
+ t.Helper()
+ body, err := os.ReadFile(path)
+ if err != nil {
+ t.Fatalf("cannot read %s: %v", path, err)
+ }
+ return string(body)
+}
+
+func stagedTempFiles(t *testing.T, dir string) []string {
+ t.Helper()
+ entries, err := os.ReadDir(dir)
+ if err != nil {
+ t.Fatalf("cannot list %s: %v", dir, err)
+ }
+ var leftovers []string
+ for _, entry := range entries {
+ if strings.Contains(entry.Name(), ".tmp-") {
+ leftovers = append(leftovers, entry.Name())
+ }
+ }
+ return leftovers
+}
+
+func TestInstallStableBinaryPublishesTheWholeFileOrNothing(t *testing.T) {
+ dir := t.TempDir()
+ src := writeBinary(t, filepath.Join(dir, "running"), "the whole binary", 0755)
+ dst := filepath.Join(dir, "pinchtab")
+
+ if err := installStableBinary(src, dst); err != nil {
+ t.Fatalf("install failed: %v", err)
+ }
+
+ if got := readBinary(t, dst); got != "the whole binary" {
+ t.Errorf("destination holds %q, want the full source content", got)
+ }
+ info, err := os.Stat(dst)
+ if err != nil {
+ t.Fatalf("cannot stat the installed binary: %v", err)
+ }
+ if runtime.GOOS != "windows" && info.Mode().Perm()&0111 == 0 {
+ t.Errorf("installed binary has mode %v, want an executable bit", info.Mode().Perm())
+ }
+ if leftovers := stagedTempFiles(t, dir); len(leftovers) != 0 {
+ t.Errorf("install left staging files behind: %v", leftovers)
+ }
+}
+
+func TestInstallStableBinarySkipsAnUnchangedDestination(t *testing.T) {
+ dir := t.TempDir()
+ src := writeBinary(t, filepath.Join(dir, "running"), "the whole binary", 0755)
+ dst := filepath.Join(dir, "pinchtab")
+
+ if err := installStableBinary(src, dst); err != nil {
+ t.Fatalf("first install failed: %v", err)
+ }
+
+ srcInfo, err := os.Stat(src)
+ if err != nil {
+ t.Fatalf("cannot stat the source: %v", err)
+ }
+ const marker = "SAME LENGTH MARK"
+ writeBinary(t, dst, marker, 0755)
+ if err := os.Chtimes(dst, srcInfo.ModTime(), srcInfo.ModTime()); err != nil {
+ t.Fatalf("cannot align the destination mtime: %v", err)
+ }
+
+ if err := installStableBinary(src, dst); err != nil {
+ t.Fatalf("second install failed: %v", err)
+ }
+
+ if got := readBinary(t, dst); got != marker {
+ t.Errorf("destination holds %q, want the marker: a destination already matching the source must not be recopied on every orchestrator construction", got)
+ }
+}
+
+func TestInstallStableBinaryLeavesAGoodDestinationIntactWhenTheCopyFails(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ src func(t *testing.T, dir string) string
+ }{
+ {
+ name: "source cannot be opened",
+ src: func(_ *testing.T, dir string) string { return filepath.Join(dir, "gone") },
+ },
+ {
+ name: "source fails part-way through the copy",
+ src: func(t *testing.T, dir string) string {
+ unreadable := filepath.Join(dir, "unreadable")
+ if err := os.Mkdir(unreadable, 0755); err != nil {
+ t.Fatalf("cannot create %s: %v", unreadable, err)
+ }
+ return unreadable
+ },
+ },
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ dir := t.TempDir()
+ dst := writeBinary(t, filepath.Join(dir, "pinchtab"), "the previously installed binary", 0755)
+
+ if err := installStableBinary(tc.src(t, dir), dst); err == nil {
+ t.Fatal("install from a broken source reported success")
+ }
+
+ if got := readBinary(t, dst); got != "the previously installed binary" {
+ t.Errorf("destination holds %q, want the pre-existing binary untouched; a launch binary must never be observed empty or partial", got)
+ }
+ if leftovers := stagedTempFiles(t, dir); len(leftovers) != 0 {
+ t.Errorf("failed install left staging files behind: %v", leftovers)
+ }
+ })
+ }
+}
+
+func TestSelectLaunchBinaryRejectsAnUnusableStableCopy(t *testing.T) {
+ dir := t.TempDir()
+ missingRunning := filepath.Join(dir, "gone")
+
+ for _, tc := range []struct {
+ name string
+ stable func(t *testing.T) string
+ wantStable bool
+ windows bool
+ }{
+ {
+ name: "truncated stable binary",
+ stable: func(t *testing.T) string { return writeBinary(t, filepath.Join(t.TempDir(), "pinchtab"), "", 0755) },
+ wantStable: false,
+ windows: true,
+ },
+ {
+ name: "non-executable stable binary",
+ stable: func(t *testing.T) string { return writeBinary(t, filepath.Join(t.TempDir(), "pinchtab"), "real", 0644) },
+ wantStable: false,
+ },
+ {
+ name: "missing stable binary",
+ stable: func(t *testing.T) string { return filepath.Join(t.TempDir(), "pinchtab") },
+ wantStable: false,
+ windows: true,
+ },
+ {
+ name: "good stable binary",
+ stable: func(t *testing.T) string { return writeBinary(t, filepath.Join(t.TempDir(), "pinchtab"), "real", 0755) },
+ wantStable: true,
+ windows: true,
+ },
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ if !tc.windows && runtime.GOOS == "windows" {
+ t.Skip("permission bits are not modelled on windows")
+ }
+ stable := tc.stable(t)
+ got := selectLaunchBinary(missingRunning, stable)
+ want := missingRunning
+ if tc.wantStable {
+ want = stable
+ }
+ if got != want {
+ t.Errorf("selectLaunchBinary picked %q, want %q", got, want)
+ }
+ })
+ }
+}
+
+func TestSelectLaunchBinaryPrefersTheRunningExecutable(t *testing.T) {
+ dir := t.TempDir()
+ running := writeBinary(t, filepath.Join(dir, "running"), "running", 0755)
+ stable := writeBinary(t, filepath.Join(dir, "pinchtab"), "stable", 0755)
+
+ if got := selectLaunchBinary(running, stable); got != running {
+ t.Errorf("selectLaunchBinary picked %q, want the running executable %q", got, running)
+ }
+}
+
+func TestInstallStableBinaryDoesNotDestroyTheBinaryItWasLaunchedFrom(t *testing.T) {
+ dir := t.TempDir()
+ binary := writeBinary(t, filepath.Join(dir, "pinchtab"), "the running server image", 0755)
+
+ if err := installStableBinary(binary, binary); err != nil {
+ t.Fatalf("installing the binary over itself failed: %v", err)
+ }
+
+ if got := readBinary(t, binary); got != "the running server image" {
+ t.Errorf("binary holds %q after installing it over itself; a server launched from the stable path must not truncate its own image", got)
+ }
+ if leftovers := stagedTempFiles(t, dir); len(leftovers) != 0 {
+ t.Errorf("self-install left staging files behind: %v", leftovers)
+ }
+}