aminghadersohi commented on code in PR #44351:
URL: https://github.com/apache/superset/pull/44351#discussion_r4053415882


##########
superset/mcp_service/chart/tool/get_chart_sql.py:
##########
@@ -509,6 +521,15 @@ async def _handle_chart_sql_request(
             error_type="Unsupported",
         )
 
+    # Eagerly refresh all attributes while the session is still active. The
+    # log_context commit above (DBEventLogger.log) expires this instance via
+    # SQLAlchemy's expire_on_commit, and the awaits below are real event-loop
+    # yield points after which the MCP per-call session can be torn down —
+    # a later lazy reload (e.g. chart.params in _resolve_effective_form_data,
+    # chart.query_context in _sql_from_saved_query_context) would then raise
+    # DetachedInstanceError. See get_chart_preview.py for the same pattern.
+    db.session.refresh(chart)

Review Comment:
   `chart.datasource_type` on L518 runs before this and already triggers the 
full unexpire load — measured on SQLAlchemy 2.0.52: one column SELECT plus the 
`table` subquery-eager SELECT, `__dict__` fully repopulated. What does 
`refresh()` add here beyond repeating those two round-trips?



##########
superset/mcp_service/chart/tool/get_chart_sql.py:
##########
@@ -454,6 +455,17 @@ async def get_chart_sql(
 
     try:
         return await _handle_chart_sql_request(request, ctx)
+    except SQLAlchemyError as e:
+        await ctx.error(
+            "Chart SQL retrieval failed due to database session error: "
+            "identifier=%s, error_type=%s, error=%s"
+            % (request.identifier, type(e).__name__, str(e))
+        )
+        logger.exception("SQLAlchemy error in get_chart_sql: %s", e)
+        return ChartError(
+            error=f"Chart SQL retrieval failed due to a database session 
error: {e}",

Review Comment:
   `StatementError.__str__` appends `[SQL: ...]` and `[parameters: (...)]`, so 
metadata-DB statements and bound values reach the MCP client. `ctx.error` and 
`logger.exception` above already retain the full text server-side.
   
   ```suggestion
               error="Chart SQL retrieval failed due to a database session 
error: "
               f"{type(e).__name__}",
   ```



-- 
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