codeant-ai-for-open-source[bot] commented on code in PR #42472:
URL: https://github.com/apache/superset/pull/42472#discussion_r3673853444
##########
superset/subjects/utils.py:
##########
@@ -176,6 +296,80 @@ def get_or_create_role_subject(role_id: int) -> Subject |
None:
return get_role_subject(role_id)
+def get_group_subject(group_id: int) -> Subject | None:
+ """Get the GROUP-type Subject for a given group ID."""
+ return (
+ db.session.query(Subject)
+ .filter_by(
+ group_id=group_id,
+ type=SubjectType.GROUP,
+ )
+ .first()
+ )
+
+
+def get_or_create_group_subject(group_id: int) -> Subject | None:
+ """Get or create the GROUP-type Subject for a given group ID.
+
+ Mirrors ``get_or_create_role_subject``: a group that exists but has no
+ Subject row yet (e.g. created before the sync hooks were installed and not
+ yet backfilled) is synced on demand rather than skipped. Returns ``None``
+ only when no such group exists.
+
+ The insert runs inside a SAVEPOINT. ``Subject.group_id`` is unique, so a
+ concurrent request backfilling the same group would otherwise fail this
+ flush with an ``IntegrityError`` and poison the surrounding transaction. On
+ that conflict the savepoint rolls back and the row the other request wrote
+ is reloaded. Any ``IntegrityError`` that did *not* leave a matching row is
+ re-raised rather than swallowed — it isn't the expected unique-key race, so
+ masking it would silently drop the group from the resulting viewer set.
+ """
+ if subject := get_group_subject(group_id):
+ return subject
+
+ from sqlalchemy.exc import IntegrityError
+
+ from superset import security_manager
+ from superset.subjects.sync import sync_group_subject
+
+ group = db.session.get(security_manager.group_model, group_id)
+ if not group:
+ return None
+
+ try:
+ with db.session.begin_nested():
+ sync_group_subject(group)
+ except IntegrityError:
Review Comment:
**Suggestion:** The nested transaction does not isolate this sync from
pending work in the surrounding session: SQLAlchemy flushes pending objects
when entering `begin_nested()`, before the SAVEPOINT is established. During an
import or asset creation, unrelated pending inserts and ORM event handlers can
therefore execute before the savepoint, and an unrelated flush `IntegrityError`
is caught as though it were a group-subject uniqueness race. Establish the
savepoint before introducing pending work, or avoid flushing unrelated session
state while resolving the subject. [possible bug]
<details>
<summary><b>Severity Level:</b> Major ⚠️</summary>
```mdx
- ❌ Asset creation can fail on unrelated pending integrity errors.
- ⚠️ Import transactions may enter failed-session state.
- ⚠️ Subject race handling can mask the original failure.
```
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=2957ca055fb4457ea3b839ad1a976a92&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
[](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=2957ca055fb4457ea3b839ad1a976a92&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** superset/subjects/utils.py
**Line:** 339:342
**Comment:**
*Possible Bug: The nested transaction does not isolate this sync from
pending work in the surrounding session: SQLAlchemy flushes pending objects
when entering `begin_nested()`, before the SAVEPOINT is established. During an
import or asset creation, unrelated pending inserts and ORM event handlers can
therefore execute before the savepoint, and an unrelated flush `IntegrityError`
is caught as though it were a group-subject uniqueness race. Establish the
savepoint before introducing pending work, or avoid flushing unrelated session
state while resolving the subject.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42472&comment_hash=6fabeecf9dda9f423039393bd623b915881027e70a665cef65b0fbba573568c2&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42472&comment_hash=6fabeecf9dda9f423039393bd623b915881027e70a665cef65b0fbba573568c2&reaction=dislike'>👎</a>
--
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]