MongoWaitQueueFullException: The wait queue for acquiring a connection to server is full
<p>Sometimes when inserting a small bunch of different document (synchronously), I get the following exception (see full stack trace further down):</p>

<blockquote>
  <p>MongoDB.Driver.MongoWaitQueueFullException: The wait queue for
  acquiring a connection to server xyz.mongolab.com:54128 is full.</p>
</blockquote>

<p>I am using singleton <code>MongoDatabase</code> instance (and thus a single connection) between all my repositories.  Essentially, I am doing something like this (with no more than 20 documents in each collection):</p>

<pre><code>Context.Collection&lt;ClientDocument&gt;("clients").InsertMany(clients);
Context.Collection&lt;VendorDocument&gt;("vendors").InsertMany(vendors);
Context.Collection&lt;SaleDocument&gt;("sales").InsertOne(sale);
</code></pre>

<p>Below is the singleton context:</p>

<pre><code>public class MongoContext
{
    public IMongoDatabase Database { get; }

    public MongoContext(IOptions&lt;MongoSettings&gt; settings)
    {
        var url = MongoUrl.Create(settings.Value.EndpointUri);

        var client = new MongoClient(new MongoClientSettings()
        {
            Server = url.Server
        });

        Database = client.GetDatabase(url.DatabaseName);
    }

    public IMongoCollection&lt;TDocument&gt; Collection&lt;TDocument&gt;(string collection)
        where TDocument : IDocument
    {
        return Database.GetCollection&lt;TDocument&gt;(collection);
    }
}
</code></pre>

<p>Something similar was filed on MongoDB's Jira (<a href="https://jira.mongodb.org/browse/CSHARP-1144" rel="noreferrer">https://jira.mongodb.org/browse/CSHARP-1144</a>) but these cases are dealing with huge bulk inserts (and often asynchronously). </p>

<p>I don't see the need to increase MaxConnectionPoolSize or WaitQueueSize with such small inserts.</p>

<p>What could be the cause of this?</p>

<p>I am using MongoDB 3.0.7 hosted in mLabs. Our application is hosted in Azure (as a Web App) and I am using the C# 2.2.3 SDK.</p>

<blockquote>
  <p>MongoDB.Driver.MongoWaitQueueFullException: The wait queue for
  acquiring a connection to server xyz.mongolab.com:54128 is full.    at
  MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.AcquireConnectionHelper.CheckingOutConnection()
  at
  MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.AcquireConnection(CancellationToken
  cancellationToken)    at
  MongoDB.Driver.Core.Servers.ClusterableServer.GetChannel(CancellationToken
  cancellationToken)    at
  MongoDB.Driver.Core.Bindings.ServerChannelSource.GetChannel(CancellationToken
  cancellationToken)    at
  MongoDB.Driver.Core.Bindings.ChannelSourceHandle.GetChannel(CancellationToken
  cancellationToken)    at
  MongoDB.Driver.Core.Operations.BulkMixedWriteOperation.Execute(IWriteBinding
  binding, CancellationToken cancellationToken)    at
  MongoDB.Driver.OperationExecutor.ExecuteWriteOperation[TResult](IWriteBinding
  binding, IWriteOperation'1 operation, CancellationToken
  cancellationToken)    at
  MongoDB.Driver.MongoCollectionImpl'1.ExecuteWriteOperation[TResult](IWriteOperation`1
  operation, CancellationToken cancellationToken)    at
  MongoDB.Driver.MongoCollectionImpl'1.BulkWrite(IEnumerable'1 requests,
  BulkWriteOptions options, CancellationToken cancellationToken)    at
  MongoDB.Driver.MongoCollectionBase'1.InsertOne(TDocument document,
  InsertOneOptions options, CancellationToken cancellationToken)</p>
</blockquote>

<p>EDIT:</p>

<p>If I set MaxConnectionPoolSize to 500 and WaitQueueSize to 2000, then I get the following exception:</p>

<blockquote>
  <p>MongoConnectionException: An exception occurred while opening a
  connection to the server. ---> System.Net.Sockets.SocketException: An
  attempt was made to access a socket in a way forbidden by its access
  permissions 191.235.xxx.xxx:54128</p>
</blockquote>

<p>Instantiating <code>MongoClient</code>:</p>

<pre><code>var client = new MongoClient(new MongoClientSettings()
{
    Server = url.Server,
    Credentials = credentials,
    MaxConnectionPoolSize = 500,
    WaitQueueSize = 2000
});
</code></pre>

<p>I raised this problem initially <a href="https://stackoverflow.com/questions/37292820/intermittent-mongodb-connection-issue-an-attempt-was-made-to-access-a-socket-in/37319765#37319765">here</a>.  This led to me trying to figure out why on earth I have so many connections.  This led to <a href="https://stackoverflow.com/questions/37322646/mongodb-exceeding-server-socket-connection-limit">this post</a> (questioning if Insert/InsertBulk could be a cause). Regardless, I still need to fix the original MongoWaitQueueFullException problem.</p>