High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: targeted session file removal with platform-specific retry
Dataadd7a1f087a5
diff --git a/internal/bridge/state.go b/internal/bridge/state.goindex bdaaefab..88bf5736 100644--- a/internal/bridge/state.go+++ b/internal/bridge/state.go@@ -3,6 +3,7 @@ package bridgeimport ("context""encoding/json"+ "fmt""log/slog""os""path/filepath"@@ -66,33 +67,56 @@ func WasUncleanExit(profileDir string) bool {return strings.Contains(prefs, `"exit_type":"Crashed"`) || strings.Contains(prefs, `"exit_type": "Crashed"`)}+// sessionRestoreFiles are the specific files Chrome uses for tab restore.+// Deleting only these is faster and less likely to hit locks than nuking+// the entire Sessions directory.+var sessionRestoreFiles = []string{+ "Current Session",+ "Current Tabs",+ "Last Session",+ "Last Tabs",+}+func ClearChromeSessions(profileDir string) {sessionsDir := filepath.Join(profileDir, "Default", "Sessions")- // Retry with backoff on Windows where file locks may persist after Chrome exit- const maxRetries = 3- const retryDelayMs = 100+ if _, err := os.Stat(sessionsDir); os.IsNotExist(err) {+ return+ }++ var failed []string+ for _, name := range sessionRestoreFiles {+ p := filepath.Join(sessionsDir, name)+ if err := retryRemove(p, 3); err != nil {+ failed = append(failed, name)+ slog.Warn("failed to remove session file", "file", name, "err", err)+ }+ }+ if len(failed) == 0 {+ slog.Info("cleared Chrome session restore files")+ }+}++// retryRemove attempts to remove a single file with exponential backoff.+// This handles Windows file lock errors where handles persist briefly+// after Chrome exits.+func retryRemove(path string, maxRetries int) error {var err errorfor attempt := 0; attempt < maxRetries; attempt++ {if attempt > 0 {- // Small delay before retry to allow file handles to be released- time.Sleep(time.Duration(retryDelayMs) * time.Millisecond)+ time.Sleep(time.Duration(50*(1<<uint(attempt))) * time.Millisecond) // 100ms, 200ms, ...}-- err = os.RemoveAll(sessionsDir)- if err == nil {- slog.Info("cleared Chrome sessions dir (prevent tab restore hang)")- return+ err = os.Remove(path)+ if err == nil || os.IsNotExist(err) {+ return nil}-- if attempt < maxRetries-1 {- slog.Debug("failed to clear Chrome sessions dir, retrying", "attempt", attempt+1, "err", err)+ if !isLockError(err) {+ return err}+ slog.Debug("file locked, retrying remove", "path", filepath.Base(path), "attempt", attempt+1)}-- // Log final error if all retries failed- slog.Warn("failed to clear Chrome sessions dir after retries", "err", err)+ return fmt.Errorf("still locked after %d attempts: %w", maxRetries, err)}func (b *Bridge) SaveState() {diff --git a/internal/bridge/state_lock_other.go b/internal/bridge/state_lock_other.gonew file mode 100644index 00000000..121a5e45--- /dev/null+++ b/internal/bridge/state_lock_other.go@@ -0,0 +1,9 @@+//go:build !windows++package bridge++// isLockError on non-Windows platforms always returns false — file locks+// after Chrome exit are a Windows-specific issue.+func isLockError(_ error) bool {+ return false+}diff --git a/internal/bridge/state_lock_windows.go b/internal/bridge/state_lock_windows.gonew file mode 100644index 00000000..53dcf67e--- /dev/null+++ b/internal/bridge/state_lock_windows.go@@ -0,0 +1,24 @@+//go:build windows++package bridge++import (+ "errors"+ "syscall"+)++// Windows error codes for file locking.+const (+ errSharingViolation syscall.Errno = 32 // ERROR_SHARING_VIOLATION+ errLockViolation syscall.Errno = 33 // ERROR_LOCK_VIOLATION+ errAccessDenied syscall.Errno = 5 // ERROR_ACCESS_DENIED+)++// isLockError reports whether err is a Windows file-lock error worth retrying.+func isLockError(err error) bool {+ var errno syscall.Errno+ if errors.As(err, &errno) {+ return errno == errSharingViolation || errno == errLockViolation || errno == errAccessDenied+ }+ return false+}diff --git a/internal/bridge/state_test.go b/internal/bridge/state_test.goindex 61db1e96..b5d04b3b 100644--- a/internal/bridge/state_test.go+++ b/internal/bridge/state_test.go@@ -161,12 +161,26 @@ func TestClearChromeSessions(t *testing.T) {tmp := t.TempDir()sessionsDir := filepath.Join(tmp, "Default", "Sessions")_ = os.MkdirAll(sessionsDir, 0755)- _ = os.WriteFile(filepath.Join(sessionsDir, "Session_1"), []byte("data"), 0644)++ // Create the specific session restore files+ for _, name := range sessionRestoreFiles {+ _ = os.WriteFile(filepath.Join(sessionsDir, name), []byte("data"), 0644)+ }+ // Also create an unrelated file that should NOT be deleted+ _ = os.WriteFile(filepath.Join(sessionsDir, "Session_1"), []byte("other"), 0644)ClearChromeSessions(tmp)- if _, err := os.Stat(sessionsDir); !os.IsNotExist(err) {- t.Error("expected Sessions dir to be removed")+ // Session restore files should be gone+ for _, name := range sessionRestoreFiles {+ if _, err := os.Stat(filepath.Join(sessionsDir, name)); !os.IsNotExist(err) {+ t.Errorf("expected %s to be removed", name)+ }+ }++ // Unrelated files should still exist+ if _, err := os.Stat(filepath.Join(sessionsDir, "Session_1")); err != nil {+ t.Error("expected unrelated Session_1 file to remain")}}@@ -182,3 +196,24 @@ func TestClearChromeSessions_MissingDir(t *testing.T) {t.Error("expected Sessions dir to not exist")}}++func TestRetryRemove_NonExistent(t *testing.T) {+ err := retryRemove("/tmp/does-not-exist-at-all", 3)+ if err != nil {+ t.Errorf("expected nil for non-existent file, got: %v", err)+ }+}++func TestRetryRemove_Success(t *testing.T) {+ tmp := t.TempDir()+ p := filepath.Join(tmp, "testfile")+ _ = os.WriteFile(p, []byte("data"), 0644)++ err := retryRemove(p, 3)+ if err != nil {+ t.Errorf("expected nil, got: %v", err)+ }+ if _, err := os.Stat(p); !os.IsNotExist(err) {+ t.Error("expected file to be removed")+ }+}