<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Wed Feb 07 21:49:34 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-4853] &quot;Thread static buffer is already in use.&quot; error after bad utf8 string write.</title>
                <link>https://jira.mongodb.org/browse/CSHARP-4853</link>
                <project id="10041" key="CSHARP">C# Driver</project>
                    <description>&lt;h4&gt;&lt;a name=&quot;Summary&quot;&gt;&lt;/a&gt;Summary&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;After an error with invalid unicode strings the thread is unable to perform operations on MongoDB.&lt;/em&gt;&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;MongoDB C# 2.10&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;Will PR on github.&lt;/p&gt;

&lt;p&gt;&#160; &lt;span class=&quot;error&quot;&gt;&amp;#91;Fact&amp;#93;&lt;/span&gt;&lt;br/&gt;
&#160; public void GetBytesUsingThreadStaticBuffer_should_leave_the_thread_operational()&lt;br/&gt;
&#160; &lt;/p&gt;
{
&#160; &#160; &#160; var invalidUtf8String = &quot;&#128293;&quot;.Substring(0, 1);
&#160; &#160; &#160; var exception = Record.Exception(() =&amp;gt; EncodingHelper.GetBytesUsingThreadStaticBuffer(Utf8Encodings.Strict, invalidUtf8String));
&#160; &#160; &#160; var e = exception.Should().BeOfType&amp;lt;EncoderFallbackException&amp;gt;().Subject;

&#160; &#160; &#160; using var segment = EncodingHelper.GetBytesUsingThreadStaticBuffer(Utf8Encodings.Strict, &quot;Thread static buffer should not be in use&quot;);
&#160; &#160; &#160; segment.Segment.Array.Length.Should().Be(256);
&#160; }
&lt;h4&gt;&lt;a name=&quot;AdditionalBackground&quot;&gt;&lt;/a&gt;Additional Background&lt;/h4&gt;
&lt;ol&gt;
	&lt;li&gt;Call to rent buffer with invalid utf8 string&lt;/li&gt;
	&lt;li&gt;encoding.GetBytes throws (using strict encoder)&lt;/li&gt;
	&lt;li&gt;disposable is not created, thread static attributes holing buffer&lt;/li&gt;
	&lt;li&gt;every subsequent call in this thread throws&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;&lt;span class=&quot;image-wrap&quot; style=&quot;&quot;&gt;&lt;img src=&quot;https://jira.mongodb.org/secure/attachment/491477/491477_image-2023-11-21-16-00-07-204.png&quot; style=&quot;border: 0px solid black&quot; /&gt;&lt;/span&gt;&lt;/p&gt;</description>
                <environment></environment>
        <key id="2506341">CSHARP-4853</key>
            <summary>&quot;Thread static buffer is already in use.&quot; error after bad utf8 string write.</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</type>
                                            <priority id="10300" iconUrl="https://jira.mongodb.org/images/icons/priorities/medium.svg">Unknown</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="13201">Fixed</resolution>
                                        <assignee username="adelin.mbidaowona@mongodb.com">Adelin Mbida Owona</assignee>
                                    <reporter username="mtb.snowboard@gmail.com">Andrea Balducci</reporter>
                        <labels>
                    </labels>
                <created>Tue, 21 Nov 2023 15:03:32 +0000</created>
                <updated>Mon, 4 Dec 2023 17:20:38 +0000</updated>
                            <resolved>Wed, 29 Nov 2023 16:37:25 +0000</resolved>
                                                    <fixVersion>2.23.0</fixVersion>
                                                        <votes>0</votes>
                                    <watches>3</watches>
                                                                                                                <comments>
                            <comment id="5912889" author="xgen-internal-githook" created="Wed, 29 Nov 2023 16:31:34 +0000"  >&lt;p&gt;Author: &lt;/p&gt;
{&apos;name&apos;: &apos;Adelin Owona&apos;, &apos;email&apos;: &apos;51498470+adelinowona@users.noreply.github.com&apos;, &apos;username&apos;: &apos;adelinowona&apos;}
&lt;p&gt;Message: &lt;a href=&quot;https://jira.mongodb.org/browse/CSHARP-4853&quot; title=&quot;&amp;quot;Thread static buffer is already in use.&amp;quot; error after bad utf8 string write.&quot; class=&quot;issue-link&quot; data-issue-key=&quot;CSHARP-4853&quot;&gt;&lt;del&gt;CSHARP-4853&lt;/del&gt;&lt;/a&gt;: &quot;Thread static buffer is already in use.&quot; error after bad utf8 string write. (#1219)&lt;br/&gt;
Branch: master&lt;br/&gt;
&lt;a href=&quot;https://github.com/mongodb/mongo-csharp-driver/commit/225ac8b7dc9042268f37e061990a26d9c3285b11&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-csharp-driver/commit/225ac8b7dc9042268f37e061990a26d9c3285b11&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="5896028" author="JIRAUSER1269368" created="Tue, 21 Nov 2023 16:49:46 +0000"  >&lt;p&gt;Actually the version where we have found the bug is the 2.21.0, but we are able to reproduce the very same behaviour on the latest version. The problem arise because we saved in a string property of an object an invalid unicode sequence, because .NET substring operator does not takes into account surrogate characters.&#160;&lt;/p&gt;

&lt;p&gt;The problem is the ThreadStaticBuffer class, it is really a danger, because if some code ask for RentBuffer and does not dispose the returned object, the entire thread is corrupted. This forces to completely restart the process. We luckly got this in a demo server and not in a customer production server, but the logic is really too dangerous to be left in production.&lt;/p&gt;

&lt;p&gt;What this pieces of code does is, you got a RentedBuffer, if you forget to dispose, every query that pass in this thread will be doomed to fail. This problem was already signaled by &lt;a href=&quot;https://jira.mongodb.org/browse/CSHARP-4159&quot; class=&quot;external-link&quot; rel=&quot;nofollow&quot;&gt;https://jira.mongodb.org/browse/CSHARP-4159&lt;/a&gt; .&#160;&lt;/p&gt;

&lt;p&gt;In my opinion the RentBuffer function must be changed, because diagnosing such error in a live server is a living hell, because you start having random failure in your access to the database.&#160;&lt;/p&gt;

&lt;p&gt;In my opinion if the __isBufferRented variable is true, it means that some code in this thread did not disposed the RentedBuffer, it is enough to log an error, and to cleanup __isBufferRented and put the __buffer to null so the routine will create a new buffer.&lt;/p&gt;


&lt;p&gt;&lt;span class=&quot;image-wrap&quot; style=&quot;&quot;&gt;&lt;img src=&quot;https://jira.mongodb.org/secure/attachment/491537/491537_image-2023-11-21-17-44-10-396.png&quot; style=&quot;border: 0px solid black&quot; /&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Also the class has this comment&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;tt&gt;&#160; &#160; /// &amp;lt;summary&amp;gt;&lt;/tt&gt;&lt;br/&gt;
&lt;tt&gt;&#160; &#160; /// Represents a class that provides reusable buffer per thread.&lt;/tt&gt;&lt;br/&gt;
&lt;tt&gt;&#160; &#160; /// Use this technique ONLY when:&lt;/tt&gt;&lt;br/&gt;
&lt;tt&gt;&#160; &#160; /// &#160; &#160; 1. Buffer is not shared across multiple threads.&lt;/tt&gt;&lt;br/&gt;
&lt;tt&gt;&#160; &#160; /// &#160; &#160; 2. No nested methods invocations use the same buffer.&lt;/tt&gt;&lt;br/&gt;
&lt;tt&gt;&#160; &#160; /// Advised to limit the usage scope to a single method.&lt;/tt&gt;&lt;br/&gt;
&lt;tt&gt;&#160; &#160; /// &amp;lt;/summary&amp;gt;&lt;/tt&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;&#160;&lt;/p&gt;

&lt;p&gt;While the code in GetBytesUsingThreadStaticBuffer completely violates rule 1, it ask for the buffer, it DOES NOT WRAP in using and then pass inside another IDisposable, letting some code to be execute completely violating the contract, where any error will render the whole process unusable. A complete disaster in production environment.&lt;/p&gt;

&lt;p&gt;Gian Maria.&lt;/p&gt;

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

&lt;p&gt;&#160;&lt;/p&gt;</comment>
                            <comment id="5895917" author="mtb.snowboard@gmail.com" created="Tue, 21 Nov 2023 16:29:14 +0000"  >&lt;p&gt;PR &lt;a href=&quot;https://github.com/mongodb/mongo-csharp-driver/pull/1217&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;https://github.com/mongodb/mongo-csharp-driver/pull/1217&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="5895516" author="dbeng-pm-bot" created="Tue, 21 Nov 2023 15:03:34 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=mtb.snowboard%40gmail.com&quot; class=&quot;user-hover&quot; rel=&quot;mtb.snowboard@gmail.com&quot;&gt;mtb.snowboard@gmail.com&lt;/a&gt;, thank you for reporting this issue! The team will look into it and get back to you soon. &lt;/p&gt;</comment>
                    </comments>
                <issuelinks>
                            <issuelinktype id="10010">
                    <name>Duplicate</name>
                                                                <inwardlinks description="is duplicated by">
                                        <issuelink>
            <issuekey id="2507154">CSHARP-4855</issuekey>
        </issuelink>
                            </inwardlinks>
                                    </issuelinktype>
                    </issuelinks>
                <attachments>
                            <attachment id="491477" name="image-2023-11-21-16-00-07-204.png" size="59779" author="mtb.snowboard@gmail.com" created="Tue, 21 Nov 2023 15:00:08 +0000"/>
                            <attachment id="491537" name="image-2023-11-21-17-44-10-396.png" size="40407" author="alkampfer@nablasoft.com" created="Tue, 21 Nov 2023 16:44:08 +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_10257" key="com.atlassian.jira.plugin.system.customfieldtypes:radiobuttons">
                        <customfieldname>Documentation Changes</customfieldname>
                        <customfieldvalues>
                                <customfieldvalue key="11861"><![CDATA[Not Needed]]></customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_14266" key="com.atlassian.jira.plugin.system.customfieldtypes:textarea">
                        <customfieldname>Documentation Changes Summary</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>&lt;p&gt;1. What would you like to communicate to the user about this feature?&lt;br/&gt;
2. Would you like the user to see examples of the syntax and/or executable code and its output?&lt;br/&gt;
3. Which versions of the driver/connector does this apply to?&lt;/p&gt;</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|i2j9zw:</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>