Skip to content

Commit f309626

Browse files
committed
CSHARP-1970: Handle unexpected empty batches from server.
1 parent b3b7b5f commit f309626

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/MongoDB.Driver.GridFS/GridFSForwardOnlyDownloadStream.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2016 MongoDB Inc.
1+
/* Copyright 2016-2017 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -244,27 +244,41 @@ private async Task GetFirstBatchAsync(CancellationToken cancellationToken)
244244

245245
private void GetNextBatch(CancellationToken cancellationToken)
246246
{
247-
var hasMore = _cursor.MoveNext(cancellationToken);
248-
GetNextBatchFromCursor(hasMore);
247+
List<BsonDocument> batch;
248+
do
249+
{
250+
var hasMore = _cursor.MoveNext(cancellationToken);
251+
batch = hasMore ? _cursor.Current.ToList() : null;
252+
}
253+
while (batch != null && batch.Count == 0);
254+
255+
ProcessNextBatch(batch);
249256
}
250257

251258
private async Task GetNextBatchAsync(CancellationToken cancellationToken)
252259
{
253-
var hasMore = await _cursor.MoveNextAsync(cancellationToken).ConfigureAwait(false);
254-
GetNextBatchFromCursor(hasMore);
260+
List<BsonDocument> batch;
261+
do
262+
{
263+
var hasMore = await _cursor.MoveNextAsync(cancellationToken).ConfigureAwait(false);
264+
batch = hasMore ? _cursor.Current.ToList() : null;
265+
}
266+
while (batch != null && batch.Count == 0);
267+
268+
ProcessNextBatch(batch);
255269
}
256270

257-
private void GetNextBatchFromCursor(bool hasMore)
271+
private void ProcessNextBatch(List<BsonDocument> batch)
258272
{
259-
if (!hasMore)
273+
if (batch == null)
260274
{
261275
#pragma warning disable 618
262276
throw new GridFSChunkException(_idAsBsonValue, _nextChunkNumber, "missing");
263277
#pragma warning restore
264278
}
265279

266280
var previousBatch = _batch;
267-
_batch = _cursor.Current.ToList();
281+
_batch = batch;
268282

269283
if (previousBatch != null)
270284
{

0 commit comments

Comments
 (0)