forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ Bolt: Parallelize session destruction in Python SDK #5
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
Open
AkCodes23
wants to merge
4
commits into
main
Choose a base branch
from
bolt-parallelize-python-stop-8687457665529838155
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
243a7ea
⚡ Bolt: Parallelize session destruction in Python SDK
google-labs-jules[bot] bfca8e7
⚡ Bolt: Parallelize session destruction in Python SDK (fix lint)
google-labs-jules[bot] 3896332
⚡ Bolt: Parallelize session destruction in Python SDK (retry fix lint)
google-labs-jules[bot] 8bcc011
⚡ Bolt: Parallelize session destruction in Python SDK (fix lint)
google-labs-jules[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| ## 2025-05-15 - [Sequential session destruction in SDKs] | ||
| **Learning:** All Copilot SDKs (Node.js, Python, Go, .NET) were initially implementing session destruction sequentially during client shutdown. This leads to a linear increase in shutdown time as the number of active sessions grows, especially when individual destructions involve retries and backoff. | ||
| **Action:** Parallelize session cleanup using language-specific concurrency primitives (e.g., `Promise.all` in Node.js, `asyncio.gather` in Python, `Task.WhenAll` in .NET, or WaitGroups/Channels in Go) to ensure shutdown time remains constant and minimal. | ||
|
|
||
| ## 2025-05-16 - [Sequential session destruction in Go and .NET SDKs] | ||
| **Learning:** While Node.js and Python SDKs have been optimized for parallel session destruction, Go and .NET SDKs still implement this sequentially. This leads to a linear increase in shutdown time as the number of active sessions grows in those languages. | ||
| **Action:** Parallelize session cleanup in Go using goroutines/WaitGroups and in .NET using Task.WhenAll to ensure consistent O(T) shutdown time across all SDKs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -313,13 +313,33 @@ 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]: | ||||||
| last_error = None | ||||||
| # Try up to 3 times with exponential backoff | ||||||
| for attempt in range(1, 4): | ||||||
| try: | ||||||
| await session.destroy() | ||||||
| return None | ||||||
| except Exception as e: | ||||||
| last_error = e | ||||||
| if attempt < 3: | ||||||
| # Exponential backoff: 100ms, 200ms | ||||||
| delay = 0.1 * (2 ** (attempt - 1)) | ||||||
| await asyncio.sleep(delay) | ||||||
|
|
||||||
| msg = f"Failed to destroy session {session.session_id}" | ||||||
| msg += f" after 3 attempts: {last_error}" | ||||||
| return StopError(message=msg) | ||||||
|
|
||||||
| if sessions_to_destroy: | ||||||
| # Parallelize session destruction to ensure O(T) shutdown time | ||||||
| results = await asyncio.gather( | ||||||
| *[destroy_with_retry(s) for s in sessions_to_destroy], | ||||||
|
||||||
| *[destroy_with_retry(s) for s in sessions_to_destroy], | |
| *(destroy_with_retry(s) for s in sessions_to_destroy), |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 comment claiming this “ensures O(T) shutdown time” is misleading: there’s still O(N) overhead to schedule tasks and send N JSON-RPC requests, and JsonRpcClient serializes writes via _write_lock. Consider rewording to something like “reduces wall-clock shutdown time by running destroys concurrently (bounded by the slowest destroy)”.