stripe api with `requests` leaks socket file descriptors via unclosed Session
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 55/100
Research direction
Start with stripe/api_requestor.py around lines 103-105 and stripe/http_client.py around lines 287 and 318-319 to trace how the requests Session is created and retained. Reproduce the issue with the provided Python command and verify that the completed change no longer emits the unclosed SSLSocket ResourceWarning.
Written by the indexing model from the issue text.
Description
Describe the bug
the http request client is assigned to a global variable here: https://github.com/stripe/stripe-python/blob/1ae42227d9df745420c1a3db11893589d91ba83e/stripe/api_requestor.py#L103-L105
requests client is defined here: https://github.com/stripe/stripe-python/blob/1ae42227d9df745420c1a3db11893589d91ba83e/stripe/http_client.py#L287
the Session is assigned here: https://github.com/stripe/stripe-python/blob/1ae42227d9df745420c1a3db11893589d91ba83e/stripe/http_client.py#L318-L319
this Session is never closed leading to file descriptor leak and a ResourceWarning -- the correct usage of a Session is to either utilize the with statement or explicitly .close() it
To Reproduce
the simplest reproduction I can come up with is this one liner:
$ python3 -Wonce -c $'import stripe; stripe.api_key="placeholder"; import contextlib\nwith contextlib.suppress(Exception):\n stripe.Account.list()'
sys:1: ResourceWarning: unclosed <ssl.SSLSocket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('10.0.2.15', 44434), raddr=('34.200.27.109', 443)>
the ResourceWarning there is coming from the teardown of that Session object I mentioned above -- it's an unclosed connection to the stripe api:
$ nslookup 34.200.27.109
109.27.200.34.in-addr.arpa name = api-34-200-27-109.stripe.com.
Authoritative answers can be found from:
Expected behavior
utilization of the stripe api should not lead to ResourceWarnings
Code snippets
above
OS
any, though I'm on linux
Language version
any, though I'm using 3.10.4
Library version
4.1.0
API version
N/A
Additional context
No response
- Dominant language
- Python
- Stars
- 2k
- Forks
- 539
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 26
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from stripe/stripe-python
-
feature-request future
Difficulty 4/5 3-5 days Newbie friendliness 35/100
stripe/stripe-python#1886 · 1 comment ·
-
feature-request
Difficulty 2/5 1-3 hours Newbie friendliness 54/100
stripe/stripe-python#1841 · 1 comment ·
-
feature-request future
Difficulty 3/5 1-2 days Newbie friendliness 52/100
stripe/stripe-python#1812 · 1 comment ·
-
feature-request future
Difficulty 3/5 1-2 days Newbie friendliness 74/100
stripe/stripe-python#1796 · 3 comments ·
-
feature-request future
Difficulty 4/5 3-5 days Newbie friendliness 38/100
stripe/stripe-python#1465 · 4 comments · 2 reactions ·
All issues in stripe/stripe-python
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
zostera/django-bootstrap4#894 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
use-agent-os/agent-os#3276 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
NousResearch/hermes-agent#117848 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
zilliztech/memsearch#759 ·