Skip to content
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

ResourceWarning: unclosed <ssl.SSLSocket fd=20, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('10.1.0.62', 52098), raddr=('52.239.140.10', 443)> #14565

Closed
lukin0110 opened this issue Oct 16, 2020 · 9 comments
Assignees
Labels
customer-reported Issues that are reported by GitHub users external to the Azure organization. Storage Storage Service (Queues, Blobs, Files)

Comments

@lukin0110
Copy link

lukin0110 commented Oct 16, 2020

Describe the bug
We've enabled a few warning filters in our application with warning.simplefilter and we're seeing a python warning when we download a file from the blob storage. ResourceWarning: unclosed <ssl.SSLSocket fd=20, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('10.1.0.62', 52098), raddr=('52.239.140.10', 443)>

To Reproduce

client = BlobServiceClient.from_connection_string("<connection string here>").get_blob_client(container="<container name>", blob="<blob name>")
raw_data = client.download_blob().readall()
with gzip.open(BytesIO(raw_data), "rb") as fh:
    data = pickle.load(fh)  # noqa: S301

Expected behavior
No warning or a proper way to close the stream.

Screenshots
NA

Additional context
NA

@ghost ghost added needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Oct 16, 2020
@xiangyan99 xiangyan99 added the Storage Storage Service (Queues, Blobs, Files) label Oct 16, 2020
@ghost ghost removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Oct 16, 2020
@tasherif-msft tasherif-msft self-assigned this Oct 16, 2020
@xiafu-msft
Copy link
Contributor

Hi @lukin0110

So you also saw this warning when only executing this two lines?

client = BlobServiceClient.from_connection_string("<connection string here>").get_blob_client(container="<container name>", blob="<blob name>")
raw_data = client.download_blob().readall()

@lukin0110
Copy link
Author

Hi @xiafu-msft

The correct way to reproduce is:

import warnings
from azure.storage.blob import BlobServiceClient

warnings.simplefilter("default", ResourceWarning)

CONNECTION = "<connection string here>"
CONTAINER = "<container name here>"
BLOB_NAME = "<blob name here>"

client = BlobServiceClient.from_connection_string(CONNECTION).get_blob_client(container=CONTAINER, blob=BLOB_NAME)
raw_data = client.download_blob().readall()

@xiafu-msft
Copy link
Contributor

Hi @lukin0110

Thanks for your code!
can you try this and see if you still get this warning:

with BlobServiceClient.from_connection_string(CONNECTION).get_blob_client(container=CONTAINER, blob=BLOB_NAME) as client:
    raw_data = client.download_blob().readall()

@lukin0110
Copy link
Author

Heh @xiafu-msft,

Using it as a context is still showing the warning.

@xiafu-msft
Copy link
Contributor

Hi @lukin0110

Thanks for reporting this issue. I was able to reproduce.
The problem is we created a session and didn't close it https://github.com/Azure/azure-sdk-for-python/blame/master/sdk/core/azure-core/azure/core/pipeline/transport/_requests_basic.py#L244

@xiangyan99 could you please find people to confirm we want to close the session before return.

@lukin0110
Copy link
Author

Thanks for the quick follow up!

@xiafu-msft xiafu-msft added bug This issue requires a change to an existing behavior in the product in order to be resolved. and removed question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Storage Storage Service (Queues, Blobs, Files) labels Oct 23, 2020
@xiafu-msft xiafu-msft added Storage Storage Service (Queues, Blobs, Files) and removed Azure.Core labels Oct 23, 2020
@tasherif-msft tasherif-msft self-assigned this Dec 31, 2020
@tasherif-msft
Copy link
Contributor

tasherif-msft commented Jan 29, 2021

Hi @lukin0110, this is actually intended behavior. When you run

import warnings
from azure.storage.blob import BlobServiceClient

warnings.simplefilter("default", ResourceWarning)

CONNECTION = "<connection string here>"
CONTAINER = "<container name here>"
BLOB_NAME = "<blob name here>"

client = BlobServiceClient.from_connection_string(CONNECTION).get_blob_client(container=CONTAINER, blob=BLOB_NAME)
raw_data = client.download_blob().readall()

You're creating a blob client from your BlobServiceClient and not closing any sessions.
If you want to ensure all sessions are closed, you must close your parent client.
You can achieve this by doing:

BlobServiceClient.from_connection_string(CONNECTION) as bsc:
    bc = bsc.get_blob_client(container="test", blob="blob.txt")
    raw_data = bc.download_blob().readall()

This ensures that the socket is closed since the child class is inheriting the same session.

You can also do:

bsc = BlobServiceClient.from_connection_string(CONNECTION)
bc = bsc.get_blob_client(container="test", blob="blob.txt")
raw_data = bc.download_blob().readall()
bsc.close()

Hope this helps!
Let me know if there anything else needed

@xiafu-msft xiafu-msft removed the bug This issue requires a change to an existing behavior in the product in order to be resolved. label Jan 29, 2021
@lukin0110
Copy link
Author

@tadam-msft thanks for clarifying. I've applied your suggestion. The resource warning is not appearing anymore.

@tasherif-msft
Copy link
Contributor

Hi @lukin0110! fantastic :)

openapi-sdkautomation bot pushed a commit to AzureSDKAutomation/azure-sdk-for-python that referenced this issue May 28, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
customer-reported Issues that are reported by GitHub users external to the Azure organization. Storage Storage Service (Queues, Blobs, Files)
Projects
None yet
4 participants