[CSHARP-2011] ObjectDisposedException on new connection Created: 05/Jul/17  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: Connectivity
Affects Version/s: 2.4.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Aristarkh Zagorodnikov Assignee: Unassigned
Resolution: Unresolved Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

.NET Core 2.0.0-preview2
Linux x64


Issue Links:
Related
is related to CSHARP-1646 TcpStreamFactory.ConfigureConnectedSo... Closed

 Description   

Occasinally when querying the database, we get the following exception:

MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Sockets.Socket'.
   at System.Net.Sockets.Socket.SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, Int32 optionValue)
   at MongoDB.Driver.Core.Connections.TcpStreamFactory.ConfigureConnectedSocket(Socket socket)
   at MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateNetworkStream(Socket socket)
   at MongoDB.Driver.Core.Connections.TcpStreamFactory.<CreateStreamAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MongoDB.Driver.Core.Connections.BinaryConnection.<OpenHelperAsync>d__48.MoveNext()
   --- End of inner exception stack trace ---
   at MongoDB.Driver.Core.Connections.BinaryConnection.<OpenHelperAsync>d__48.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MongoDB.Driver.Core.Servers.Server.<GetChannelAsync>d__27.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MongoDB.Driver.Core.Operations.FindOperation`1.<ExecuteAsync>d__111.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MongoDB.Driver.GridFS.GridFSBucket`1.<GetFileInfoByNameAsync>d__70.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MongoDB.Driver.GridFS.GridFSBucket`1.<OpenDownloadStreamByNameAsync>d__29.MoveNext()

The strange part about this is that according to a stack trace it should be a newly created socket.
This might be a problem with low connection timeout (but I expect it to throw TimeoutException) and/or externally passed non-default cancellationToken, this looks like the only explanation how socket hapens to get disposed in TcpStreamFactory.ConnectAsync.

This also might be a bug in .NET Core 2.0-preview2 (especially since it's a preview), but I don't think so, the other network-related code in the application functions normally (though it doesn't rule this out completely).
In fact, if System.Threading.Timer had a bug that allowed it to fire after being disposed, this could lead to the described problem.



 Comments   
Comment by Oleg Temnov [ 20/Feb/18 ]

https://github.com/mongodb/mongo-csharp-driver/pull/315

Comment by Oleg Temnov [ 19/Feb/18 ]

I get this exception several times per day, connecting from Azure to Amazon

Comment by Robert Stam [ 11/Jan/18 ]

Looking at the Connect method in TcpStreamFactory:

https://github.com/mongodb/mongo-csharp-driver/blob/v2.5.0/src/MongoDB.Driver.Core/Core/Connections/TcpStreamFactory.cs#L125

There would appear to be some race conditions that would allow the method to return without throwing an exception and yet the socket could be Disposed.

That could explain the ObjectDisposedException shortly thereafter.

Comment by Aristarkh Zagorodnikov [ 05/Jul/17 ]

Connection string: mongodb://127.0.0.1/a?uuidRepresentation=Standard;maxPoolSize=300;socketTimeout=00:00:06;connectTimeout=00:00:01.500;serverSelectionTimeout=00:00:03
The code is roughly as follows:

var url = new MongoUrl(connectionString);
var client = new MongoClient(url);
var database = client.GetDatabase(url.DatabaseName);
var bucket = new GridFSBucket<BsonValue>(database.WithReadPreference(ReadPreference.Nearest));
...
using (var stream = await bucket.OpenDownloadStreamByNameAsync(filename, null, cancellationToken).ConfigureAwait(false))
{
...
}

The cancellationToken is a Kestrel RequestAborted one.

Comment by Craig Wilson [ 05/Jul/17 ]

Thanks Aristarkh,

Yeah, this is a bit weird, since the stack trace shows a brand new socket.

1. What are you doing? Just a normal query?
2. Could you provide any connection information that is different? Like setting a socket timeout?

Craig

Generated at Wed Feb 07 21:41:22 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.