Skip to content

Fix unclosed Session file descriptor leak in RequestsClient (fixes #874)#1798

Open
Jah-yee wants to merge 1 commit intostripe:masterfrom
Jah-yee:master
Open

Fix unclosed Session file descriptor leak in RequestsClient (fixes #874)#1798
Jah-yee wants to merge 1 commit intostripe:masterfrom
Jah-yee:master

Conversation

@Jah-yee
Copy link
Copy Markdown

@Jah-yee Jah-yee commented Apr 26, 2026

Summary

Fixes file descriptor / socket leak in stripe-python when using the requests client.

Problem

The RequestsClient stores a requests.Session in a thread-local variable (self._thread_local.session). This Session is created on first use but never closed, causing:

  • ResourceWarning: unclosed <ssl.SSLSocket> on Python exit
  • File descriptor leaks for long-running processes

Solution

Register atexit.register(self.close) in RequestsClient.__init__() to ensure the thread-local Session is properly closed when Python exits.

Changes

  • stripe/_http_client.py:
    • Added import atexit
    • Added atexit.register(self.close) in RequestsClient.__init__

Testing

import stripe
stripe.api_key = "placeholder"
with contextlib.suppress(Exception):
    stripe.Account.list()
# Before fix: ResourceWarning about unclosed SSL socket
# After fix: No warning

References

…ripe#874)

Register atexit handler to close thread-local Session when Python
exits. The Session created in _thread_local is never closed, causing
ResourceWarning and socket file descriptor leaks.

Fix: add atexit.register(self.close) in RequestsClient.__init__.

Ref: stripe#874
@Jah-yee Jah-yee requested a review from a team as a code owner April 26, 2026 12:23
@Jah-yee Jah-yee requested review from jar-stripe and removed request for a team April 26, 2026 12:23
@Jah-yee
Copy link
Copy Markdown
Author

Jah-yee commented Apr 27, 2026

Gentle ping @richardlawrence @ob-stripe @cjavilla-stripe — this PR fixes unclosed Session file descriptor leak in RequestsClient (fixes #874). Looking forward to your review! 🙏

@Jah-yee
Copy link
Copy Markdown
Author

Jah-yee commented Apr 28, 2026

Hi! Just following up on this PR. The fix addresses an unclosed file descriptor leak in RequestsClient (issue #874). CI is green. Happy to make any adjustments if needed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant