⚡ Bolt: Parallelize session destruction in Python SDK#2
⚡ Bolt: Parallelize session destruction in Python SDK#2
Conversation
Optimizes the `CopilotClient.stop()` method in the Python SDK by switching from sequential session destruction to parallel execution using `asyncio.gather`. Also adds a robust 3-attempt retry mechanism with exponential backoff to match the Node.js SDK's behavior. 💡 What: Parallelized session destruction in Python SDK stop() method. 🎯 Why: Sequential destruction leads to shutdown time proportional to session count. 📊 Impact: Reduces shutdown time for multiple sessions by ~90% (e.g., 10 sessions). 🔬 Measurement: Verified with benchmark_stop.py script (simulated latency). Co-authored-by: AkCodes23 <135016848+AkCodes23@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Pull request overview
Improves Python SDK shutdown performance by destroying active Copilot sessions concurrently (instead of sequentially) when CopilotClient.stop() is called, adding retry + backoff for robustness during teardown.
Changes:
- Parallelized session destruction in
CopilotClient.stop()usingasyncio.gather. - Added
destroy_with_retry()helper to retry session destruction up to 3 times with exponential backoff. - Updated internal Bolt notes documenting the optimization.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
python/copilot/client.py |
Parallelizes session cleanup during client shutdown and adds retry/backoff around session.destroy(). |
.jules/bolt.md |
Documents the learning/action item for parallel session destruction in the Python SDK. |
| # 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]) |
There was a problem hiding this comment.
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.
| **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. |
There was a problem hiding this comment.
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.
| **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. |
Optimizes the `CopilotClient.stop()` method in the Python SDK by switching from sequential session destruction to parallel execution using `asyncio.gather`. Also adds a robust 3-attempt retry mechanism with exponential backoff to match the Node.js SDK's behavior. Fixed formatting issues found in CI. 💡 What: Parallelized session destruction in Python SDK stop() method. 🎯 Why: Sequential destruction leads to shutdown time proportional to session count. 📊 Impact: Reduces shutdown time for multiple sessions by ~90% (e.g., 10 sessions). 🔬 Measurement: Verified with benchmark_stop.py script (simulated latency). Co-authored-by: AkCodes23 <135016848+AkCodes23@users.noreply.github.com>
Optimizes the `CopilotClient.stop()` method in the Python SDK by switching from sequential session destruction to parallel execution using `asyncio.gather`. Also adds a robust 3-attempt retry mechanism with exponential backoff to match the Node.js SDK's behavior. Fixed formatting issues found in CI. 💡 What: Parallelized session destruction in Python SDK stop() method. 🎯 Why: Sequential destruction leads to shutdown time proportional to session count. 📊 Impact: Reduces shutdown time for multiple sessions by ~90% (e.g., 10 sessions). 🔬 Measurement: Verified with benchmark_stop.py script (simulated latency). Co-authored-by: AkCodes23 <135016848+AkCodes23@users.noreply.github.com>
Optimizes the `CopilotClient.stop()` method in the Python SDK by switching from sequential session destruction to parallel execution using `asyncio.gather`. Also adds a robust 3-attempt retry mechanism with exponential backoff to match the Node.js SDK's behavior. Fixed formatting issues found in CI. 💡 What: Parallelized session destruction in Python SDK stop() method. 🎯 Why: Sequential destruction leads to shutdown time proportional to session count. 📊 Impact: Reduces shutdown time for multiple sessions by ~90% (e.g., 10 sessions). 🔬 Measurement: Verified with benchmark_stop.py script (simulated latency). Co-authored-by: AkCodes23 <135016848+AkCodes23@users.noreply.github.com>
⚡ Bolt Optimization: Parallel Session Destruction
This PR implements a performance improvement in the Python SDK by parallelizing the cleanup of active sessions when the client is stopped.
💡 What
CopilotClient.stop()inpython/copilot/client.pyto useasyncio.gatherfor session destruction.destroy_with_retryhelper function with up to 3 attempts and exponential backoff (100ms, 200ms).🎯 Why
Previously, the Python SDK destroyed sessions one by one. Since each
session.destroy()call involves a JSON-RPC request over stdio/TCP, the total shutdown time was the sum of all session destruction latencies. For users with multiple active sessions, this caused a noticeable delay during application shutdown.📊 Impact
🔬 Measurement
The optimization was verified using a benchmark script
benchmark_stop.pywhich mocked theJsonRpcClientto simulate latency. The results consistently showed a significant reduction in total time for multi-session scenarios.Existing unit and E2E tests in the Python SDK were run and passed successfully.
PR created automatically by Jules for task 604084263500025276 started by @AkCodes23