Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend): handle UniqueViolationError in workspace file retry path (#12267)

Ottob74d41d50cec
diff --git a/autogpt_platform/backend/backend/util/workspace.py b/autogpt_platform/backend/backend/util/workspace.py
index e855040abf..8287f4411d 100644
--- a/autogpt_platform/backend/backend/util/workspace.py
+++ b/autogpt_platform/backend/backend/util/workspace.py
@@ -228,52 +228,43 @@ class WorkspaceManager:
# Create database record - handle race condition where another request
# created a file at the same path between our check and create
- try:
- file = await db.create_workspace_file(
- workspace_id=self.workspace_id,
- file_id=file_id,
- name=filename,
- path=path,
- storage_path=storage_path,
- mime_type=mime_type,
- size_bytes=len(content),
- checksum=checksum,
- )
- except UniqueViolationError:
- # Race condition: another request created a file at this path
- if overwrite:
- # Re-fetch and delete the conflicting file, then retry
- existing = await db.get_workspace_file_by_path(self.workspace_id, path)
- if existing:
- await self.delete_file(existing.id)
- # Retry the create - if this also fails, clean up storage file
- try:
- file = await db.create_workspace_file(
- workspace_id=self.workspace_id,
- file_id=file_id,
- name=filename,
- path=path,
- storage_path=storage_path,
- mime_type=mime_type,
- size_bytes=len(content),
- checksum=checksum,
+ async def _persist_db_record(
+ retries: int = 2 if overwrite else 0,
+ ) -> WorkspaceFile:
+ """Create DB record, retrying on conflict if overwrite=True.
+
+ Cleans up the orphaned storage file on any failure.
+ """
+ try:
+ return await db.create_workspace_file(
+ workspace_id=self.workspace_id,
+ file_id=file_id,
+ name=filename,
+ path=path,
+ storage_path=storage_path,
+ mime_type=mime_type,
+ size_bytes=len(content),
+ checksum=checksum,
+ )
+ except UniqueViolationError:
+ if retries > 0:
+ # Delete conflicting file and retry
+ existing = await db.get_workspace_file_by_path(
+ self.workspace_id, path
)
- except Exception:
- # Clean up orphaned storage file on retry failure
- try:
- await storage.delete(storage_path)
- except Exception as e:
- logger.warning(f"Failed to clean up orphaned storage file: {e}")
- raise
- else:
- # Clean up the orphaned storage file before raising
- try:
- await storage.delete(storage_path)
- except Exception as e:
- logger.warning(f"Failed to clean up orphaned storage file: {e}")
+ if existing:
+ await self.delete_file(existing.id)
+ return await _persist_db_record(retries=retries - 1)
+ if overwrite:
+ raise ValueError(
+ f"Unable to overwrite file at path: {path} "
+ f"(concurrent write conflict)"
+ ) from None
raise ValueError(f"File already exists at path: {path}")
+
+ try:
+ file = await _persist_db_record()
except Exception:
- # Any other database error (connection, validation, etc.) - clean up storage
try:
await storage.delete(storage_path)
except Exception as e:
diff --git a/autogpt_platform/backend/backend/util/workspace_test.py b/autogpt_platform/backend/backend/util/workspace_test.py
new file mode 100644
index 0000000000..dcdbda648e
--- /dev/null
+++ b/autogpt_platform/backend/backend/util/workspace_test.py
@@ -0,0 +1,158 @@
+"""
+Tests for WorkspaceManager.write_file UniqueViolationError handling.
+"""
+
+from datetime import datetime, timezone
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+from prisma.errors import UniqueViolationError
+
+from backend.data.workspace import WorkspaceFile
+from backend.util.workspace import WorkspaceManager
+
+_NOW = datetime(2024, 1, 1, tzinfo=timezone.utc)
+
+
+def _make_workspace_file(
+ id: str = "existing-file-id",
+ workspace_id: str = "ws-123",
+ name: str = "test.txt",
+ path: str = "/test.txt",
+ storage_path: str = "ws-123/existing-uuid/test.txt",
+ mime_type: str = "text/plain",
+ size_bytes: int = 5,
+ checksum: str = "abc123",
+) -> WorkspaceFile:
+ """Create a mock WorkspaceFile with sensible defaults."""
+ return WorkspaceFile(
+ id=id,
+ workspace_id=workspace_id,
+ name=name,
+ path=path,
+ storage_path=storage_path,
+ mime_type=mime_type,
+ size_bytes=size_bytes,
+ checksum=checksum,
+ metadata={},
+ created_at=_NOW,
+ updated_at=_NOW,
+ )
+
+
+def _unique_violation() -> UniqueViolationError:
+ """Create a UniqueViolationError for testing."""
+ data = {
+ "user_facing_error": {
+ "message": "Unique constraint failed on the fields: (`path`)",
+ }
+ }
+ return UniqueViolationError(data)
+
+
+def manager():
+ return WorkspaceManager(user_id="user-123", workspace_id="ws-123")
+
+
+def mock_storage():
+ storage = AsyncMock()
+ storage.store.return_value = "ws-123/some-uuid/test.txt"
+ storage.delete = AsyncMock()
+ return storage
+
+
+def mock_db():
+ """Create a mock workspace_db() return value."""
+ db = MagicMock()
+ db.create_workspace_file = AsyncMock()
+ db.get_workspace_file_by_path = AsyncMock()
+ db.get_workspace_file = AsyncMock()
+ return db
+
+
+async def test_write_file_no_overwrite_unique_violation_raises_and_cleans_up(
+ manager, mock_storage, mock_db
+):
+ """overwrite=False + UniqueViolationError → ValueError + storage cleanup."""
+ mock_db.get_workspace_file_by_path.return_value = None
+ mock_db.create_workspace_file.side_effect = _unique_violation()
+
+ with (
+ patch(
+ "backend.util.workspace.get_workspace_storage",
+ return_value=mock_storage,
+ ),
+ patch("backend.util.workspace.workspace_db", return_value=mock_db),
+ patch("backend.util.workspace.scan_content_safe", new_callable=AsyncMock),
+ ):
+ with pytest.raises(ValueError, match="File already exists"):
+ await manager.write_file(
+ filename="test.txt", content=b"hello", overwrite=False
+ )
+
+ mock_storage.delete.assert_called_once()
+
+
+async def test_write_file_overwrite_conflict_then_retry_succeeds(
+ manager, mock_storage, mock_db
+):
+ """overwrite=True + conflict → delete existing → retry succeeds."""
+ created_file = _make_workspace_file()
+ existing_file = _make_workspace_file(id="old-id")
+
+ mock_db.get_workspace_file_by_path.return_value = existing_file
+ mock_db.create_workspace_file.side_effect = [_unique_violation(), created_file]
+
+ with (
+ patch(
+ "backend.util.workspace.get_workspace_storage",
+ return_value=mock_storage,
+ ),
+ patch("backend.util.workspace.workspace_db", return_value=mock_db),
+ patch("backend.util.workspace.scan_content_safe", new_callable=AsyncMock),
+ patch.object(manager, "delete_file", new_callable=AsyncMock) as mock_delete,
+ ):
+ result = await manager.write_file(
+ filename="test.txt", content=b"hello", overwrite=True
+ )
+
+ assert result == created_file
+ mock_delete.assert_called_once_with("old-id")
+ mock_storage.delete.assert_not_called()
+
+
+async def test_write_file_overwrite_exhausted_retries_raises_and_cleans_up(
+ manager, mock_storage, mock_db
+):
+ """overwrite=True + all retries exhausted → ValueError + cleanup."""
+ existing_file = _make_workspace_file(id="old-id")
+
+ mock_db.get_workspace_file_by_path.return_value = existing_file
+ # Initial + 2 retries = 3 UniqueViolationErrors
+ mock_db.create_workspace_file.side_effect = [
+ _unique_violation(),
+ _unique_violation(),
+ _unique_violation(),
+ ]
+
+ with (
+ patch(
+ "backend.util.workspace.get_workspace_storage",
+ return_value=mock_storage,
+ ),
+ patch("backend.util.workspace.workspace_db", return_value=mock_db),
+ patch("backend.util.workspace.scan_content_safe", new_callable=AsyncMock),
+ patch.object(manager, "delete_file", new_callable=AsyncMock),
+ ):
+ with pytest.raises(ValueError, match="Unable to overwrite.*concurrent write"):
+ await manager.write_file(
+ filename="test.txt", content=b"hello", overwrite=True
+ )
+
+ mock_storage.delete.assert_called_once()