codeant-ai-for-open-source[bot] commented on code in PR #42803:
URL: https://github.com/apache/superset/pull/42803#discussion_r3732976832


##########
superset/extensions/__init__.py:
##########
@@ -154,7 +154,19 @@ def init_app(self, app: Flask) -> None:
 cache_manager = CacheManager()
 celery_app = celery.Celery()
 csrf = CSRFProtect()
-db = get_sqla_class()()
+
+# Flask-SQLAlchemy 3.x scopes db.session by the identity of the current Flask
+# app-context object (id(app_ctx)) rather than by thread/greenlet identity like
+# 2.x did. Superset's codebase (and its test fixtures) widely assumes a single
+# shared session per thread across nested `app.app_context()` blocks, often
+# relying on that implicit sharing instead of an explicit commit. Restoring the
+# 2.x scopefunc here keeps that assumption valid under FSA 3.x.
+try:
+    from greenlet import getcurrent as _session_scopefunc
+except ImportError:
+    from threading import get_ident as _session_scopefunc
+
+db = get_sqla_class()(session_options={"scopefunc": _session_scopefunc})

Review Comment:
   **Suggestion:** Using a greenlet/thread scope for Flask-SQLAlchemy makes 
nested application contexts share one scoped session, but Flask-SQLAlchemy 
removes that session whenever any nested context is torn down. The outer 
context then continues with a newly created session, losing its identity map 
and any uncommitted state. Keep the app-context scope or implement 
teardown-safe session ownership before changing the scope function. [stale 
reference]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ Nested contexts can roll back outer uncommitted database changes.
   - ⚠️ Outer code can retain detached or stale ORM objects.
   - ⚠️ Celery eager-mode and integration workflows use nested contexts.
   ```
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=d7b33e13298d4e9190a5506cf0406d1c&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=d7b33e13298d4e9190a5506cf0406d1c&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/extensions/__init__.py
   **Line:** 169:169
   **Comment:**
        *Stale Reference: Using a greenlet/thread scope for Flask-SQLAlchemy 
makes nested application contexts share one scoped session, but 
Flask-SQLAlchemy removes that session whenever any nested context is torn down. 
The outer context then continues with a newly created session, losing its 
identity map and any uncommitted state. Keep the app-context scope or implement 
teardown-safe session ownership before changing the scope function.
   
   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%2F42803&comment_hash=c81fd637c942effe3f80fcda8bacee680f167b3c20795b56f617dbb7042045f9&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42803&comment_hash=c81fd637c942effe3f80fcda8bacee680f167b3c20795b56f617dbb7042045f9&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]

Reply via email to