René's URL Explorer Experiment


Title: Clients do not clean up Requests sessions · Issue #64 · googleapis/python-cloud-core · GitHub

Open Graph Title: Clients do not clean up Requests sessions · Issue #64 · googleapis/python-cloud-core

X Title: Clients do not clean up Requests sessions · Issue #64 · googleapis/python-cloud-core

Description: Environment details OS type and version: macOS Python version: 3.9 google-cloud-core version: 1.4.3 google-cloud-storage version: 1.33.0 (since it's related) Steps to reproduce This is a sibling issue to googleapis/google-auth-library-py...

Open Graph Description: Environment details OS type and version: macOS Python version: 3.9 google-cloud-core version: 1.4.3 google-cloud-storage version: 1.33.0 (since it's related) Steps to reproduce This is a sibling is...

X Description: Environment details OS type and version: macOS Python version: 3.9 google-cloud-core version: 1.4.3 google-cloud-storage version: 1.33.0 (since it's related) Steps to reproduce This is a siblin...

Opengraph URL: https://github.com/googleapis/python-cloud-core/issues/64

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Clients do not clean up Requests sessions","articleBody":"#### Environment details\r\n\r\n  - OS type and version: macOS\r\n  - Python version: 3.9\r\n  - `google-cloud-core` version: 1.4.3\r\n  - `google-cloud-storage` version: 1.33.0 (since it's related)\r\n\r\n#### Steps to reproduce\r\n\r\nThis is a sibling issue to https://github.com/googleapis/google-auth-library-python/issues/658 and as such the below is similar to that issue.\r\n\r\n[Py.test 6.2 enables hard errors on unhandled thread exceptions and resource errors](https://docs.pytest.org/en/stable/usage.html#warning-about-unraisable-exceptions-and-unhandled-thread-exceptions) such as leaking sockets/SSL sockets/FDs/.... I've spent the last couple hours debugging various\r\n\r\n```\r\nResourceWarning: unclosed \u003cssl.SSLSocket fd=12, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('192.168.1.23', 60522), raddr=('216.58.207.202', 443)\u003e\r\n```\r\n\r\nWhen initiating (e.g.) a [`google-cloud-storage` storage client](https://github.com/googleapis/python-storage) with e.g.\r\n\r\n```python\r\n        creds = self.get_client_credentials()\r\n        return storage.Client(project=creds.project_id, credentials=creds)\r\n```\r\n\r\nthe client object is initialized with no explicit `_http` object, and the [default code path](https://github.com/googleapis/python-cloud-core/blob/d01c825d4b7638e76d9d2883baba5a91c884e310/google/cloud/client.py#L172-L184) is run, creating a new `requests.Session()` that will be _eventually_ GC'd but is otherwise not [`close()`d](https://requests.readthedocs.io/en/latest/api/#requests.Session.close), leading to the above warning (and a dropped connection and socket, which may have implications down the line).\r\n\r\nSince `google.core.client.Client` doesn't implement e.g. the context manager protocol or `close()`, there's no officially sanctioned way to clean up that session; for a storage Client, I subclassed one as a workaround but it would be nice if Clients had an official `close()`.\r\n\r\n```python\r\n        class CloseableStorageClient(storage.Client):\r\n            def __enter__(self):\r\n                return self\r\n\r\n            def __exit__(self, *args):\r\n                self._http.close()\r\n```\r\n\r\n#### Resolution suggestion\r\n\r\nAt the very least, add `close()` to `Client`s; by default it should probably only be something like\r\n  ```python\r\n  def close(self):\r\n      if self._http_internal:\r\n          self._http_internal.close()\r\n  ```\r\n, the semantics being a best-effort cleanup of resources allocated by the client, but not irreversible finalization.\r\n\r\nWith this, `Client`s could be used with [`contextlib.closing()`](https://docs.python.org/3.9/library/contextlib.html#contextlib.closing). Additionally, one could add a minimal context management protocol implementation á la\r\n\r\n```python\r\n            def __enter__(self):\r\n                return self\r\n\r\n            def __exit__(self, *args):\r\n                self.close()\r\n```\r\n\r\nIf you folks think this is a good way to go, I can certainly write the PR :)\r\n\r\ncc @busunkim96 (for having triaged the other issue).","author":{"url":"https://github.com/akx","@type":"Person","name":"akx"},"datePublished":"2020-12-22T19:02:03.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/64/python-cloud-core/issues/64"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:fe362e33-eb21-2b56-f147-cacf976597b7
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idD820:23E3E9:9C0C433:9F22EDA:6A507E9A
html-safe-nonce91965b44d47eb0f4fe47e1988c4725d75f0b79dfa02fdb051916406c7ab6105d
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEODIwOjIzRTNFOTo5QzBDNDMzOjlGMjJFREE6NkE1MDdFOUEiLCJ2aXNpdG9yX2lkIjoiMjUxNzgyNTUwNzA1MzA0MzM1NCIsInJlZ2lvbl9lZGdlIjoic2VhIiwicmVnaW9uX3JlbmRlciI6InNlYSJ9
visitor-hmac61db3549c8527c6c5417837468aa4db5f2f199718cf103bbb0f487611de5e71c
hovercard-subject-tagissue:773175159
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/googleapis/python-cloud-core/64/issue_layout
twitter:imagehttps://opengraph.githubassets.com/098671364c284cb9fdb35886a71ebd455156cbc8031da8eb5d21bb7223895902/googleapis/python-cloud-core/issues/64
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/098671364c284cb9fdb35886a71ebd455156cbc8031da8eb5d21bb7223895902/googleapis/python-cloud-core/issues/64
og:image:altEnvironment details OS type and version: macOS Python version: 3.9 google-cloud-core version: 1.4.3 google-cloud-storage version: 1.33.0 (since it's related) Steps to reproduce This is a sibling is...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameakx
hostnamegithub.com
expected-hostnamegithub.com
Noned6dc8294eb500fa36bbc57472d61fe87c522f9c3c1d64f70f4926f66a66a7efb
turbo-cache-controlno-preview
go-importgithub.com/googleapis/python-cloud-core git https://github.com/googleapis/python-cloud-core.git
octolytics-dimension-user_id16785467
octolytics-dimension-user_logingoogleapis
octolytics-dimension-repository_id226992462
octolytics-dimension-repository_nwogoogleapis/python-cloud-core
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id226992462
octolytics-dimension-repository_network_root_nwogoogleapis/python-cloud-core
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release7ac0ad2f2c7e4b9056617355fd9e33e22b0c8df9
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/googleapis/python-cloud-core/issues/64#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgoogleapis%2Fpython-cloud-core%2Fissues%2F64
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fgoogleapis%2Fpython-cloud-core%2Fissues%2F64
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=googleapis%2Fpython-cloud-core
Reloadhttps://github.com/googleapis/python-cloud-core/issues/64
Reloadhttps://github.com/googleapis/python-cloud-core/issues/64
Reloadhttps://github.com/googleapis/python-cloud-core/issues/64
Please reload this pagehttps://github.com/googleapis/python-cloud-core/issues/64
googleapis https://github.com/googleapis
python-cloud-corehttps://github.com/googleapis/python-cloud-core
Notifications https://github.com/login?return_to=%2Fgoogleapis%2Fpython-cloud-core
Fork 19 https://github.com/login?return_to=%2Fgoogleapis%2Fpython-cloud-core
Star 34 https://github.com/login?return_to=%2Fgoogleapis%2Fpython-cloud-core
Code https://github.com/googleapis/python-cloud-core
Issues 0 https://github.com/googleapis/python-cloud-core/issues
Pull requests 0 https://github.com/googleapis/python-cloud-core/pulls
Actions https://github.com/googleapis/python-cloud-core/actions
Projects https://github.com/googleapis/python-cloud-core/projects
Security and quality 0 https://github.com/googleapis/python-cloud-core/security
Insights https://github.com/googleapis/python-cloud-core/pulse
Code https://github.com/googleapis/python-cloud-core
Issues https://github.com/googleapis/python-cloud-core/issues
Pull requests https://github.com/googleapis/python-cloud-core/pulls
Actions https://github.com/googleapis/python-cloud-core/actions
Projects https://github.com/googleapis/python-cloud-core/projects
Security and quality https://github.com/googleapis/python-cloud-core/security
Insights https://github.com/googleapis/python-cloud-core/pulse
#100https://github.com/googleapis/python-cloud-core/pull/100
Clients do not clean up Requests sessionshttps://github.com/googleapis/python-cloud-core/issues/64#top
#100https://github.com/googleapis/python-cloud-core/pull/100
priority: p2Moderately-important priority. Fix may not be included in next release.https://github.com/googleapis/python-cloud-core/issues?q=state%3Aopen%20label%3A%22priority%3A%20p2%22
type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.https://github.com/googleapis/python-cloud-core/issues?q=state%3Aopen%20label%3A%22type%3A%20bug%22
https://github.com/akx
akxhttps://github.com/akx
on Dec 22, 2020https://github.com/googleapis/python-cloud-core/issues/64#issue-773175159
googleapis/google-auth-library-python#658https://github.com/googleapis/google-auth-library-python/issues/658
Py.test 6.2 enables hard errors on unhandled thread exceptions and resource errorshttps://docs.pytest.org/en/stable/usage.html#warning-about-unraisable-exceptions-and-unhandled-thread-exceptions
google-cloud-storage storage clienthttps://github.com/googleapis/python-storage
default code pathhttps://github.com/googleapis/python-cloud-core/blob/d01c825d4b7638e76d9d2883baba5a91c884e310/google/cloud/client.py#L172-L184
close()dhttps://requests.readthedocs.io/en/latest/api/#requests.Session.close
contextlib.closing()https://docs.python.org/3.9/library/contextlib.html#contextlib.closing
@busunkim96https://github.com/busunkim96
priority: p2Moderately-important priority. Fix may not be included in next release.https://github.com/googleapis/python-cloud-core/issues?q=state%3Aopen%20label%3A%22priority%3A%20p2%22
type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.https://github.com/googleapis/python-cloud-core/issues?q=state%3Aopen%20label%3A%22type%3A%20bug%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.