Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 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.
## 2026-02-07 - [Python SDK] Parallelize Session Destruction
**Learning:** Sequential cleanup of network-bound resources (like JSON-RPC sessions) leads to (N)$ shutdown time. Parallelizing with `asyncio.gather` reduces it to (1)$ relative to session count.
Copy link

Copilot AI Feb 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Bolt note uses malformed big-O notation: "(N)$" and "(1)$" look like accidental LaTeX remnants and read incorrectly. Please change these to standard notation (e.g., O(N) and O(1)) so the guidance is unambiguous.

Suggested change
**Learning:** Sequential cleanup of network-bound resources (like JSON-RPC sessions) leads to (N)$ shutdown time. Parallelizing with `asyncio.gather` reduces it to (1)$ relative to session count.
**Learning:** Sequential cleanup of network-bound resources (like JSON-RPC sessions) leads to O(N) shutdown time. Parallelizing with `asyncio.gather` reduces it to O(1) relative to session count.

Copilot uses AI. Check for mistakes.
**Action:** Always check cleanup/stop methods for sequential IO and parallelize where safe. Implement retry logic for cleanup to match robust SDK patterns.
28 changes: 22 additions & 6 deletions python/copilot/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,29 @@ 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: Optional[Exception] = None
# Try up to 3 times with exponential backoff (match Node.js SDK)
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
await asyncio.sleep(0.1 * (2 ** (attempt - 1)))

return StopError(
message=(
f"Failed to destroy session {session.session_id} after 3 attempts: {last_error}"
)
)

# Destroy all active sessions in parallel to ensure shutdown time is
# independent of the number of active sessions.
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 +335 to +338
Copy link

Copilot AI Feb 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CopilotClient.stop() now has new retry + parallel-destruction behavior, but there are no unit/E2E tests covering stop/cleanup semantics (success after retry, aggregated errors after 3 failures, and that it still closes the JSON-RPC client). Please add a test that creates multiple sessions and asserts destroy is invoked for all of them, including retry behavior when session.destroy() fails transiently.

Copilot uses AI. Check for mistakes.

# Close client
if self._client:
Expand Down
Loading