aminghadersohi opened a new pull request, #38934: URL: https://github.com/apache/superset/pull/38934
## **User description** ### SUMMARY Fixes cascading `PendingRollbackError` in MCP service tools after PostgreSQL SSL connection drops. When SSL connections drop unexpectedly (`psycopg2.OperationalError: SSL connection has been closed unexpectedly`), the SQLAlchemy session becomes poisoned. Because MCP tools catch exceptions and return error responses (rather than re-raising), `mcp_auth_hook`'s `except` block never fires, so the poisoned session leaks into subsequent tool calls causing cascading `PendingRollbackError`. **Two-layer fix:** - **Layer 1 — Tool-level rollback**: Added `db.session.rollback()` (wrapped in `try/except SQLAlchemyError` for secondary failure protection) to outer except blocks in `generate_dashboard`, `generate_chart`, `update_chart`, and `add_chart_to_existing_dashboard`. - **Layer 2 — Systemic safety net**: Added `_ensure_session_cleanup()` in a `finally` block of `mcp_auth_hook` that always cleans up the session after every tool call, even when tools catch their own exceptions. Also improves test mocks: uses `side_effect` for consecutive `DashboardDAO.find_by_id` calls and rewrites eager loading tests to call actual tool functions via MCP client. ### TESTING INSTRUCTIONS ```bash pytest tests/unit_tests/mcp_service/ -x -q ``` ### ADDITIONAL INFORMATION - [ ] Has associated issue: - [ ] Required feature flags: - [ ] Changes UI - [ ] Includes DB Migration (follow approval process in [SIP-59](https://github.com/apache/superset/issues/13351)) - [ ] Migration is atomic, supports rollback & is backwards-compatible - [ ] Confirm DB migration upgrade and downgrade tested - [ ] Runtime estimates and downtime expectations provided - [ ] Introduces new feature or API - [ ] Removes existing feature or API ___ ## **CodeAnt-AI Description** **Keep MCP tools working after database connection drops** ### What Changed - MCP tool calls now clean up the database session after each run, so a failed request does not poison the next one. - Dashboard and chart actions now roll back the session when they hit database-related errors, reducing cascading failures after SSL disconnects or other connection issues. - Tests were updated to cover the new cleanup flow and the fallback behavior when a dashboard re-fetch fails. ### Impact `✅ Fewer cascading MCP tool failures` `✅ Clearer recovery after SSL connection drops` `✅ More reliable dashboard and chart actions after errors` <details> <summary><strong>💡 Usage Guide</strong></summary> ### Checking Your Pull Request Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later. ### Talking to CodeAnt AI Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask: <pre> <code>@codeant-ai ask: Your question here</code> </pre> This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code. #### Example <pre> <code>@codeant-ai ask: Can you suggest a safer alternative to storing this secret?</code> </pre> ### Preserve Org Learnings with CodeAnt You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input: <pre> <code>@codeant-ai: Your feedback here</code> </pre> This helps CodeAnt AI learn and adapt to your team's coding style and standards. #### Example <pre> <code>@codeant-ai: Do not flag unused imports.</code> </pre> ### Retrigger review Ask CodeAnt AI to review the PR again, by typing: <pre> <code>@codeant-ai: review</code> </pre> ### Check Your Repository Health To analyze the health of your code repository, visit our dashboard at [https://app.codeant.ai](https://app.codeant.ai). This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health. </details> -- 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]
