<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Wed Feb 07 21:36:19 UTC 2024

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary append 'field=key&field=summary' to the URL of your request.
-->
<rss version="0.92" >
<channel>
    <title>MongoDB Jira</title>
    <link>https://jira.mongodb.org</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>9.7.1</version>
        <build-number>970001</build-number>
        <build-date>13-04-2023</build-date>
    </build-info>


<item>
            <title>[CSHARP-268] NullReferenceException on lost primary</title>
                <link>https://jira.mongodb.org/browse/CSHARP-268</link>
                <project id="10041" key="CSHARP">C# Driver</project>
                    <description>&lt;p&gt;3-server replica set (primary, slave, arbiter). Primary steps down, the next query (actually any queries) fail with NullReferenceException with the following stack trace: &lt;br/&gt;
	MongoDB.Driver.DLL!MongoDB.Driver.MongoServerInstance.AcquireConnection(MongoDB.Driver.MongoDatabase database) Line 183 + 0x8 bytes&lt;br/&gt;
 	MongoDB.Driver.DLL!MongoDB.Driver.MongoServer.AcquireConnection(MongoDB.Driver.MongoDatabase database, bool slaveOk) Line 893 + 0xf bytes&lt;br/&gt;
 	MongoDB.Driver.DLL!MongoDB.Driver.MongoCursorEnumerator&amp;lt;System.__Canon&amp;gt;.AcquireConnection() Line 184 + 0x42 bytes&lt;br/&gt;
 	MongoDB.Driver.DLL!MongoDB.Driver.MongoCursorEnumerator&amp;lt;MongoDB.Bson.BsonDocument&amp;gt;.GetFirst() Line 194 + 0xc bytes&lt;br/&gt;
 	MongoDB.Driver.DLL!MongoDB.Driver.MongoCursorEnumerator&amp;lt;System.__Canon&amp;gt;.MoveNext() Line 126 + 0x8 bytes&lt;/p&gt;

&lt;p&gt;It appears that connectionPool is null, probably because MongoServerInstance.Disconnect due to the connection error. Might be related to &lt;a href=&quot;https://jira.mongodb.org/browse/CSHARP-217&quot; title=&quot;Disposing a cursor that used a connection that failed leads to NullReferenceException&quot; class=&quot;issue-link&quot; data-issue-key=&quot;CSHARP-217&quot;&gt;&lt;del&gt;CSHARP-217&lt;/del&gt;&lt;/a&gt; and &lt;a href=&quot;https://jira.mongodb.org/browse/CSHARP-233&quot; title=&quot;MongoServerInstance.Disconnect() contains an NRE point&quot; class=&quot;issue-link&quot; data-issue-key=&quot;CSHARP-233&quot;&gt;&lt;del&gt;CSHARP-233&lt;/del&gt;&lt;/a&gt;. I&apos;ll post more information later.&lt;/p&gt;</description>
                <environment>git version d7ce7f2ee560183d8031</environment>
        <key id="19455">CSHARP-268</key>
            <summary>NullReferenceException on lost primary</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</type>
                                            <priority id="3" iconUrl="https://jira.mongodb.org/images/icons/priorities/major.svg">Major - P3</priority>
                        <status id="6" iconUrl="https://jira.mongodb.org/images/icons/statuses/closed.png" description="The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.">Closed</status>
                    <statusCategory id="3" key="done" colorName="success"/>
                                    <resolution id="9">Done</resolution>
                                        <assignee username="robert@mongodb.com">Robert Stam</assignee>
                                    <reporter username="onyxmaster">Aristarkh Zagorodnikov</reporter>
                        <labels>
                    </labels>
                <created>Wed, 13 Jul 2011 12:05:31 +0000</created>
                <updated>Thu, 2 Apr 2015 18:28:22 +0000</updated>
                            <resolved>Mon, 1 Aug 2011 19:08:08 +0000</resolved>
                                    <version>1.2</version>
                                    <fixVersion>1.2</fixVersion>
                                                        <votes>0</votes>
                                    <watches>0</watches>
                                                                                                                <comments>
                            <comment id="45826" author="onyxmaster" created="Tue, 2 Aug 2011 10:09:33 +0000"  >&lt;p&gt;My default RS setup: primary, secondary, arbiter; slaveOk is false. Primary goes down, all following queries fail with InvalidOperationException: Server instance &amp;lt;host&amp;gt;:&amp;lt;port&amp;gt; is no longer connected. (host/port match the ones of the gone server). Stack trace follows:&lt;/p&gt;

&lt;p&gt;	MongoDB.Driver.DLL!MongoDB.Driver.MongoServerInstance.AcquireConnection(MongoDB.Driver.MongoDatabase database) Line 200	C#&lt;br/&gt;
 	MongoDB.Driver.DLL!MongoDB.Driver.MongoServer.AcquireConnection(MongoDB.Driver.MongoDatabase database, bool slaveOk) Line 946 + 0xe bytes	C#&lt;br/&gt;
 	MongoDB.Driver.DLL!MongoDB.Driver.MongoCursorEnumerator&amp;lt;System.__Canon&amp;gt;.AcquireConnection() Line 184 + 0x42 bytes	C#&lt;br/&gt;
 	MongoDB.Driver.DLL!MongoDB.Driver.MongoCursorEnumerator&amp;lt;MongoDB.Bson.BsonDocument&amp;gt;.GetFirst() Line 194 + 0xc bytes	C#&lt;br/&gt;
 	MongoDB.Driver.DLL!MongoDB.Driver.MongoCursorEnumerator&amp;lt;System.__Canon&amp;gt;.MoveNext() Line 126 + 0x8 bytes	C#&lt;/p&gt;</comment>
                            <comment id="45703" author="rstam" created="Mon, 1 Aug 2011 19:08:09 +0000"  >&lt;p&gt;Comitted a fix for the deadlock. Added an additional lock (stateLock) to synchronize lower level state changes that happen in response to events on multiple threads. The remaining MongoServer operations are synchronized with serverLock.&lt;/p&gt;

&lt;p&gt;p.s. Still need to do some more testing on replica sets (specially failover) so there may be further change forthcoming.&lt;/p&gt;</comment>
                            <comment id="45661" author="rstam" created="Mon, 1 Aug 2011 17:38:15 +0000"  >&lt;p&gt;Fix had problems with deadlocks. More work needed.&lt;/p&gt;</comment>
                            <comment id="45516" author="rstam" created="Mon, 1 Aug 2011 05:05:32 +0000"  >&lt;p&gt;Added locking to MongoServerInstance to make it thread safe. Simplified locking in MongoServer by using just one lock (multiple locks were confusing and had the potential for deadlock).&lt;/p&gt;</comment>
                            <comment id="42123" author="onyxmaster" created="Wed, 13 Jul 2011 12:45:28 +0000"  >&lt;p&gt;It also appears that MongoServer.AcquireConnection(MongoDatabase, MongoServerInstance) call hierarchy is not lock-protected at all (looks like it&apos;s used for cursors since they &quot;bind&quot; to a specific server instance), so its&apos; calls to MongoServerInstance.AcquireConnection is also vulnerable to a race condition with MongoServerInstance.Disconnect calls.&lt;/p&gt;</comment>
                            <comment id="42122" author="onyxmaster" created="Wed, 13 Jul 2011 12:36:02 +0000"  >&lt;p&gt;Yes, MongoServerInstance.Disconnect is called and the primary-before-the-step-down MongoServerInstance becomes disconnected, although MongoServer.GetServerInstance keeps returning it using the MongoServer.Primary property.&lt;br/&gt;
I&apos;m not sure what protocol is followed by the replica set connector to reconnect to the servers, but I believe that MongoServer.GetServerInstance should check the server that is returned Primary property to be connected (it checks slaves to be connected if slaveOk is enabled) and probably treat this as a &quot;no primary&quot; case, behaving as if Primary returned null (for some reasons that I can&apos;t explain even to myself I don&apos;t think that Primary property itself should check if it&apos;s connected).&lt;/p&gt;

&lt;p&gt;Also, I would like to note that it appears that MongoServerInstance.Disconnect and MongoServerInstance.AcquireConnection can be called concurrently (there is no consistent locks around all of these calls), so this might lead to some kind of a nasty race condition. &lt;br/&gt;
For example, consider there is a network failure, so MongoConnection.SendMessage fails, calls MongoConnection.Disconnect. This path is protected only by the lock (MongoConnection.connectionLock). Now, some other thread performs a cursor enumeration that leads to a need to acquire a connection, hence the MongoServer.AcquireConnection is called, that calls GetServerInstance that can lead to the same NRE, because AcquireConnection locks on the MongoServer.serverLock.&lt;br/&gt;
So, it appears that MongoServer performs connection management (acquiring connections) using MongoServer.serverLock, while disconnection is handled using MongoConnection.connectionLock only and not all Disconnect call hierarchies are protected with the MongoServer.serverLock:&lt;br/&gt;
1. ReplicaSetConnector.ProcessAdditionalResponsesWorkItem is called from a thread pool thread that does not do any locking&lt;br/&gt;
2. MongoConnection.SendMessage calls HandleException under connection lock only&lt;br/&gt;
3. MongoConnection.ReceiveMessage calls HandleException under connection lock only&lt;/p&gt;</comment>
                    </comments>
                <issuelinks>
                            <issuelinktype id="10010">
                    <name>Duplicate</name>
                                                                <inwardlinks description="is duplicated by">
                                        <issuelink>
            <issuekey id="20179">CSHARP-288</issuekey>
        </issuelink>
                            </inwardlinks>
                                    </issuelinktype>
                            <issuelinktype id="10012">
                    <name>Related</name>
                                            <outwardlinks description="related to">
                                        <issuelink>
            <issuekey id="20346">CSHARP-294</issuekey>
        </issuelink>
                            </outwardlinks>
                                                        </issuelinktype>
                    </issuelinks>
                <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|hrh8pr:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>14211</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </customfields>
    </item>
</channel>
</rss>