<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 08:51:52 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>[JAVA-263] NullPointerException due to race condition during concurrent access to DBTCPTransport</title>
                <link>https://jira.mongodb.org/browse/JAVA-263</link>
                <project id="10006" key="JAVA">Java Driver</project>
                    <description>&lt;p&gt;DBTCPConnector._set() is susceptible to a race condition where two threads call it at the same time, allowing a thread to mistaking think _masterPortPool is set when it is still null, and go on to cause a NullPointerException in DBTCPConnector$MyPort.get().&lt;/p&gt;

&lt;p&gt;Further detail:&lt;/p&gt;

&lt;p&gt;I have two threads invoking DB.getCollection(&quot;differentCollectionForEachThread&quot;).drop() at approximately the same time. These are the first connections to Mongo. Approximately every 2nd run of this causes a NullPointerException in DBTCPConnector$MyPort.get(). I&apos;ve tried to make a simple test app to reproduce, but can&apos;t - timing issues are tricky to replicate.&lt;/p&gt;

&lt;p&gt;The problem occurs here:&lt;/p&gt;

&lt;p&gt;    private boolean _set( ServerAddress addr )&lt;/p&gt;
{
        if ( _curMaster == addr) // should check that _masterPortPool != null
            return false;
        _curMaster = addr; // _curMaster set before _masterPortPool. At this point _masterPortPool is still null.
        _masterPortPool = _portHolder.get( addr );
        return true;
    }

&lt;p&gt;Then in MyPort.get() the NPE happens:&lt;/p&gt;

&lt;p&gt;            _pool = _masterPortPool;&lt;br/&gt;
            DBPort p = _pool.get(); // NPE&lt;/p&gt;

&lt;p&gt;In the above code, &lt;/p&gt;
&lt;ul class=&quot;alternate&quot; type=&quot;square&quot;&gt;
	&lt;li&gt;Thread 1 enters _set() first, sets _curMaster = addr.&lt;/li&gt;
	&lt;li&gt;Thread 2 enters _set() next, sees _curMaster already == addr so exits early.&lt;/li&gt;
	&lt;li&gt;Thread 2 enters get(), assigns _pool to _masterPortPool (null) then calls _pool.get() and NPE&lt;/li&gt;
	&lt;li&gt;Thread 1 continues in _set(), sets _masterPortPool to addr&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Stack Trace (based on git head, commit a052b4f35af4069121cbf47adc88d6199563c4d4):&lt;/p&gt;

&lt;p&gt;java.lang.NullPointerException&lt;br/&gt;
	at com.mongodb.DBTCPConnector$MyPort.get(DBTCPConnector.java:296)&lt;br/&gt;
	at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:205)&lt;br/&gt;
	at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:271)&lt;br/&gt;
	at com.mongodb.DB.command(DB.java:154)&lt;br/&gt;
	at com.mongodb.DB.command(DB.java:139)&lt;br/&gt;
	at com.mongodb.DBCollection.drop(DBCollection.java:683)&lt;/p&gt;

&lt;p&gt;Suggested fix:&lt;/p&gt;

&lt;p&gt;    private boolean _set( ServerAddress addr )&lt;/p&gt;
{
        if ( _curMaster == addr &amp;amp;&amp;amp; _masterPortPool != null)
            return false;
        _curMaster = addr;
        _masterPortPool = _portHolder.get( addr );
        return true;
    }</description>
                <environment>Single MongoDB instance, Java 6 (OS X 10.6.6).</environment>
        <key id="14387">JAVA-263</key>
            <summary>NullPointerException due to race condition during concurrent access to DBTCPTransport</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</type>
                                            <priority id="2" iconUrl="https://jira.mongodb.org/images/icons/priorities/critical.svg">Critical - P2</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="antoine">Antoine Girbal</assignee>
                                    <reporter username="mikec">Mike Copley</reporter>
                        <labels>
                    </labels>
                <created>Tue, 1 Feb 2011 20:00:19 +0000</created>
                <updated>Thu, 17 Mar 2011 19:13:54 +0000</updated>
                            <resolved>Tue, 22 Feb 2011 19:56:19 +0000</resolved>
                                    <version>2.4</version>
                    <version>2.5</version>
                                    <fixVersion>2.5</fixVersion>
                                                        <votes>0</votes>
                                    <watches>1</watches>
                                                                                                                <comments>
                            <comment id="24450" author="antoine" created="Tue, 22 Feb 2011 19:56:19 +0000"  >&lt;p&gt;considering resolved until further report&lt;/p&gt;</comment>
                            <comment id="24104" author="antoine" created="Thu, 17 Feb 2011 07:15:40 +0000"  >&lt;p&gt;did some refactoring for dbtcpconnector.&lt;br/&gt;
