Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

botocore: how to close or clean up a session or client

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?

like image 224
Skaperen Avatar asked Nov 01 '15 11:11

Skaperen


People also ask

Should I close Boto3 client?

There is little to gain by manually closing the boto connections because they are just HTTP connections and will close automatically after a few minutes of idle time. I wouldn't worry about trying to close them.

How do I close Boto3 session in Python?

How to Close Boto3 Session. The idea of closing a session in AWS does not exist. The way sessions work are by leveraging the API and Secret Key.

What is Botocore client?

Botocore is a low-level interface to a growing number of Amazon Web Services. Botocore serves as the foundation for the AWS-CLI command line utilities. It will also play an important role in the boto3. x project. The botocore package is compatible with Python versions Python 3.7 and higher.

Should I use Boto3 resource or client?

Clients vs ResourcesResources are the recommended pattern to use boto3 as you don't have to worry about a lot of the underlying details when interacting with AWS services. As a result, code written with Resources tends to be simpler.


1 Answers

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
like image 80
Jervelund Avatar answered Sep 20 '22 18:09

Jervelund