<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Thu Feb 08 09:02:11 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-4477] MongoChangeStreamCursor tryNext() call blocks and waits indefinitely when there is a network connection loss</title>
                <link>https://jira.mongodb.org/browse/JAVA-4477</link>
                <project id="10006" key="JAVA">Java Driver</project>
                    <description>&lt;h4&gt;&lt;a name=&quot;Summary&quot;&gt;&lt;/a&gt;Summary&lt;/h4&gt;

&lt;p&gt;MongoChangeStreamCursor tryNext() call blocks and waits indefinitely when there is a network connection loss. &lt;/p&gt;

&lt;p&gt;My code looks something like this:&lt;/p&gt;


&lt;p&gt;&lt;tt&gt;MongoChangeStreamCursor&amp;lt;ChangeStreamDocument&amp;lt;Document&amp;gt;&amp;gt; cursor = collection.watch(pipeline).fullDocument(FullDocument.UPDATE_LOOKUP).cursor();&lt;/tt&gt;&lt;br/&gt;
&lt;tt&gt;ChangeStreamDocument&amp;lt;Document&amp;gt; changeStreamDocument = cursor.tryNext();&lt;/tt&gt;&lt;br/&gt;
{{if (changeStreamDocument != null) { }}&lt;br/&gt;
&lt;tt&gt;&#160; &#160; &#160;// do something&lt;/tt&gt;&lt;br/&gt;
&lt;tt&gt;}&lt;/tt&gt;&lt;/p&gt;

&lt;p&gt;The purpose of using the tryNext (instead of hasNext and next) is because of its non-blocking nature.&lt;br/&gt;
But in case of a network connection loss, the tryNext call blocks the thread and waits indefinitely without throwing any exception.&lt;/p&gt;

&lt;p&gt;Although the MongoClient was created with these options i.e. &amp;amp;connectTimeoutMS=10000&amp;amp;serverSelectionTimeoutMS=10000, the tryNext call gets stuck even after surpassing the configured timeouts.&lt;/p&gt;

&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Expected behavior&lt;/b&gt;&lt;br/&gt;
tryNext method is expected to be a non blocking call.&lt;/p&gt;

&lt;p&gt;An appropriate MongoException should have been thrown immediately or as soon as the waiting time crosses the configured timeout period.&lt;/p&gt;
&lt;h4&gt;&lt;a name=&quot;Pleaseprovidetheversionofthedriver.Ifapplicable%2CpleaseprovidetheMongoDBserverversionandtopology%28standalone%2Creplicaset%2Corshardedcluster%29.&quot;&gt;&lt;/a&gt;Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).&lt;/h4&gt;

&lt;p&gt;The driver version is mongo-java-driver version 3.12.10&lt;br/&gt;
But the issue could be reproduced in java driver version 3.11 also.&lt;/p&gt;

&lt;p&gt;The database to which I have connected is Mongo DB Atlas.&lt;/p&gt;
&lt;h4&gt;&lt;a name=&quot;HowtoReproduce&quot;&gt;&lt;/a&gt;How to Reproduce&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;Steps to reproduce. If possible, please include a &lt;a href=&quot;http://sscce.org/&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;Short, Self Contained, Correct (Compilable), Example&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Write a simple MongoChangeStream cursor program and call to its tryNext() method and simulate a network connection loss.&lt;/p&gt;

&lt;p&gt;This use case can be easily tested by turning off the Wifi of the computer or the router.&lt;br/&gt;
The thread dump captured using JStack is also attached.&lt;/p&gt;
&lt;h4&gt;&lt;a name=&quot;AdditionalBackground&quot;&gt;&lt;/a&gt;Additional Background&lt;/h4&gt;

&lt;p&gt;This use case can be easily tested by turning off the Wifi of the computer or the router.&lt;br/&gt;
The thread dump captured using JStack is also attached.&lt;br/&gt;
The database to which I have connected is Mongo DB Atlas. The driver version is mongo-java-driver version 3.12.10&lt;/p&gt;

&lt;p&gt;The thread dump captured using JStack is also attached.&lt;/p&gt;</description>
                <environment></environment>
        <key id="1978261">JAVA-4477</key>
            <summary>MongoChangeStreamCursor tryNext() call blocks and waits indefinitely when there is a network connection loss</summary>
                <type id="6" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14720&amp;avatarType=issuetype">Question</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="13202">Works as Designed</resolution>
                                        <assignee username="jeff.yemin@mongodb.com">Jeffrey Yemin</assignee>
                                    <reporter username="ajay2589@gmail.com">Ajay Mathias</reporter>
                        <labels>
                            <label>changestreams</label>
                            <label>external-user</label>
                            <label>trynext</label>
                    </labels>
                <created>Mon, 7 Feb 2022 09:52:00 +0000</created>
                <updated>Fri, 27 Oct 2023 13:20:54 +0000</updated>
                            <resolved>Tue, 8 Feb 2022 12:20:47 +0000</resolved>
                                                                    <component>Change Streams</component>
                                        <votes>1</votes>
                                    <watches>3</watches>
                                                                                                                <comments>
                            <comment id="4341688" author="jeff.yemin" created="Tue, 8 Feb 2022 12:20:47 +0000"  >&lt;p&gt;Glad to hear you got it working as you expected.&lt;/p&gt;</comment>
                            <comment id="4341169" author="JIRAUSER1269349" created="Tue, 8 Feb 2022 03:16:45 +0000"  >&lt;p&gt;Update - Setting the&#160;serverSelectionTimeoutMS worked.&lt;/p&gt;

&lt;p&gt;Thanks&lt;/p&gt;</comment>
                            <comment id="4341153" author="JIRAUSER1269349" created="Tue, 8 Feb 2022 02:59:01 +0000"  >&lt;p&gt;Hi&#160;@Jeffrey Yemin&lt;/p&gt;

&lt;p&gt;Many thanks. That worked.&lt;br/&gt;
 I had set the&#160;&lt;tt&gt;socketTimeoutMS=10000&lt;/tt&gt; in the client, but the exception was thrown after 30 seconds.&lt;/p&gt;

&lt;p&gt;{{Exception in thread &quot;main&quot; com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector&lt;/p&gt;

{readPreference=primary}

&lt;p&gt;}}&lt;/p&gt;

&lt;p&gt;Is there a way to override this and still throw the exception after 10 seconds even if the client is connected to the primary?&lt;/p&gt;

&lt;p&gt;&lt;tt&gt;&#160;&lt;/tt&gt;&lt;/p&gt;</comment>
                            <comment id="4339011" author="jeff.yemin" created="Mon, 7 Feb 2022 13:54:36 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=ajay2589%40gmail.com&quot; class=&quot;user-hover&quot; rel=&quot;ajay2589@gmail.com&quot;&gt;ajay2589@gmail.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Can you try again after adding &lt;tt&gt;socketTimeoutMS=10000&lt;/tt&gt; to the connection string?  Socket timeout is what ensures that a socket read will time out, not connect timeout.&lt;/p&gt;</comment>
                    </comments>
                    <attachments>
                            <attachment id="358953" name="AltasTryNextCallDump.txt" size="11964" author="JIRAUSER1261666" created="Mon, 7 Feb 2022 09:50:38 +0000"/>
                    </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|i02e9z:</customfieldvalue>

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