Fix LiteClient connection closure and error handling#55
Fix LiteClient connection closure and error handling#55yungwine merged 2 commits intoyungwine:masterfrom
Conversation
|
🔥 |
|
Attached is a minimal reproducible example: https://gist.github.com/nessshon/a686212dab0e23a09e5bb0f174da36bb This PR fixes several errors found in versions
|
There was a problem hiding this comment.
Pull Request Overview
This PR improves the stability and error handling of the LiteClient class during connection loss and shutdown scenarios. The changes focus on preventing crashes, eliminating hanging tasks, and ensuring proper cleanup of failed peers in the LiteBalancer.
Key changes:
- Enhanced error handling in
receive()andlisten()methods to handle various network failure scenarios - Improved
close()method with safer task cancellation and self-await protection - Better exception suppression using
contextlib.suppressfor cleaner error handling
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if not request.done(): | ||
| request.set_result(result) | ||
| except asyncio.CancelledError: | ||
| pass # normal shutdown path |
There was a problem hiding this comment.
Missing space after '#' in comment. Should be '# normal shutdown path' for consistency with Python style guidelines.
| pass # normal shutdown path | |
| pass # normal shutdown path |
| except asyncio.CancelledError: | ||
| pass # normal shutdown path | ||
| except (ConnectionResetError, asyncio.IncompleteReadError, ConnectionAbortedError, TimeoutError): | ||
| return # expected network tear-downs |
There was a problem hiding this comment.
Missing space after '#' in comment. Should be '# expected network tear-downs' for consistency with Python style guidelines.
| return # expected network tear-downs | |
| return # expected network tear-downs |
| while not self.tasks: | ||
| await asyncio.sleep(self.delta) | ||
| try: | ||
| while True: |
There was a problem hiding this comment.
I think this infinite loop can be good replaced with asyncio.Event, then in close method set event.
Improves
LiteClientstability during connection loss:• Safer shutdown and task cancellation.
• Prevents crashes and hanging tasks.
• Helps reduce have no alive peers issues by correctly removing failed peers from
LiteBalanceron disconnect.