To resolve Unclosed client session errors which occur when the Web App service is running as well as when the local start.sh script is used to run the application.
What problem does it solve?
As per issues #751 and #593 improper handling of database and other asynchronous connections results in Unclosed client session errors.
What scenario does it contribute to?
Using .aio libraries requires the proper handling and/or closing of asynchronous connections.
If it fixes an open issue, please link to the issue here.
It resolves issues #751 and #593.
Does this solve an issue or add a feature that all users of this sample app can benefit from? Contributions will only be accepted that apply across all users of this app.
It benefits all users who run the quart version of the app.
Description
As per the CosmosClient class in the azure-cosmos Python library, creating a database instance is a heavy operation:
... It's recommended to maintain a single instance of CosmosClient per lifetime of the application which enables efficient connection management and performance. CosmosClient initialization is a heavy operation, ...
To avoid multiple connection attempts, only a single global instance of the cosmos database connection is created in the init() function as part of the create_app() function. Only a single call to init_cosmosdb_client() is now needed instead of multiple calls within each function. This implementation leverages the current_app method of the quart library to maintain the database object's state during the lifetime of the application. When the database object is needed, it can be called using current_app, e.g., current_app.cosmos_conversation_client.create_message(...)
In addition, an Event is used to signal the readiness status of the database initialization to downstream functions to avoid the app calling methods of the CosmosConversationClient class before the client is completely initialized. Without this approach, a user would see an error on the frontend that disappears only once the initialization is complete. Functions can 'wait' on the database using the .wait() method of the Event class.
Another source of the Unclosed client session error stems from the use of the asynchronous import of DefaultAzureCredential from .aio. This connection is handled using a context created by the async with statement. Thanks to QuentinAd for the template.
Contribution Checklist
[ x ] I have built and tested the code locally and in a deployed app
[ x ] This is a change for all users of this app. No code or asset is specific to my use case or my organization.
[ x ] I didn't break any existing functionality :smile: 🤞
Motivation and Context
To resolve
Unclosed client session
errors which occur when the Web App service is running as well as when the localstart.sh
script is used to run the application.As per issues #751 and #593 improper handling of database and other asynchronous connections results in
Unclosed client session
errors.Using
.aio
libraries requires the proper handling and/or closing of asynchronous connections.It resolves issues #751 and #593.
It benefits all users who run the quart version of the app.
Description
As per the
CosmosClient
class in theazure-cosmos
Python library, creating a database instance is a heavy operation:To avoid multiple connection attempts, only a single global instance of the cosmos database connection is created in the
init()
function as part of thecreate_app()
function. Only a single call toinit_cosmosdb_client()
is now needed instead of multiple calls within each function. This implementation leverages thecurrent_app
method of thequart
library to maintain the database object's state during the lifetime of the application. When the database object is needed, it can be called usingcurrent_app
, e.g.,current_app.cosmos_conversation_client.create_message(...)
In addition, an
Event
is used to signal the readiness status of the database initialization to downstream functions to avoid the app calling methods of theCosmosConversationClient
class before the client is completely initialized. Without this approach, a user would see an error on the frontend that disappears only once the initialization is complete. Functions can 'wait' on the database using the.wait()
method of theEvent
class.Another source of the
Unclosed client session
error stems from the use of the asynchronous import ofDefaultAzureCredential
from.aio
. This connection is handled using a context created by theasync with
statement. Thanks to QuentinAd for the template.Contribution Checklist