Still got "Thread static buffer is already in use" after v2.18

Hey MongoDB Team.

I’m getting an issues on the production environment about the following:

System.InvalidOperationException: Thread static buffer is already in use. at MongoDB.Bson.IO.ThreadStaticBuffer.RentBuffer(Int32 size) at MongoDB.Bson.IO.EncodingHelper.GetBytesUsingThreadStaticBuffer(Encoding encoding, String value).

This exception came from different places:
MongoDB.Bson.IO.ByteBufferStream.WriteString(String value, UTF8Encoding encoding)
MongoDB.Bson.IO.BsonTrie1.Add(String elementName, TValue value) MongoDB.Bson.IO.BsonTrie1.TryGetValue(String elementName, TValue& value)

Whenever my web app gets that exception then the thread can not process anymore until I restart my web app.

I have a look at the source
mongo-csharp-driver/src/MongoDB.Bson/IO/EncodingHelper.cs at master · mongodb/mongo-csharp-driver (github.com)
and look like the exception came from this

Could you give me some insight about what might be causing the issue?
I’m using MongoDB Driver 2.18 and my web app still had a lot of RAM when I got that issue.

Thanks in advance.

Hi, @Quan_Tran,

Welcome to the MongoDB Community Forums. It appears that you have encountered CSHARP-4853 whereby attempting to decode invalid unicode results in the thread static buffer not being returned correctly. This issue was fixed in MongoDB .NET/C# Driver 2.23.0. Please try upgrading to 2.23.0 or newer.

Sincerely,
James

2 Likes