-
Notifications
You must be signed in to change notification settings - Fork 16
fix: catch BaseException in cleanup loops to survive asyncio.CancelledError #11
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -335,7 +335,7 @@ async def collect_contributions(self, channel: str) -> list[Any]: | |||||||||
|
|
||||||||||
| if result is not None: | ||||||||||
| contributions.append(result) | ||||||||||
| except Exception as e: | ||||||||||
| except BaseException as e: | ||||||||||
| logger.warning( | ||||||||||
| f"Contributor '{contributor['name']}' on channel '{channel}' failed: {e}" | ||||||||||
| ) | ||||||||||
|
|
@@ -353,7 +353,7 @@ async def cleanup(self): | |||||||||
| result = cleanup_fn() | ||||||||||
| if inspect.iscoroutine(result): | ||||||||||
| await result | ||||||||||
| except Exception as e: | ||||||||||
| except BaseException as e: | ||||||||||
|
||||||||||
| except BaseException as e: | |
| except BaseException as e: | |
| if isinstance(e, (KeyboardInterrupt, SystemExit)): | |
| raise |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,7 +20,7 @@ | |||||||||||||||
| logger = logging.getLogger(__name__) | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| def _safe_exception_str(e: Exception) -> str: | ||||||||||||||||
| def _safe_exception_str(e: BaseException) -> str: | ||||||||||||||||
| """ | ||||||||||||||||
| CRITICAL: Explicitly handle exception string conversion for Windows cp1252 compatibility. | ||||||||||||||||
| Default encoding can fail on non-cp1252 characters, causing a crash during error handling. | ||||||||||||||||
|
|
@@ -432,7 +432,7 @@ async def execute(self, prompt: str) -> str: | |||||||||||||||
| self.status.status = "completed" | ||||||||||||||||
| return result | ||||||||||||||||
|
|
||||||||||||||||
| except Exception as e: | ||||||||||||||||
| except BaseException as e: | ||||||||||||||||
| # Check if this was a cancellation-related exception | ||||||||||||||||
| if self.coordinator.cancellation.is_cancelled: | ||||||||||||||||
| self.status.status = "cancelled" | ||||||||||||||||
|
|
@@ -455,8 +455,11 @@ async def execute(self, prompt: str) -> str: | |||||||||||||||
|
|
||||||||||||||||
| async def cleanup(self: "AmplifierSession") -> None: | ||||||||||||||||
| """Clean up session resources.""" | ||||||||||||||||
| await self.coordinator.cleanup() | ||||||||||||||||
| # Clean up sys.path modifications | ||||||||||||||||
| try: | ||||||||||||||||
| await self.coordinator.cleanup() | ||||||||||||||||
| except BaseException as e: | ||||||||||||||||
| logger.error(f"Error during coordinator cleanup: {e}") | ||||||||||||||||
| # Clean up sys.path modifications - must always run | ||||||||||||||||
|
Comment on lines
+461
to
+462
|
||||||||||||||||
| logger.error(f"Error during coordinator cleanup: {e}") | |
| # Clean up sys.path modifications - must always run | |
| # Re-raise critical exceptions to allow proper termination | |
| if isinstance(e, (KeyboardInterrupt, SystemExit)): | |
| raise | |
| logger.error(f"Error during coordinator cleanup: {_safe_exception_str(e)}") | |
| # Clean up sys.path modifications - must always run for non-critical errors |
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.
Catching
BaseExceptionwithout re-raising critical exceptions likeKeyboardInterruptandSystemExitcan prevent proper program termination. While this allows the contribution collection to surviveasyncio.CancelledError, it also suppresses other critical exceptions.Consider explicitly re-raising these critical exceptions: