Repository navigation
Conversation
When an insert fails on a duplicate task name, assume another request created the task first. Look up the existing task and return it through the normal existing-task path, applying the losing request's params to the winner's task. Duplicate errors on unnamed tasks are re-raised as before.
Comment on lines
+183
to
+185
| existing = ( | ||
| await session.execute(select(TaskORM).where(TaskORM.name == task.name)) | ||
| ).scalar_one() |
Contributor
There was a problem hiding this comment.
Concurrent task creation still fails
When two requests insert the same new task name, the losing request's session.flush() raises IntegrityError and leaves the session unusable until rollback. The next session.execute() raises PendingRollbackError, so the caller gets HTTP 500 instead of the winning task. Roll back before the lookup, or isolate the insert in a savepoint.
Prompt To Fix With AI
This is a comment left during a code review.
Path: agentex/src/domain/repositories/task_repository.py
Line: 183-185
Comment:
**Concurrent task creation still fails**
When two requests insert the same new task name, the losing request's `session.flush()` raises `IntegrityError` and leaves the session unusable until rollback. The next `session.execute()` raises `PendingRollbackError`, so the caller gets HTTP 500 instead of the winning task. Roll back before the lookup, or isolate the insert in a savepoint.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
task/createis get-or-create keyed on the taskname, but the lookup and the insert aren't atomic. If two requests with the same new name arrive at the same time (for example a client retry or a double submit), both miss the lookup and both try to insert. The unique constraint ontasks.namerejects the second insert, so that caller gets a400 Item already existsinstead of the existing task the API promises to return.Fix
When an insert fails on a duplicate task name, assume another request created the task first:
INTERRUPTEDtask and applies the newparams, the same as for any existing task.Implementation
Modified
TaskRepository.create()inagentex/src/domain/repositories/task_repository.pyto catchIntegrityErroron the uniquetasks.nameconstraint. When a duplicate name is detected, the existing task is looked up and returned, with the losing request's params merged into the winner's task.Fix the failed-session lookup before merging; concurrent named requests still return an error.
Fix with agent prompt
Summary
This PR adds a duplicate-name recovery branch to
TaskRepository.create.Diagram
sequenceDiagram participant A as First request participant B as Concurrent request participant DB as PostgreSQL A->>DB: Insert named task and commit B->>DB: Insert the same name DB-->>B: IntegrityError B->>B: Query without rollback B-->>B: PendingRollbackErrorReviews (1) · Last reviewed commit: "fix: make named task/create idempotent u..." · Reviewed by Greptile