13

While doing some automation around AWS-EC2 with the botocore library in Python, I noticed a lot of HTTPS connections remained established that were no longer needed by processes that were busy doing other things (so killing them or recoding them to exit is not an option). I think the botocore session and/or client object is leaving the connections to AWS endpoints established. The botocore documentation shows how to start or create them, but not how to close them or clean things up. I tried a .close method but it did not exist. How can I get these connections to gracefully close without killing the processes?

1 Answer 1

8

I had the same issue, but from a slightly different angle: When closing worker threads, my log files would get cluttered with these warnings - also due to open connections:

Exception ignored in: <ssl.SSLSocket fd=4, family=AddressFamily.AF_INET, type=2049, proto=6, laddr=('[internal ip]', 49266), raddr=('[external ip]', 443)>

ResourceWarning: unclosed <ssl.SSLSocket fd=4, family=AddressFamily.AF_INET, type=2049, proto=6, laddr=('[internal ip]', 49266), raddr=('[external ip]', 443)>

After some time down the rabbit hole, I have figured out how to close the connections correctly, before closing the threads.

I'm using SQS with boto3, so you might need to modify the call a bit for it to work with botocore.

My example to produces the warning above is:

import boto3
import boto3.session
import warnings

warnings.simplefilter('error', ResourceWarning)  # Display warnings
session = boto3.session.Session()
sqs = session.resource('sqs', region_name=AWSregion)
sqs_q = sqs.Queue(url=SQSQueueUrl)
sqs_msg = sqs_q.receive_messages(MaxNumberOfMessages=1)

The SQS connection can be closed using:

sqs.meta.client._endpoint.http_session.close()  # closing a boto3 resource
sqs._endpoint.http_session.close()  # closing a boto3 client
3
  • 1
    Is this still the recommended way to close the socket? _endpoint is a private attribute Jan 25, 2019 at 18:12
  • @AaronLelevier There is apparently no real solution, however the warning is a false positive. See: github.com/boto/boto3/issues/454
    – RVH
    May 29, 2020 at 15:35
  • This doesn't work for me with boto3 == 1.20.52 for s3 http_session is a URLib3 resource which has no close method
    – Kevin R.
    Feb 10, 2022 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.