Skip to content

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

fix: failed profile import leaves a partial directory that blocks every retry

Luigi Agosti59cf083c985e
diff --git a/internal/profiles/import_cleanup_test.go b/internal/profiles/import_cleanup_test.go
new file mode 100644
index 00000000..934efad1
--- /dev/null
+++ b/internal/profiles/import_cleanup_test.go
@@ -0,0 +1,71 @@
+package profiles
+
+import (
+ "errors"
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+// A live Chrome user data dir contains Singleton* symlinks, which copyDir
+// deliberately refuses. The copy fails partway through, so Import must not
+// leave the half-copied destination behind — otherwise the profile name is
+// permanently blocked by a directory-collision error on every retry.
+func TestImportCleansUpPartialCopyOnFailure(t *testing.T) {
+ src := t.TempDir()
+ if err := os.WriteFile(filepath.Join(src, "Preferences"), []byte("{}"), 0600); err != nil {
+ t.Fatal(err)
+ }
+ // Sorts after "Preferences", so a chunk of the profile is copied first.
+ if err := os.Symlink(filepath.Join(src, "Preferences"), filepath.Join(src, "SingletonLock")); err != nil {
+ t.Skipf("symlinks unavailable: %v", err)
+ }
+
+ base := t.TempDir()
+ pm := NewProfileManager(base)
+
+ if err := pm.Import("work", src); err == nil {
+ t.Fatal("Import should fail on the Singleton symlink")
+ }
+
+ dest := filepath.Join(base, profileID("work"))
+ if _, err := os.Stat(dest); err == nil {
+ t.Errorf("failed import left a partial profile directory at %s", dest)
+ }
+
+ retryErr := pm.Import("work", src)
+ if retryErr == nil {
+ t.Fatal("retry should fail the same way")
+ }
+ if errors.Is(retryErr, ErrProfileDirExists) || errors.Is(retryErr, ErrProfileExists) {
+ t.Errorf("retry blocked by leftover state instead of reporting the real cause: %v", retryErr)
+ }
+}
+
+// A successful import must still land, so the cleanup cannot be over-eager.
+func TestImportSucceedsAndIsRetryableAfterDelete(t *testing.T) {
+ src := t.TempDir()
+ if err := os.MkdirAll(filepath.Join(src, "Default"), 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(src, "Default", "Preferences"), []byte("{}"), 0600); err != nil {
+ t.Fatal(err)
+ }
+
+ base := t.TempDir()
+ pm := NewProfileManager(base)
+
+ if err := pm.Import("work", src); err != nil {
+ t.Fatalf("Import: %v", err)
+ }
+ if _, err := os.Stat(filepath.Join(base, profileID("work"), "Default", "Preferences")); err != nil {
+ t.Fatalf("imported profile missing copied content: %v", err)
+ }
+
+ if err := pm.Delete("work"); err != nil {
+ t.Fatalf("Delete: %v", err)
+ }
+ if err := pm.Import("work", src); err != nil {
+ t.Fatalf("re-import after delete: %v", err)
+ }
+}
diff --git a/internal/profiles/profiles_crud.go b/internal/profiles/profiles_crud.go
index 05a275e1..ddad6493 100644
--- a/internal/profiles/profiles_crud.go
+++ b/internal/profiles/profiles_crud.go
@@ -62,17 +62,26 @@ func (pm *ProfileManager) Import(name, sourcePath string) error {
}
slog.Info("importing profile", "name", name, "source", resolvedSourcePath)
+ // Import is all-or-nothing. preflight already established that dest did not
+ // exist, so anything under it now is ours to remove — and leaving a partial
+ // copy would make every retry fail with "already exists" instead of the real
+ // cause (a live Chrome user data dir has Singleton* symlinks copyDir rejects).
if err := copyDir(resolvedSourcePath, dest); err != nil {
+ _ = os.RemoveAll(dest)
return fmt.Errorf("copy failed: %w", err)
}
if err := os.WriteFile(filepath.Join(dest, ".pinchtab-imported"), []byte(resolvedSourcePath), 0600); err != nil {
slog.Warn("failed to write import marker", "err", err)
}
- return writeProfileMeta(dest, ProfileMeta{
+ if err := writeProfileMeta(dest, ProfileMeta{
ID: profileID(name),
Name: name,
- })
+ }); err != nil {
+ _ = os.RemoveAll(dest)
+ return err
+ }
+ return nil
}
func (pm *ProfileManager) ImportWithMeta(name, sourcePath string, meta ProfileMeta) error {