bitflicker64 commented on code in PR #439:
URL: https://github.com/apache/hugegraph-ai/pull/439#discussion_r4658146722


##########
ontogeny/server/ontogeny/api/routes_objects.py:
##########
@@ -203,18 +204,34 @@ async def execute_action(
         started = time.perf_counter()
         from ..errors import PolicyDeniedError, RuleRejectedError
 
-        async with sc.sessionmaker() as s:
+        # Keep same-key executions serialized until the revision is committed;
+        # exiting the session first also rolls back failures before unlocking.
+        async with (
+            sc.runtime.idempotency_guard(action_name, principal, 
body.idempotency_key),
+            sc.sessionmaker() as s,
+        ):
             try:
-                rev = await sc.runtime.execute(
-                    s,
-                    action_name,
-                    principal,
-                    body.parameters,
-                    body.target_id,
-                    expected_revision=body.expected_revision,
-                    idempotency_key=body.idempotency_key,
-                )
-                await s.commit()
+                try:
+                    rev = await sc.runtime.execute(
+                        s,
+                        action_name,
+                        principal,
+                        body.parameters,
+                        body.target_id,
+                        expected_revision=body.expected_revision,
+                        idempotency_key=body.idempotency_key,
+                    )
+                    await s.commit()
+                except IntegrityError:
+                    # The per-key lock only serializes one process. When 
another
+                    # worker wins the same key, roll back and resolve the 
winner's
+                    # committed revision instead of surfacing a 500.
+                    if not body.idempotency_key:
+                        raise
+                    await s.rollback()
+                    rev = await sc.runtime.await_committed_revision(s, 
action_name, principal, body.idempotency_key)

Review Comment:
   Minor: `uq_revision_idem` covers `idempotency_key` alone, so when another 
action or principal already owns the key, `await_committed_revision` polls 20 
times for a revision its scoped lookup can never find and returns the same 500 
about one second later while holding the connection and the key lock. Look the 
conflicting key up by `idempotency_key` alone and raise `ConflictError` when it 
belongs to another scope or a non-executed outcome, instead of polling.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to