Session lifecycle and service resolution
The session is the shared async entry point for service model data, credentials, configuration, and client creation. It turns a service name into a ready service client through an awaitable setup step, then keeps the client and its network resources valid only for the lifetime that the caller manages.
Session role
aiobotocore/session.py defines AioSession as the place where shared client inputs are coordinated. A session can resolve service data and provide shared components before it creates any client. The session also selects the async primitives that match the configured HTTP backend, so related providers use the same async model.
Use get_session to create a new session. Session creation itself is synchronous and does not create a network client. The async work starts when a caller resolves credentials, loads service data, or enters a client creation context.
References: aiobotocore/session.py• L44-65 aiobotocore/session.py• L114-125 aiobotocore/session.py• L356-360 aiobotocore/_async_primitives.py• L8-16
Session initialization flow
Client acquisition is an async flow. The caller asks the session for a client context, enters that context, uses the resulting client, and exits the context to release the client's network resources.
The following diagram shows the lifetime boundary between a session, a client, and the underlying HTTP resources.
sequenceDiagram
participant Caller
participant Session as AioSession
participant Client as Service client
participant HTTP as HTTP resources
Caller->>Session: create_client(service, options)
Session-->>Caller: client creation context
Caller->>Session: async enter
Session->>Session: resolve config, credentials, and service data
Session->>Client: construct client
Client->>HTTP: enter transport resources
Client-->>Caller: usable client
Caller->>Client: await service operation
Caller->>Client: async exit or close
Client->>HTTP: close transport resources
create_client returns a ClientCreatorContext, not the client itself. Entering it awaits client construction and then enters the client. This design makes the normal usage pattern explicit: keep all client operations inside async with session.create_client(...) or close the client yourself when the work ends.
References: aiobotocore/session.py• L31-42 aiobotocore/session.py• L187-204 aiobotocore/session.py• L320-336 aiobotocore/client.py• L673-678
Service model resolution
The service model is the description that gives a service client its operations and service metadata. The session loads service data by service name and optional API version, then exposes the loaded description either as raw service data or as a ServiceModel object.
Service model lookup follows this path:
get_service_dataasks the session's data loader for the service description.- The session emits a service-data-loaded event so registered handlers can observe the loaded data.
get_service_modelwraps that description in aServiceModelfor callers that need the model view.- Client creation loads the model again through the client creator as part of building service operations and endpoint information.
An unknown service can fail during model loading. Optional supporting data, such as paginator or waiter descriptions, may be absent without making cache warm-up fail. Cache warm-up is an optional preparation step. When enabled through client configuration, it moves model file work away from the main async path before the client is built.
References: aiobotocore/session.py• L121-142 aiobotocore/session.py• L144-185 aiobotocore/session.py• L227-245 aiobotocore/client.py• L42-75 tests/…/test_session.py• L148-158
Component access
Session components are shared services that supply the inputs needed for client creation. The most important examples are the data loader, credential provider, configuration store, event emitter, response parser factory, endpoint resolver, and exception factory. Callers can access supported components through the session's inherited component API when they need to inspect or customize shared behavior.
Component use has two important effects:
- Credential access is asynchronous. If credentials are not already set on the session,
get_credentialsasks the credential provider to load them and stores the result for later use. - The configured HTTP session class selects the async primitive family used by credential and metadata providers. A session configured for the HTTPX backend therefore keeps nested providers aligned with that backend.
Client creation reads these shared components, combines them with per-client settings, and passes the resulting services to the client creator. Explicit credentials or an unsigned configuration can avoid credential-provider loading. Partial explicit credentials are rejected instead of silently falling back to another source.
References: aiobotocore/session.py• L61-65 aiobotocore/session.py• L75-105 aiobotocore/session.py• L107-119 aiobotocore/session.py• L247-319 tests/test_session.py• L270-316
Client and resource lifetime
The client owns the endpoint and HTTP session used for its requests. Entering the client starts those resources. Exiting the client or calling close ends them, so a client must not outlive the context that owns its transport.
The session remains available after a client closes. This lets one session create more than one client while keeping shared model, configuration, and credential state in one place. Each client still has its own client lifetime boundary, and callers must not use a closed client or retain response streams beyond the resource lifetime that supports them.
The default aiohttp-based transport creates its internal sessions when it is entered and makes them unusable after exit. The HTTPX transport follows the same client-facing contract. This common boundary is why callers should prefer the client context manager rather than rely on process shutdown for cleanup.
References: aiobotocore/client.py• L330-351 aiobotocore/client.py• L673-678 aiobotocore/httpsession.py• L70-87 aiobotocore/httpsession.py• L124-136 aiobotocore/httpxsession.py• L297-348 aiobotocore/httpxsession.py• L383-406
Caller boundaries
Callers should separate the session lifetime from the client lifetime. Keep the session for as long as shared state is useful, and keep each client context open for every operation that uses that client.
session = get_session()
async with session.create_client("s3", region_name="us-east-1") as client:
response = await client.list_buckets()
For manual control, await the creation coroutine through the context object and call close when the client is no longer needed. The context-manager form is safer because it covers both client setup and cleanup, including setup failures after resources begin to open.
For a broader view of the public session and client surface, read Session and client API. For the next stage of client setup, read Client construction and customization.
References: aiobotocore/session.py• L31-42 aiobotocore/session.py• L187-204 aiobotocore/client.py• L349-351 aiobotocore/client.py• L673-678 tests/…/test_session.py• L160-180
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to the component names or registration order can alter when credentials, models, endpoint data, or event hooks become available during client creation.
- Changes to
ClientCreatorContextor client enter/exit behavior must preserve the contract that a yielded client has an active transport and that exit closes it. - Changes to HTTP backend selection must keep the session's async primitive choice aligned with nested credential and metadata providers.
- Changes to service model loading or event names can affect plugins and callers that observe
service-data-loadedbefore client construction. - Changes to optional model warm-up must keep missing paginator, waiter, or example data from blocking services that do not provide those files.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Client creation context is not entered | Client construction does not complete, so callers have no usable service client | Use async with session.create_client(...) or explicitly await the context's enter operation |
| Client is used after exit or close | The underlying HTTP resources are closed and requests can fail | Keep all operations inside the client lifetime and close it only after the final operation |
| Only one explicit credential value is supplied | Client creation raises PartialCredentialsError | Supply the complete explicit credential set or omit explicit credentials so the provider can load them |
| Service name or API version has no matching model | Service resolution raises an unknown-service error | Use a supported service and API version, or provide matching service data through the normal loader path |
| A client uses a backend-specific nested provider with a mismatched async model | Credential or metadata work can use the wrong async primitives | Set the backend through the session's client configuration so dependent providers infer the same model |
Key files
| File | Why to read it |
|---|---|
| aiobotocore/session.py | Defines the session entry point, service model lookup, component use, and client lifetime wrapper. |
| aiobotocore/client.py | Defines client construction handoff and the client's async enter, exit, and close boundary. |
| aiobotocore/_async_primitives.py | Shows how the configured HTTP backend determines the async primitive family. |
| aiobotocore/httpsession.py | Shows the default transport's resource entry and cleanup contract. |
| aiobotocore/httpxsession.py | Shows the alternate transport's matching entry and cleanup contract. |
| tests/…/test_session.py | Demonstrates the public session flow, model lookup, configuration precedence, and credential rules. |