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

BUG: Could not close neither session no connection after retrieving data from GCS #854

Open
jcierocki opened this issue Jan 24, 2025 · 0 comments

Comments

@jcierocki
Copy link

I am able to retrieve data correctly, however I get alarming warning-like messages:

Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x732ff0fdede0>
Unclosed connector
connections: ['deque([(<aiohttp.client_proto.ResponseHandler object at 0x732ff0fe5910>, 12327.646955165)])']
connector: <aiohttp.connector.TCPConnector object at 0x732ff0fded20>

This should be a complete, reproducible example.

import io

import aiohttp
from gcloud.aio.auth import Token
from gcloud.aio.storage import Storage


async def retrive_data_gcs(bucket_object_map: dict[str, str], service_file_path: str) -> dict[str, io.BytesIO]:
    """Retrives multiple objects (files) from Google Cloud Storage asynchronously."""

    token = Token(service_file_path, scopes=["https://www.googleapis.com/auth/devstorage.read_only"])

    retrived_objects = {}  # not creating aiohttp.ClientSession leads to exactly same result
    async with aiohttp.ClientSession() as session, Storage(token=token, session=session) as client:
        for bucket_name, file_name in bucket_object_map.items():
            byte_data = await client.download(bucket_name, file_name, session=session)
            retrived_objects[file_name] = io.BytesIO(byte_data)

    return retrived_objects


if __name__ == "__main__":
    import asyncio

    from uvloop import EventLoopPolicy

    asyncio.set_event_loop_policy(EventLoopPolicy())
    loop = asyncio.new_event_loop()

    bytes_data = loop.run_until_complete(
        retrive_data_gcs(
            {"some_bucket_name": "some_file_name"},   # replace with actual values
            "some_service_account_key.json",   # replace with actual values
        )
    )
    loop.close()

I tried rewriting the code multiple times, manually calling close etc.. Nothing helps. If I do not create session manually, I get exactly the same effect as well...

Could you recommend how to fix it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant