Skip to content

Avoid unclosed SSLSocket warnings #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from posthog.consumer import Consumer
from posthog.poller import Poller
from posthog.request import APIError, batch_post, decide, get
from posthog.request import APIError, batch_post, decide, get, shutdown as request_close
from posthog.utils import clean, guess_timezone
from posthog.version import VERSION

Expand Down Expand Up @@ -309,6 +309,9 @@ def join(self):
if self.poller:
self.poller.stop()

# Close request sessions before leaving
request_close()

def shutdown(self):
"""Flush all messages and cleanly shutdown the client"""
self.flush()
Expand Down
7 changes: 7 additions & 0 deletions posthog/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ def get(api_key: str, url: str, host: Optional[str] = None, timeout: Optional[in
res = requests.get(url, headers={"Authorization": "Bearer %s" % api_key, "User-Agent": USER_AGENT}, timeout=timeout)
return _process_response(res, success_message=f"GET {url} completed successfully")

def shutdown():
# Avoid logs with
# sys:1: ResourceWarning: unclosed
# <ssl.SSLSocket fd=10, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0,
# laddr=('x.x.x.x', y), raddr=('x.x.x.x', 443)>
# Should only be called when once, renders `_session` unusable
_session.close()

class APIError(Exception):
def __init__(self, status: Union[int, str], message: str):
Expand Down