codeant-ai-for-open-source[bot] commented on code in PR #41921:
URL: https://github.com/apache/superset/pull/41921#discussion_r3627368746
##########
superset/mcp_service/middleware.py:
##########
@@ -363,6 +506,14 @@ async def on_call_tool(
mcp_call_id,
extra,
)
+ await self._emit_call_metrics(
+ context,
+ tool_name,
+ mcp_tool,
+ success=success,
+ raised_is_user_error=raised_is_user_error,
+ duration_ms=duration_ms,
+ )
Review Comment:
**Suggestion:** The metrics emission is awaited directly inside the
`finally` block without protection, so any runtime failure in stats logging
(for example, backend outage or client error) will raise a new exception and
can mask the original tool result/exception. Wrap this call in its own
`try/except` and swallow/log metric failures so observability side effects
never alter MCP tool behavior. [possible bug]
<details>
<summary><b>Severity Level:</b> Major ⚠️</summary>
```mdx
- ❌ MCP tool calls can fail when stats logger misbehaves.
- ⚠️ Original ToolError replaced by metrics RuntimeError in finally.
- ⚠️ Downstream MCP clients see spurious failures unrelated to tools.
```
</details>
<details>
<summary><b>Steps of Reproduction ✅ </b></summary>
```mdx
1. In `superset/mcp_service/middleware.py:409-517`, inspect
`LoggingMiddleware.on_call_tool`; its `finally` block ends with an awaited
call to
`_emit_call_metrics` at lines 509-516.
2. In a test, import `LoggingMiddleware` from
`superset/mcp_service/middleware.py` and
construct a middleware instance plus a minimal `MiddlewareContext` and
`call_next` that
returns a successful `ToolResult`.
3. Monkeypatch `stats_logger_manager.instance.incr` or `timing` (called
inside
`_emit_call_metrics` at lines 398-405) to raise `RuntimeError("metrics
backend down")`,
simulating a failing metrics backend or misconfigured client.
4. Await `LoggingMiddleware.on_call_tool(context, call_next)`; `call_next`
returns
successfully, but when the `finally` block executes, the awaited
`_emit_call_metrics` call
raises `RuntimeError`, causing `on_call_tool` to propagate this new
exception instead of
returning the `ToolResult` (or instead of preserving any original exception),
demonstrating that metrics failures can alter or mask the real tool outcome.
```
</details>
[](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=d3fdc50dc261493fb50121ee028a8f48&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=d3fdc50dc261493fb50121ee028a8f48&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/mcp_service/middleware.py
**Line:** 509:516
**Comment:**
*Possible Bug: The metrics emission is awaited directly inside the
`finally` block without protection, so any runtime failure in stats logging
(for example, backend outage or client error) will raise a new exception and
can mask the original tool result/exception. Wrap this call in its own
`try/except` and swallow/log metric failures so observability side effects
never alter MCP tool behavior.
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%2F41921&comment_hash=572fd6e20321135030b6088448966238c2308d3ad5a30ec6f5789fa6aa786d27&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41921&comment_hash=572fd6e20321135030b6088448966238c2308d3ad5a30ec6f5789fa6aa786d27&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]