Basically removed class variables that were redundant and could lead to inconsistent states.&lt;br/&gt;
The NPE should be gone.&lt;br/&gt;
The masterPortPool can only be null at startup, if it was never set.&lt;br/&gt;
Even then:&lt;/p&gt;
&lt;ul class=&quot;alternate&quot; type=&quot;square&quot;&gt;
	&lt;li&gt;for single server, it will be set to that server in constructor&lt;/li&gt;
	&lt;li&gt;for repl set, it will be probably set by the 1st operation that happens (call to checkmaster)&lt;/li&gt;
	&lt;li&gt;in case all servers of repl set are down from driver start up, it may remain null, and appropriate MongoException are thrown.&lt;/li&gt;
&lt;/ul&gt;
</comment>
                            <comment id="24103" author="auto" created="Thu, 17 Feb 2011 07:12:20 +0000"  >&lt;p&gt;Author:&lt;/p&gt;
{u&apos;login&apos;: u&apos;agirbal&apos;, u&apos;name&apos;: u&apos;agirbal&apos;, u&apos;email&apos;: u&apos;antoine@10gen.com&apos;}
&lt;p&gt;Message: &lt;a href=&quot;https://jira.mongodb.org/browse/JAVA-263&quot; title=&quot;NullPointerException due to race condition during concurrent access to DBTCPTransport&quot; class=&quot;issue-link&quot; data-issue-key=&quot;JAVA-263&quot;&gt;&lt;del&gt;JAVA-263&lt;/del&gt;&lt;/a&gt;: added NPE handling in rare edge case&lt;br/&gt;
&lt;a href=&quot;https://github.com/mongodb/mongo-java-driver/commit/403d28d87b78fe84e4945c221272d74b53e5f4f3&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-java-driver/commit/403d28d87b78fe84e4945c221272d74b53e5f4f3&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="24095" author="auto" created="Thu, 17 Feb 2011 04:49:24 +0000"  >&lt;p&gt;Author:&lt;/p&gt;
{u&apos;login&apos;: u&apos;agirbal&apos;, u&apos;name&apos;: u&apos;agirbal&apos;, u&apos;email&apos;: u&apos;antoine@10gen.com&apos;}
&lt;p&gt;Message: &lt;a href=&quot;https://jira.mongodb.org/browse/JAVA-263&quot; title=&quot;NullPointerException due to race condition during concurrent access to DBTCPTransport&quot; class=&quot;issue-link&quot; data-issue-key=&quot;JAVA-263&quot;&gt;&lt;del&gt;JAVA-263&lt;/del&gt;&lt;/a&gt;: NullPointerException due to race condition during concurrent access to DBTCPTransport, comprehensive refactoring&lt;br/&gt;
&lt;a href=&quot;https://github.com/mongodb/mongo-java-driver/commit/8a955e265e40160742c8782d5e8284d1c0bed7b0&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-java-driver/commit/8a955e265e40160742c8782d5e8284d1c0bed7b0&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="24064" author="antoine" created="Wed, 16 Feb 2011 22:33:18 +0000"  >&lt;p&gt;looking at it&lt;/p&gt;</comment>
                            <comment id="24056" author="mikec" created="Wed, 16 Feb 2011 22:16:50 +0000"  >&lt;p&gt;Sounds like this case needs to be reopened then. Perhaps a more comprehensive synchronized block is required for this task.&lt;/p&gt;</comment>
                            <comment id="23843" author="david_dawson" created="Mon, 14 Feb 2011 10:45:06 +0000"  >&lt;p&gt;We&apos;ve seen this error quite a bit.  We took 2.5 trunk (with this fix applied) and ran with that for a while to see what result we could get, however we are still seeing occasional NPE on this code (although much reduced)&lt;/p&gt;

