-
Notifications
You must be signed in to change notification settings - Fork 1
fix: log error as warning during WebSocket unsubscribe and close operations #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
WalkthroughEnhances error handling and logging in WebSocket resource cleanup within the finally block of metric collection. Adds explicit handling for asyncio.CancelledError during unsubscribe and websocket.close operations, replaces single broad error log with granular non-critical logging for each operation. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
common/metric_types.py (2)
94-99: Clarify the log message for CancelledError handling.The message "Unsubscribe completed despite cancellation" is misleading. When
asyncio.shield()raisesCancelledErrorhere, it indicates that the outer task was cancelled but the shielded unsubscribe operation completed successfully. The current wording suggests the operation was cancelled but somehow finished anyway, which isn't accurate.Consider revising the message to be more precise:
try: await asyncio.shield(self.unsubscribe(websocket)) except asyncio.CancelledError: - logging.info("Unsubscribe completed despite cancellation") + logging.debug("Unsubscribe completed (outer task was cancelled)") except Exception as e: logging.warning(f"Error during unsubscribe (non-critical): {e!s}")Also consider using
logging.debug()instead ofinfo()since this is expected behavior during task cancellation rather than something that needs to be highlighted at the info level.
101-106: Clarify the log message for CancelledError handling.The same issue with the log message applies here as with the unsubscribe operation (lines 96-97). The message "WebSocket close completed despite cancellation" is misleading when
asyncio.shield()raisesCancelledErrorto indicate the outer task was cancelled.Apply a similar revision for consistency:
try: await asyncio.shield(websocket.close()) except asyncio.CancelledError: - logging.info("WebSocket close completed despite cancellation") + logging.debug("WebSocket close completed (outer task was cancelled)") except Exception as e: logging.warning(f"Error closing websocket (non-critical): {e!s}")
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
common/metric_types.py(1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.py
📄 CodeRabbit inference engine (AGENTS.md)
**/*.py: Follow PEP 8 style guidelines
Use type hints for all function parameters and return values
Include Google-style docstrings for all functions and classes
Keep cyclomatic complexity below 10 (enforced by Ruff)
Files:
common/metric_types.py
common/**/*.py
📄 CodeRabbit inference engine (AGENTS.md)
/common directory should contain shared utilities and base classes
Files:
common/metric_types.py
🪛 Ruff (0.14.2)
common/metric_types.py
98-98: Do not catch blind exception: Exception
(BLE001)
105-105: Do not catch blind exception: Exception
(BLE001)
🔇 Additional comments (2)
common/metric_types.py (2)
98-98: The broad exception catches are appropriate here (Ruff false positives).Ruff flags these lines for catching blind
Exception, but in cleanup code within afinallyblock, this is actually the correct pattern. These catches ensure that:
- Errors during cleanup are logged but don't prevent the remaining cleanup operations from executing
- The "non-critical" labeling accurately reflects that these are best-effort cleanup operations
- Unexpected exceptions from the websockets library don't cause the cleanup to fail entirely
The current implementation is pragmatic and follows established patterns for resource cleanup in async contexts.
Also applies to: 105-105
94-106: Good enhancement to cleanup error handling.The changes improve the robustness and debuggability of WebSocket resource cleanup by:
- Adding explicit
asyncio.CancelledErrorhandling to acknowledge task cancellation scenarios- Separating error handling for unsubscribe and close operations for better granularity
- Using
asyncio.shield()to ensure cleanup completes even when the outer task is cancelled- Logging errors as warnings with "non-critical" labels to reflect their nature
This approach correctly handles the edge cases where task cancellation occurs during cleanup while ensuring resources are properly released.
Summary by CodeRabbit
Bug Fixes
Chores