question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Async/Await unclosed connector

See original GitHub issue

I’m using async call inside class Map method _get_city_center_coord.

import ipinfo
import folium

class Map:
    def __init__(self, event_coordinate=None, zoom_start=15, popup=None, tooltip=None):
        self.event_coordinate = event_coordinate
        self.zoom_start = zoom_start
        self.popup = popup
        self.tooltip = tooltip

    @staticmethod
    async def _get_city_center_coord():
        handler = ipinfo.getHandlerAsync('ipinfo_access_token')
        details = await handler.getDetails()
        lat, lon = (details.latitude, details.longitude)
        return lat, lon

    async def show_events(self, event_list):
        m = await self._init_map()
        for event in event_list:
            id, popup, lat, long, tooltip = event
            folium.Marker(
                location=list((lat, long)),
                popup=popup,
                tooltip=tooltip,
                icon=folium.Icon(color="red", icon="info-sign")
            ).add_to(m)
        return m

    async def _init_map(self):
        if self.event_coordinate:
            return folium.Map(location=self.event_coordinate, zoom_start=self.zoom_start)
        else:
            return folium.Map(
                location=await self._get_city_center_coord(),
                zoom_start=self.zoom_start
            )

And this code used by fastapi framework this way

@router.get('/all', response_model=List[EventList])
async def events_list(
        request: Request,
        service: EventsService = Depends(),
        user: User = Depends(UserService.get_authenticated_user_id),
):
    events = await service.get_list()
    events_data = [
        (
            event['id'], event['title'], event['location']['lat'],
            event['location']['long'], event['activity']['name']
        )
        for event in events
    ]
    m = await Map(zoom_start=12).show_events(events_data)
    return templates.TemplateResponse(
        'events.html',
        context={
            'request': request,
            'events': events,
            'm': m._repr_html_(),
            'user': user
        }
        )

Everything works fine, IP coordinates are detected and the map is displayed. But in the console I see a warning

Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f25f9e1e800>
Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x7f25f9e2bac0>, 178355.411)]']
connector: <aiohttp.connector.TCPConnector object at 0x7f25f9e1dff0>

Is there a way to close the connection after a request has been made? It looks like there is a lack of async with syntax implementation

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
e-lepilovcommented, Oct 26, 2022

Thank you for your help. I’ll try to figure it out on my side.

1reaction
UmanShahzadcommented, Oct 5, 2022

We’ll check why there may be a leak there, and also if we can implement async with. Thanks for reporting.

Read more comments on GitHub >

github_iconTop Results From Across the Web

aiohttp: Unclosed client session client_session - Stack Overflow
I have a test.py file and a AsyncioCurl.py file. I already use ...
Read more >
Unclosed connector / client session when using asyncio #13242
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external ...
Read more >
Using Asyncio with Elasticsearch
Receiving 'Unclosed client session / connector' warning?¶. This warning is created by aiohttp when an open HTTP connection is garbage collected. You'll ...
Read more >
Asyncpraw/aiohttp issue : r/redditdev
I get these "Unclosed client session" and "Unclosed connector" messages, which lead into: Traceback (most recent call last): File "D:\Code ...
Read more >
Why do I keep getting this message: "Unclosed client session"?
I am trying to encode a live event using async functions for python and my program looks somewhat like: client = AzureMediaServices(credentials, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found