customerio-python
customerio-python copied to clipboard
ResourceWarning unclosed ssl.SSLSocket
I'm getting a Python ResourceWarning when following the examples from the Customer.io Python docs:
ResourceWarning: unclosed <ssl.SSLSocket fd=7, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('172.21.X.X', 57030), raddr=('35.227.X.X', 443)>
ResourceWarning: Enable tracemalloc to get the object allocation traceback
I believe the issue is that customerio.APIClient never closes the Requests Session object (see https://stackoverflow.com/questions/48160728/resourcewarning-unclosed-socket-in-python-3-unit-test). I couldn't find a documented method on APIClient to close the session either.
As a workaround, we can reach into the APIClient object and call .http.close() directly, but that seems brittle:
client = customerio.APIClient(CUSTOMERIO_APP_API_KEY)
request = customerio.SendEmailRequest(...)
client.send_email(request)
client.http.close() # <-- manually close the session
Here's an idea of how this could work in the API client. It'd be nice if APIClient supported the Context Manager protocol, so that we could use it like this:
with customerio.APIClient(...) as client:
client.send_email(...)
This relates to #27 and #31.