&lt;p&gt;java.lang.NullPointerException&lt;br/&gt;
        at com.mongodb.DBTCPConnector$MyPort.get(DBTCPConnector.java:302)&lt;br/&gt;
        at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:206)&lt;br/&gt;
        at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:271)&lt;br/&gt;
        at com.mongodb.DBCursor._check(DBCursor.java:342)&lt;br/&gt;
        at com.mongodb.DBCursor._hasNext(DBCursor.java:472)&lt;br/&gt;
        at com.mongodb.DBCursor.hasNext(DBCursor.java:497)&lt;br/&gt;
        .......&lt;/p&gt;
</comment>
                            <comment id="23324" author="auto" created="Tue, 8 Feb 2011 00:34:21 +0000"  >&lt;p&gt;Author:&lt;/p&gt;
{u&apos;login&apos;: u&apos;agirbal&apos;, u&apos;name&apos;: u&apos;agirbal&apos;, u&apos;email&apos;: u&apos;antoine@10gen.com&apos;}
&lt;p&gt;Message: &lt;a href=&quot;https://jira.mongodb.org/browse/JAVA-263&quot; title=&quot;NullPointerException due to race condition during concurrent access to DBTCPTransport&quot; class=&quot;issue-link&quot; data-issue-key=&quot;JAVA-263&quot;&gt;&lt;del&gt;JAVA-263&lt;/del&gt;&lt;/a&gt;: didnt mean to make it synchronized&lt;br/&gt;
&lt;a href=&quot;https://github.com/mongodb/mongo-java-driver/commit/eb8d14e0ce975b1db2bb3742d1f25e1227d42825&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-java-driver/commit/eb8d14e0ce975b1db2bb3742d1f25e1227d42825&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="23323" author="antoine" created="Tue, 8 Feb 2011 00:22:29 +0000"  >&lt;p&gt;thanks for report, fixed.&lt;br/&gt;
There is still a race condition whereby _currentMaster and _masterPortPool may point to different servers from one thread&apos;s point of view.&lt;br/&gt;
But that should not be a problem and will become consistent quickly.&lt;/p&gt;</comment>
                            <comment id="23322" author="auto" created="Tue, 8 Feb 2011 00:20:38 +0000"  >&lt;p&gt;Author:&lt;/p&gt;
{u&apos;login&apos;: u&apos;agirbal&apos;, u&apos;name&apos;: u&apos;agirbal&apos;, u&apos;email&apos;: u&apos;antoine@10gen.com&apos;}
&lt;p&gt;Message: &lt;a href=&quot;https://jira.mongodb.org/browse/JAVA-263&quot; title=&quot;NullPointerException due to race condition during concurrent access to DBTCPTransport&quot; class=&quot;issue-link&quot; data-issue-key=&quot;JAVA-263&quot;&gt;&lt;del&gt;JAVA-263&lt;/del&gt;&lt;/a&gt;: NullPointerException due to race condition during concurrent access to DBTCPTransport&lt;br/&gt;
&lt;a href=&quot;https://github.com/mongodb/mongo-java-driver/commit/b0e163ea02057f994464eaec5c5873be99332597&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-java-driver/commit/b0e163ea02057f994464eaec5c5873be99332597&lt;/a&gt;&lt;/p&gt;</comment>
                    </comments>
                    <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                        <customfield id="customfield_10011" key="com.atlassian.jira.plugin.system.customfieldtypes:radiobuttons">
                        <customfieldname>Backwards Compatibility</customfieldname>
                        <customfieldvalues>
                                <customfieldvalue key="10038"><![CDATA[Fully Compatible]]></customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                <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|hrginz:</customfieldvalue>

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