Skip to content

fix: prevent UnboundLocalError in A2A telemetry and escape SSE error JSON - #181

Open
WangXukang-cypher wants to merge 1 commit into
volcengine:mainfrom
WangXukang-cypher:fix/a2a-unbound-var-and-sse-json-escape
Open

fix: prevent UnboundLocalError in A2A telemetry and escape SSE error JSON#181
WangXukang-cypher wants to merge 1 commit into
volcengine:mainfrom
WangXukang-cypher:fix/a2a-unbound-var-and-sse-json-escape

Conversation

@WangXukang-cypher

Copy link
Copy Markdown

Summary

  • a2a_app.py: _wrap_agent_executor_execute_func references result in the
    finally block, but result is only assigned inside try. When execute_func
    raises, the finally clause hits UnboundLocalError, which suppresses the
    original exception and makes debugging impossible. Fix: initialize result = None
    before the try block.

  • agent_server_app.py: The _invoke_compat SSE error path builds JSON via
    f-string (f'data: {{"error": "{str(e)}"}}'). If the exception message contains
    ", \, or newlines, the resulting JSON is malformed and breaks client-side
    parsing. The /run_sse endpoint at line 475 already uses json.dumps() correctly;
    this patch aligns _invoke_compat to the same pattern.

Test plan

  • pytest tests/apps/test_a2a_app.py — 20 passed
  • pytest tests/apps/test_agent_server_invoke.py — 19 passed
  • ruff check — no new lint errors (count decreased from 13 → 12)
  • ruff format --check — both files formatted

…JSON

1. a2a_app.py: initialize `result = None` before the try block so the
   finally clause can always reference it when execute_func raises.
   Previously an UnboundLocalError would suppress the original exception.

2. agent_server_app.py: use json.dumps() instead of an f-string to build
   the SSE error payload, matching the /run_sse path. Exception messages
   containing quotes or backslashes previously produced malformed JSON.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant