Skip to content

Commit 873db20

Browse files
committed
CSHARP-1461: fixing issue with calling Dispose on a null reference.
1 parent f499c6c commit 873db20

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

src/MongoDB.Driver.Core.Tests/Core/Connections/BinaryConnection_CommandEventTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,45 @@ public void Should_process_a_query_with_the_explain_modifier()
675675
commandSucceededEvent.RequestId.Should().Be(commandStartedEvent.RequestId);
676676
}
677677

678+
[Test]
679+
public void Should_process_a_failed_query()
680+
{
681+
var expectedCommand = new BsonDocument
682+
{
683+
{ "find", MessageHelper.DefaultCollectionNamespace.CollectionName },
684+
{ "filter", new BsonDocument("x", 1) },
685+
};
686+
var queryFailureDocument = BsonDocument.Parse("{ $err: \"Can't canonicalize query: BadValue $or needs an array\", code: 17287 }");
687+
688+
var requestMessage = MessageHelper.BuildQuery(
689+
(BsonDocument)expectedCommand["filter"],
690+
requestId: 10);
691+
SendMessages(requestMessage);
692+
693+
694+
var replyMessage = MessageHelper.BuildQueryFailedReply<BsonDocument>(
695+
queryFailureDocument,
696+
requestMessage.RequestId);
697+
ReceiveMessages(replyMessage);
698+
699+
var commandStartedEvent = (CommandStartedEvent)_capturedEvents.Next();
700+
var commandFailedEvent = (CommandFailedEvent)_capturedEvents.Next();
701+
702+
commandStartedEvent.CommandName.Should().Be(expectedCommand.GetElement(0).Name);
703+
commandStartedEvent.Command.Should().Be(expectedCommand);
704+
commandStartedEvent.ConnectionId.Should().Be(_subject.ConnectionId);
705+
commandStartedEvent.DatabaseNamespace.Should().Be(MessageHelper.DefaultDatabaseNamespace);
706+
commandStartedEvent.OperationId.Should().Be(EventContext.OperationId);
707+
commandStartedEvent.RequestId.Should().Be(requestMessage.RequestId);
708+
709+
commandFailedEvent.CommandName.Should().Be(commandStartedEvent.CommandName);
710+
commandFailedEvent.ConnectionId.Should().Be(commandStartedEvent.ConnectionId);
711+
commandFailedEvent.Duration.Should().BeGreaterThan(TimeSpan.Zero);
712+
commandFailedEvent.OperationId.Should().Be(commandStartedEvent.OperationId);
713+
((MongoCommandException)commandFailedEvent.Failure).Result.Should().Be(queryFailureDocument);
714+
commandFailedEvent.RequestId.Should().Be(commandStartedEvent.RequestId);
715+
}
716+
678717
[Test]
679718
public void Should_process_an_update_without_gle()
680719
{

src/MongoDB.Driver.Core.Tests/Core/Helpers/MessageHelper.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,24 @@ public static InsertMessage<T> BuildInsert<T>(
155155
false);
156156
}
157157

158+
public static ReplyMessage<T> BuildQueryFailedReply<T>(
159+
BsonDocument queryFailureDocument,
160+
int responseTo = 0)
161+
{
162+
return new ReplyMessage<T>(
163+
false,
164+
0,
165+
false,
166+
null,
167+
1,
168+
true,
169+
queryFailureDocument,
170+
0,
171+
responseTo,
172+
BsonSerializer.SerializerRegistry.GetSerializer<T>(),
173+
0);
174+
}
175+
158176
public static ReplyMessage<T> BuildReply<T>(
159177
T document,
160178
IBsonSerializer<T> serializer = null,

src/MongoDB.Driver.Core/Core/Connections/CommandEventHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ private void ProcessReplyMessage(CommandState state, ResponseMessage message, IB
599599
}
600600
finally
601601
{
602-
if (disposeOfDocuments)
602+
if (disposeOfDocuments && replyMessage.Documents != null)
603603
{
604604
replyMessage.Documents.ForEach(d => d.Dispose());
605605
}

0 commit comments

Comments
 (0)