Skip to content

fix: NameError in wsrelay when JSON decode fails with DEBUG logging#16340

Merged
AlanCoding merged 1 commit intoansible:develfrom
benthomasson:fix/wsrelay-nameerror
Mar 18, 2026
Merged

fix: NameError in wsrelay when JSON decode fails with DEBUG logging#16340
AlanCoding merged 1 commit intoansible:develfrom
benthomasson:fix/wsrelay-nameerror

Conversation

@benthomasson
Copy link
Contributor

@benthomasson benthomasson commented Mar 10, 2026

Summary

  • run_connection() referenced payload in the JSONDecodeError handler, but payload was never assigned because json.loads() is what failed
  • Raises NameError when DEBUG logging is enabled
  • Fix: use msg.data instead of payload to log the raw message content

Files changed

File Change
awx/main/wsrelay.py payloadmsg.data in error handler

Fixes: AAP-68045

Test plan

  • Verify WebSocket relay handles malformed JSON messages without crashing
  • Verify debug logging shows the raw message data on decode failure

Change Type

  • Bug, Docs Fix or other nominal change

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved error logging for JSON parsing failures: logs now include the actual raw payloads involved in the failures (in two affected locations), yielding clearer and more accurate diagnostic information to aid troubleshooting.

@coderabbitai
Copy link

coderabbitai bot commented Mar 10, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 49b0bb65-c0aa-4955-8baa-20713a719ea7

📥 Commits

Reviewing files that changed from the base of the PR and between ae5a246 and aa06cfc.

📒 Files selected for processing (1)
  • awx/main/wsrelay.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • awx/main/wsrelay.py

📝 Walkthrough

Walkthrough

Fixed two debug log statements in awx/main/wsrelay.py to reference the actual raw payloads (msg.data and notif.payload) when JSON decoding fails, avoiding previously incorrect/undefined variable usage.

Changes

Cohort / File(s) Summary
JSON Decode Error Logging Fix
awx/main/wsrelay.py
Adjusted two JSONDecodeError debug logs: in run_connection (TEXT message) to log msg.data, and in on_ws_heartbeat (pg_notify) to log notif.payload instead of the incorrect variable.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: resolving a NameError in wsrelay that occurs when JSON decoding fails with DEBUG logging enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@awx/main/wsrelay.py`:
- Around line 139-143: In on_ws_heartbeat(), the json.JSONDecodeError except
block still references `payload` which may not exist and can raise; change that
block to mirror the fix in the other handler by building the warning message
without using `payload` and, when logger.isEnabledFor(logging.DEBUG), append the
raw `msg.data` (or equivalent raw message variable) to the log; then call
logger.warning(...) and continue/return as the handler intends. Ensure you
update the except block in on_ws_heartbeat() to use `msg.data` instead of
`payload` and preserve the debug-only extra data behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6a8657c3-c489-4b7b-bf50-d88d570c82cc

📥 Commits

Reviewing files that changed from the base of the PR and between 0d08a4d and 2f082f4.

📒 Files selected for processing (1)
  • awx/main/wsrelay.py

@AlanCoding AlanCoding enabled auto-merge (squash) March 18, 2026 13:57
run_connection() referenced payload in the JSONDecodeError handler,
but payload was never assigned because json.loads() is what failed.
Use msg.data instead to log the raw message content.

Fixes: AAP-68045

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Fix other instance of undefined payload
@AlanCoding AlanCoding force-pushed the fix/wsrelay-nameerror branch from ae5a246 to aa06cfc Compare March 18, 2026 17:03
@AlanCoding AlanCoding merged commit bfefee5 into ansible:devel Mar 18, 2026
18 checks passed
@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants