EasyPost/easypost-python

ResourceWarning: Unclosed ssl.SSLSOCKET

Justintime50 opened this issue · 2 comments

This may or may not be an issue as from my research you sometimes want to keep SSL sockets open but I wanted to report this in the off chance we need to address it. When PYTHONWARNINGS=always is used, I get the following warning:

sys:1: ResourceWarning: unclosed <ssl.SSLSocket fd=5, family=AddressFamily.AF_INET6, 
type=SocketKind.SOCK_STREAM, proto=0, laddr=('2601:681:201:15ec:5c9c:82f1:1c10:f6a9', 
51562, 0, 0), raddr=('2607:f0d0:3803:ca::3', 443, 0, 0)>

This is an actual bug. We open a requests.Session object at import time (super-bad! can cause bugs in a pre-fork environment!) and then never clean it up.

In a better architecture, there would be some long-lived parent object (say, EasyPostClient) and usage of this library would revolve around that object. Something like

from easypost import EasyPostClient
from easypost import Shipment

def main():
    client = EasyPostClient(api_key=api_key)
    shipment = Shipment.create(client)

That would give us a reasonably-scoped place to stick the requests session (instead of in sys.modules). It would also be a massive breaking change to the client library. :-/

What about creating a new transient session per action instead of using a singleton? You'll still get the retry adapter, and it's not like the init overhead is devastating.