-
Notifications
You must be signed in to change notification settings - Fork 18.5k
fix: add exception handling to Redis subscription listener for resource cleanup #28542
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
base: main
Are you sure you want to change the base?
Conversation
…ce cleanup Signed-off-by: NeatGuyCoding <[email protected]>
Summary of ChangesHello @NeatGuyCoding, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the stability of the Redis broadcast channel subscription mechanism by introducing comprehensive exception handling. It guarantees that Redis PubSub resources are properly unsubscribed and closed, even if unforeseen errors occur during message reception or processing, thereby preventing potential resource leaks and improving the overall resilience of the system. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request introduces crucial exception handling to the Redis subscription listener, ensuring robust resource cleanup even when errors occur. The use of a try...finally block around the listener loop and individual try...except blocks for cleanup operations is a solid approach to improve reliability. I have one suggestion to simplify the cleanup logic slightly for better readability.
| try: | ||
| pubsub.close() | ||
| _logger.debug("%s PubSub closed for topic %s", self._get_subscription_type().title(), self._topic) | ||
| except Exception as e: | ||
| _logger.warning( | ||
| "Error closing PubSub for %s channel %s: %s", | ||
| self._get_subscription_type(), | ||
| self._topic, | ||
| type(payload_bytes), | ||
| e, | ||
| ) | ||
| continue | ||
|
|
||
| self._enqueue_message(payload_bytes) | ||
|
|
||
| _logger.debug("%s listener thread stopped for channel %s", self._get_subscription_type().title(), self._topic) | ||
| self._unsubscribe() | ||
| pubsub.close() | ||
| _logger.debug("%s PubSub closed for topic %s", self._get_subscription_type().title(), self._topic) | ||
| self._pubsub = None | ||
| finally: | ||
| self._pubsub = None |
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.
The nested finally block is not necessary here. Execution will proceed to the line after the try...except block regardless of whether an exception was caught. You can simplify the code and improve readability by removing the finally statement and un-indenting self._pubsub = None.
try:
pubsub.close()
_logger.debug("%s PubSub closed for topic %s", self._get_subscription_type().title(), self._topic)
except Exception as e:
_logger.warning(
"Error closing PubSub for %s channel %s: %s",
self._get_subscription_type(),
self._topic,
e,
)
self._pubsub = NoneThere 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.
Pull request overview
This PR adds comprehensive exception handling and resource cleanup to the Redis subscription listener to prevent resource leaks when errors occur during message processing or cleanup operations.
Key changes:
- Wraps the main message processing loop in a try-except block to catch and log unexpected errors
- Moves resource cleanup operations (unsubscribe and pubsub close) into a finally block to ensure they always execute
- Wraps individual cleanup operations in separate try-except blocks to prevent one failing cleanup from blocking others
- Uses a nested finally block to guarantee
_pubsubis always set toNone, even if cleanup operations fail
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| "Received invalid data from %s channel %s, type=%s", | ||
| try: | ||
| pubsub.close() | ||
| _logger.debug("%s PubSub closed for topic %s", self._get_subscription_type().title(), self._topic) |
Copilot
AI
Nov 22, 2025
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.
Inconsistent terminology in log message. This message uses "for topic %s" while all other log messages in this file use "channel" (e.g., lines 108, 115, 125: "for channel %s").
Consider changing to:
_logger.debug("%s PubSub closed for channel %s", self._get_subscription_type().title(), self._topic)This would make the terminology consistent throughout the file.
| _logger.debug("%s PubSub closed for topic %s", self._get_subscription_type().title(), self._topic) | |
| _logger.debug("%s PubSub closed for channel %s", self._get_subscription_type().title(), self._topic) |
Important
Fixes #<issue number>.Summary
fix: add exception handling to Redis subscription listener for resource cleanup
Screenshots
Checklist
dev/reformat(backend) andcd web && npx lint-staged(frontend) to appease the lint gods