<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Wed Feb 07 21:35:57 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-154] MongoConnection.Close() and Dispose</title>
                <link>https://jira.mongodb.org/browse/CSHARP-154</link>
                <project id="10041" key="CSHARP">C# Driver</project>
                    <description>&lt;p&gt;MongoConnection.tcpClient might be not connected in MongoConnection.Close() leading to a handled InvalidOperationException (I encountered this when simulating reading from a replica set, shutting down primary member in the process). While this does not affect functionality, debugging (especially online) and performance suffers from extra exceptions.&lt;br/&gt;
Looks like TcpClient.GetNetworkStream() throws an exception instead of returning null if it&apos;s not connected, so checking TcpClient.Connected before trying to get the stream (actually around entire first try/catch block in MongoConnection.Close()) would be nice to prevent that extra exception.&lt;/p&gt;

&lt;p&gt;I also don&apos;t think that casting the TcpClient to IDisposable and calling Dispose(), along with closing the stream explicitly is necessary, because TcpClient.Close() calls Dispose() internally.&lt;br/&gt;
I&apos;ve seen the following: &quot;// note: TcpClient.Close doesn&apos;t close the NetworkStream!?&quot; &amp;#8211; this does not seem to be the case.&lt;/p&gt;

&lt;p&gt;The following is partial output from Red Gate Reflector utility on certainl BCL methods from .NET 4:&lt;/p&gt;

&lt;p&gt;public void TcpClient.Close()&lt;br/&gt;
{&lt;br/&gt;
    if (Logging.On)&lt;/p&gt;
    {
        Logging.Enter(Logging.Sockets, this, &quot;Close&quot;, &quot;&quot;);
    }
&lt;p&gt;    ((IDisposable) this).Dispose();&lt;br/&gt;
    if (Logging.On)&lt;/p&gt;
    {
        Logging.Exit(Logging.Sockets, this, &quot;Close&quot;, &quot;&quot;);
    }
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;void TcpClient.IDisposable.Dispose()&lt;br/&gt;
{&lt;br/&gt;
    this.Dispose(true);&lt;br/&gt;
}&lt;/p&gt;

&lt;p&gt;protected virtual void TcpClient.Dispose(bool disposing)&lt;br/&gt;
{&lt;br/&gt;
    if (Logging.On)&lt;/p&gt;
    {
        Logging.Enter(Logging.Sockets, this, &quot;Dispose&quot;, &quot;&quot;);
    }
&lt;p&gt;    if (this.m_CleanedUp)&lt;br/&gt;
    {&lt;br/&gt;
        if (Logging.On)&lt;/p&gt;
        {
            Logging.Exit(Logging.Sockets, this, &quot;Dispose&quot;, &quot;&quot;);
        }&lt;br/&gt;
    }&lt;br/&gt;
    else&lt;br/&gt;
    {&lt;br/&gt;
        if (disposing)&lt;br/&gt;
        {&lt;br/&gt;
            IDisposable dataStream = this.m_DataStream;&lt;br/&gt;
            if (dataStream != null)&lt;br/&gt;
            {
                dataStream.Dispose();
            }&lt;br/&gt;
            else&lt;br/&gt;
            {&lt;br/&gt;
                Socket client = this.Client;&lt;br/&gt;
                if (client != null)&lt;br/&gt;
                {&lt;br/&gt;
                    try&lt;br/&gt;
                    {
                        client.InternalShutdown(SocketShutdown.Both);
                    }&lt;br/&gt;
                    finally&lt;br/&gt;
                    {
                        client.Close();
                        this.Client = null;
                    }&lt;br/&gt;
                }&lt;br/&gt;
            }&lt;br/&gt;
            GC.SuppressFinalize(this);&lt;br/&gt;
        }&lt;br/&gt;
        this.m_CleanedUp = true;&lt;br/&gt;
        if (Logging.On)&lt;br/&gt;
        {            Logging.Exit(Logging.Sockets, this, &quot;Dispose&quot;, &quot;&quot;);        }
&lt;p&gt;    }&lt;br/&gt;
}&lt;/p&gt;

&lt;p&gt;public void Stream.Dispose()&lt;br/&gt;
{&lt;br/&gt;
    this.Close();&lt;br/&gt;
}&lt;/p&gt;

&lt;p&gt;public virtual void Stream.Close()&lt;br/&gt;
{&lt;br/&gt;
    this.Dispose(true);&lt;br/&gt;
    GC.SuppressFinalize(this);&lt;br/&gt;
}&lt;/p&gt;

&lt;p&gt;Neither Stream.Close() nor Stream.Dispose() are overriden in NetworkStream (only Dispose(bool) does, but it doesn&apos;t matter).&lt;/p&gt;

&lt;p&gt;This is also related to &lt;a href=&quot;http://jira.mongodb.org/browse/CSHARP-150&quot; class=&quot;external-link&quot; rel=&quot;nofollow&quot;&gt;http://jira.mongodb.org/browse/CSHARP-150&lt;/a&gt;&lt;/p&gt;
</description>
                <environment></environment>
        <key id="14265">CSHARP-154</key>
            <summary>MongoConnection.Close() and Dispose</summary>
                <type id="4" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14710&amp;avatarType=issuetype">Improvement</type>
                                            <priority id="4" iconUrl="https://jira.mongodb.org/images/icons/priorities/minor.svg">Minor - P4</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>Thu, 20 Jan 2011 13:21:18 +0000</created>
                <updated>Thu, 2 Apr 2015 18:27:31 +0000</updated>
                            <resolved>Tue, 15 Mar 2011 22:35:59 +0000</resolved>
                                    <version>1.0</version>
                                    <fixVersion>1.0</fixVersion>
                                                        <votes>0</votes>
                                    <watches>0</watches>
                                                                                                                <comments>
                            <comment id="25967" author="rstam" created="Tue, 15 Mar 2011 22:35:59 +0000"  >&lt;p&gt;Simplified implementation of MongoConnection.Close based on the information provided. Thanks for the detailed information!&lt;/p&gt;</comment>
                    </comments>
                    <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|hrh9dr:</customfieldvalue>

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