An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full

Mehboob Ahmad 61 Reputation points
2023-02-12T15:49:14.8466667+00:00

Using Microsoft.Azure.Cosmos Fetch Data Form Azure Cosmos Db.
Facing Error on Live Site After 4k request**
Error:**
An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full.

Generic Call:

public class CosmosConfigurationRepository : IDisposable
    {
        /// <summary>
        /// The database identifier
        /// </summary>
        private readonly string databaseId = string.Empty;

        /// <summary>
        /// The collection identifier
        /// </summary>
        private readonly string collectionId = string.Empty;

        /// <summary>
        /// The client
        /// </summary>
        private readonly CosmosClient client;

        /// <summary>
        /// The container
        /// </summary>
        private readonly Container container;

        /// <summary>
        /// Initializes a new instance of the <see cref="CosmosConfigurationRepository"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public CosmosConfigurationRepository(SnmpCosmosContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            this.databaseId = context.DatabaseId;
            this.collectionId = context.CollectionId;
            string endpoint = context.Endpoint;
            string authKey = context.AuthKey;
            this.client = new CosmosClient(endpoint, authKey);
            this.container = this.client.GetContainer(this.databaseId, this.collectionId);
        }

        /// Finalizes an instance of the <see cref="CosmosConfigurationRepository"/> class
        /// </summary>
        ~CosmosConfigurationRepository()
        {
            this.Dispose(false);
        }

        /// <summary>
        /// The ExecuteQuery.
        /// </summary>  
        /// <returns>container   <see cref="Container"/>Object type.</returns>
        public Container GetCosmosContainer()
        {
            return this.container;
        }

        /// <summary>
        /// The ExecuteQuery.
        /// </summary>
        /// <typeparam name="T">Object type.</typeparam>
        /// <param name="query">The query<see cref="QueryDefinition"/>.</param>
        /// <returns>The <see cref="FeedIterator{T}"/>Object type.</returns>
        public FeedIterator<T> ExecuteQuery<T>(QueryDefinition query)
        {
            return this.container.GetItemQueryIterator<T>(query, requestOptions: new       QueryRequestOptions() { MaxItemCount = -1 });
        }
}
public async Task<List<PreDeliveryInspection>> GetPreDeliveryInspectionGridDataAsync(string fistName, string lastName)
        {
            List<PreDeliveryInspection> pdiDetails = new List<PreDeliveryInspection>();

            QueryDefinition query =
           new QueryDefinition(
           "SELECT c.branchData  FROM c Where c.lastName=lastName and c.lastName=@serial")
           .WithParameter("@fistName", fistName).WithParameter("@serial", serial) ;

            this.cosmosdbcredentials.CollectionId = this.containerConfig.BranchSystemData;

            this.cosmosdb = new CosmosConfigurationRepository(this.cosmosdbcredentials);
            FeedIterator<PreDeliveryInspection> data 
               = this.cosmosdb.ExecuteQuery<PreDeliveryInspection>(query);

            while (data.HasMoreResults)
            {
                var response = await data.ReadNextAsync().ConfigureAwait(false);
                pdiDetails.AddRange(response);
            }

            return pdiDetails;
        }
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,478 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,423 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rahul Randive 8,756 Reputation points Microsoft Employee
    2023-02-12T16:25:55.46+00:00

    Thanks for your question. 

    I believe the no buffer space available exception is happening because with the default behavior of the SDK which maintain connections open indefinitely. You may be running into this error due to this, by opening enough connections to run into the error.

     

    This error message is typically caused by a lack of available resources on your client machine. It's recommended to monitor the resource utilization on the nodes running the Azure Cosmos DB client and scale up or out if they're running at high load.

    Additionally, you can check the portal metrics to determine if it's a client-side issue or if there's an issue with the service.

    Another possible resolution for this incident could be an issue of checking the max number of connections that can be established by your application and calibrating the idleConnectionTimeout value properly for the application use-case. 

    Sharing some reference document related to similar error-

     

    https://github.com/Azure/Azure-Functions/issues/1127

    https://social.msdn.microsoft.com/Forums/azure/en-US/d81b8aad-0253-4059-9996-ed0ea65f49be/cosmosdb-replacedocumentasync-an-operation-on-a-socket-could-not-be-performed-because-the-system?forum=azurecosmosdb  

    https://stackoverflow.com/questions/62928536/avoid-an-operation-on-a-socket-could-not-be-performed-because-the-system-lacke

    Hope this helps!

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful