aminghadersohi opened a new pull request, #39921:
URL: https://github.com/apache/superset/pull/39921

   ### SUMMARY
   
   `get_chart_preview` intermittently raised `DetachedInstanceError` when a 
`Slice` ORM object was loaded by `find_chart_by_identifier()` and then accessed 
after a session expire inside `validate_chart_dataset()`.
   
   Two changes:
   
   1. **Eager session refresh** — call `db.session.refresh(chart)` immediately 
after loading the `Slice` so all column attributes are populated in the 
object's `__dict__` while the session is still active. This prevents 
`DetachedInstanceError` when strategy classes later access lazy attributes.
   
   2. **SQLAlchemy error handler** — add an explicit `except SQLAlchemyError` 
block inside `_get_chart_preview_internal` so any residual session errors (e.g. 
`DetachedInstanceError`) produce a clear `ChartError` with a "database session 
error, please retry" message instead of propagating to the outer wrapper with a 
generic message.
   
   ### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
   
   N/A — backend-only fix.
   
   ### TESTING INSTRUCTIONS
   
   1. Unit tests added in 
`tests/unit_tests/mcp_service/chart/tool/test_get_chart_preview.py::TestDetachedInstanceError`:
      - `test_session_refresh_called_after_chart_load` — verifies 
`db.session.refresh()` is called once after chart lookup
      - `test_detached_instance_error_returns_chart_error` — verifies 
`DetachedInstanceError` raised inside the preview strategy returns a 
`ChartError` (not an unhandled exception)
   2. Run: `pytest 
tests/unit_tests/mcp_service/chart/tool/test_get_chart_preview.py::TestDetachedInstanceError
 -xvs`
   
   ### ADDITIONAL INFORMATION
   - [ ] Has associated issue:
   - [ ] Required feature flags:
   - [ ] Changes UI
   - [ ] Includes DB Migration
   - [ ] Introduces new feature or API
   - [ ] Removes existing feature or API


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