Skip to content

Commit 6904bc0

Browse files
CSHARP-2768: Server selection times again out after launching mongocryptd process.
1 parent 5f95a59 commit 6904bc0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/MongoDB.Driver/Encryption/AutoEncryptionLibMongoController.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
*/
1515

1616
using System;
17+
using System.Diagnostics;
18+
using System.Linq;
1719
using System.Threading;
1820
using System.Threading.Tasks;
1921
using MongoDB.Bson;
2022
using MongoDB.Driver.Core.Misc;
23+
using MongoDB.Driver.Core.Servers;
2124
using MongoDB.Driver.Core.WireProtocol;
2225
using MongoDB.Libmongocrypt;
2326

@@ -181,6 +184,7 @@ private void ProcessNeedMongoMarkingsState(CryptContext context, string database
181184
catch (TimeoutException) when (attempt == 1)
182185
{
183186
_mongocryptdFactory.SpawnMongocryptdProcessIfRequired();
187+
WaitForMongocryptdReady();
184188
}
185189
}
186190

@@ -205,6 +209,7 @@ private async Task ProcessNeedMongoMarkingsStateAsync(CryptContext context, stri
205209
catch (TimeoutException) when (attempt == 1)
206210
{
207211
_mongocryptdFactory.SpawnMongocryptdProcessIfRequired();
212+
await WaitForMongocryptdReadyAsync().ConfigureAwait(false);
208213
}
209214
}
210215

@@ -223,5 +228,35 @@ private void RestoreDbNodeInResponse(BsonDocument request, BsonDocument response
223228
}
224229
}
225230
}
231+
232+
private void WaitForMongocryptdReady()
233+
{
234+
var stopwatch = Stopwatch.StartNew();
235+
while (stopwatch.Elapsed < TimeSpan.FromSeconds(5))
236+
{
237+
var clusterDescription = _mongocryptdClient.Cluster?.Description;
238+
var mongocryptdServer = clusterDescription?.Servers?.FirstOrDefault();
239+
if (mongocryptdServer != null && mongocryptdServer.Type != ServerType.Unknown)
240+
{
241+
return;
242+
}
243+
Thread.Sleep(TimeSpan.FromMilliseconds(5));
244+
}
245+
}
246+
247+
private async Task WaitForMongocryptdReadyAsync()
248+
{
249+
var stopwatch = Stopwatch.StartNew();
250+
while (stopwatch.Elapsed < TimeSpan.FromSeconds(5))
251+
{
252+
var clusterDescription = _mongocryptdClient.Cluster?.Description;
253+
var mongocryptdServer = clusterDescription?.Servers?.FirstOrDefault();
254+
if (mongocryptdServer != null && mongocryptdServer.Type != ServerType.Unknown)
255+
{
256+
return;
257+
}
258+
await Task.Delay(TimeSpan.FromMilliseconds(5)).ConfigureAwait(false);
259+
}
260+
}
226261
}
227262
}

0 commit comments

Comments
 (0)