Skip to content

Unclosed client session asyncio ERROR logs from internal aiohttp sessions #156

@sunayana

Description

@sunayana

What happened?

When using cdsapi.Client() to retrieve data, asyncio logs multiple ERROR-level messages about unclosed aiohttp client sessions. These
appear during garbage collection and create noise in application logs, even though data retrieval completes successfully.

Environment

  • cdsapi version: [your version, e.g., 0.7.x]
  • Python version: 3.12
  • OS: Linux (AWS Batch container)

Steps to Reproduce

  import cdsapi                                                                                                                             
                                                                                                                                            
  client = cdsapi.Client()                                                                                                                  
  client.retrieve(                                                                                                                          
      'sis-agrometeorological-indicators',                                                                                                  
      {                                                                                                                                     
          'variable': '2m_temperature',                                                                                                     
          'year': '2024',                                                                                                                   
          'month': '01',                                                                                                                    
          'day': '01',                                                                                                                      
          'version': '2_0',                                                                                                                 
      },                                                                                                                                    
      'output.zip'                                                                                                                          
  )                                                                                                                                        

Observed Behavior

2026-01-19 06:37:54,906 - asyncio - ERROR - Unclosed client session                                                                       
  client_session: <aiohttp.client.ClientSession object at 0x7fcda4d5a9f0>                                                                   
  2026-01-19 06:37:54,906 - asyncio - ERROR - Unclosed connector                                                                            
  connections: ['deque([...])']                                                                                                             
  connector: <aiohttp.connector.TCPConnector object at 0x7fcda4b9c740>                                                                      

These errors appear:

  1. Immediately after Client() initialization (before any API requests)
  2. At the end of the process during garbage collection

Expected Behavior

The library should properly close its internal aiohttp sessions, either:

  1. By exposing a close() method on the Client
  2. By implementing context manager support (enter/exit)
  3. By properly cleaning up sessions internally

Workaround

Currently, users can suppress these logs with a logging filter, but this isn't ideal:

import logging                                                                                                                            
                                                                                                                                          
class UnclosedSessionFilter(logging.Filter):                                                                                              
    def filter(self, record):                                                                                                             
        return "Unclosed client session" not in record.getMessage()                                                                       
                                                                                                                                          
logging.getLogger("asyncio").addFilter(UnclosedSessionFilter())                                                                           

Additional Context

The sessions appear to be created by ecmwf.datastores.legacy_client during initialization. The cdsapi.Client object has a session
attribute (requests.Session) and a client attribute (ecmwf.datastores.client.Client), but neither exposes the internal aiohttp sessions
for cleanup.

What are the steps to reproduce the bug?

import cdsapi                                                                                                                             
                                                                                                                                            
  client = cdsapi.Client()                                                                                                                  
  client.retrieve(                                                                                                                          
      'sis-agrometeorological-indicators',                                                                                                  
      {                                                                                                                                     
          'variable': '2m_temperature',                                                                                                     
          'year': '2024',                                                                                                                   
          'month': '01',                                                                                                                    
          'day': '01',                                                                                                                      
          'version': '2_0',                                                                                                                 
      },                                                                                                                                    
      'output.zip'                                                                                                                          
  )

Version

v0.7.x

Platform (OS and architecture)

Linux (AWS Batch container)

Relevant log output

2026-01-19 06:37:54,906 - asyncio - ERROR - Unclosed client session                                                                       
  client_session: <aiohttp.client.ClientSession object at 0x7fcda4d5a9f0>                                                                   
  2026-01-19 06:37:54,906 - asyncio - ERROR - Unclosed connector                                                                            
  connections: ['deque([...])']                                                                                                             
  connector: <aiohttp.connector.TCPConnector object at 0x7fcda4b9c740>

Accompanying data

No response

Organisation

CarbonSpace

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions