-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ Bolt: parallelize session destruction in Python SDK #3
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
23c1d99
452297f
c921ff6
c62916f
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 |
|---|---|---|
|
|
@@ -313,13 +313,30 @@ async def stop(self) -> list["StopError"]: | |
| sessions_to_destroy = list(self._sessions.values()) | ||
| self._sessions.clear() | ||
|
|
||
| for session in sessions_to_destroy: | ||
| try: | ||
| await session.destroy() | ||
| except Exception as e: | ||
| errors.append( | ||
| StopError(message=f"Failed to destroy session {session.session_id}: {e}") | ||
| async def destroy_with_retry(session: CopilotSession) -> Optional[StopError]: | ||
| """Destroy a session with up to 3 attempts and exponential backoff.""" | ||
| last_err: Optional[Exception] = None | ||
| for attempt in range(1, 4): | ||
| try: | ||
| await session.destroy() | ||
| return None | ||
| except Exception as e: | ||
| last_err = e | ||
| if attempt < 3: | ||
| # Exponential backoff: 100ms, 200ms | ||
| delay = 0.1 * (2 ** (attempt - 1)) | ||
| await asyncio.sleep(delay) | ||
|
|
||
| return StopError( | ||
| message=( | ||
| f"Failed to destroy session {session.session_id} after 3 attempts: {last_err}" | ||
| ) | ||
| ) | ||
|
|
||
| # Destroy all active sessions in parallel with retry logic | ||
| if sessions_to_destroy: | ||
| results = await asyncio.gather(*(destroy_with_retry(s) for s in sessions_to_destroy)) | ||
| errors.extend([r for r in results if r is not None]) | ||
|
Comment on lines
+316
to
+339
|
||
|
|
||
| # Close client | ||
| if self._client: | ||
|
|
||
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.
destroy_with_retrycatchesException, which on Python 3.9/3.10 includesasyncio.CancelledError. This can swallow task cancellation during shutdown (and even retry after cancellation), preventingstop()from being promptly cancellable. Handleasyncio.CancelledErrorexplicitly by re-raising it before the broadexcept Exception(and avoid retrying on cancellation).