perf(client): add session pooling for connection reuse TAV-4105#151
perf(client): add session pooling for connection reuse TAV-4105#151tinosattavily wants to merge 1 commit into
Conversation
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
This PR is being reviewed by Cursor Bugbot
Details
Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.
To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.
| self.session = requests.Session() | ||
| self.session.headers.update(self.headers) | ||
| if self.proxies: | ||
| self.session.proxies.update(self.proxies) |
There was a problem hiding this comment.
Shared session used concurrently is not thread-safe
Medium Severity
The new requests.Session object is shared across threads in get_company_info, which uses ThreadPoolExecutor to make parallel API calls. requests.Session is not documented as thread-safe, and the previous implementation using independent requests.post() calls was explicitly safe for concurrent use. This change introduces potential race conditions when multiple threads access the session simultaneously.
Additional Locations (1)
| self.session = requests.Session() | ||
| self.session.headers.update(self.headers) | ||
| if self.proxies: | ||
| self.session.proxies.update(self.proxies) |
There was a problem hiding this comment.
Session pooling introduced without cleanup mechanism
Low Severity
The requests.Session is created and holds connection pools, but there's no close() method, __del__ destructor, or context manager support (__enter__/__exit__) to release these resources. Before this change, each request created and released its own connection. Now resources accumulate until garbage collection. Users creating multiple TavilyClient instances (e.g., in loops or long-running applications) could hit file descriptor limits or experience memory growth. The async client handles this correctly by creating/closing clients per-request with context managers.
Summary
requests.Session()toTavilyClientfor HTTP connection poolingTest plan
Fixes TAV-4105
Note
Improves HTTP performance by introducing a shared
requests.Sessionintavily/tavily.pyand using it across all client requests for connection reuse.self.sessionwith defaultheadersand optionalproxies; removes per-callheaders/proxiesargumentsrequests.post/getwithself.session.post/getforsearch,extract,crawl,map,research(including streaming), andget_researchNo public API changes; error handling and response parsing remain the same.
Written by Cursor Bugbot for commit 31d0694. This will update automatically on new commits. Configure here.