[CSHARP-1646] TcpStreamFactory.ConfigureConnectedSocket Exception Created: 05/May/16  Updated: 08/Feb/23  Resolved: 11/Jan/18

Status: Closed
Project: C# Driver
Component/s: Connectivity
Affects Version/s: 2.2.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: slolitor Assignee: Robert Stam
Resolution: Cannot Reproduce Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

windows server 2008r2


Issue Links:
Related
related to CSHARP-2011 ObjectDisposedException on new connec... Backlog

 Description   

the stack trace is :
System.AggregateException: One or more errors occurred. ---> MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.Net.Sockets.SocketException: 提供了一个无效的参数。 at System.Net.Sockets.Socket.SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, Int32 optionValue, Boolean silent) at System.Net.Sockets.Socket.SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, Int32 optionValue) at MongoDB.Driver.Core.Connections.TcpStreamFactory.ConfigureConnectedSocket(Socket socket) in D:\jenkins\workspace\mongo-csharp-driver-2.2.x-build\src\MongoDB.Driver.Core\Core\Connections\TcpStreamFactory.cs:line 65 at MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateNetworkStream(Socket socket) in D:\jenkins\workspace\mongo-csharp-driver-2.2.x-build\src\MongoDB.Driver.Core\Core\Connections\TcpStreamFactory.cs:line 171 at MongoDB.Driver.Core.Connections.TcpStreamFactory.d_4.MoveNext() in D:\jenkins\workspace\mongo-csharp-driver-2.2.x-build\src\MongoDB.Driver.Core\Core\Connections\TcpStreamFactory.cs:line 58 — End of stack trace from previous location where exception was thrown — at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() at MongoDB.Driver.Core.Connections.BinaryConnection.d47.MoveNext() in D:\jenkins\workspace\mongo-csharp-driver-2.2.x-build\src\MongoDB.Driver.Core\Core\Connections\BinaryConnection.cs:line 285 — End of inner exception stack trace — at MongoDB.Driver.Core.Connections.BinaryConnection.d47.MoveNext() in D:\jenkins\workspace\mongo-csharp-driver-2.2.x-build\src\MongoDB.Driver.Core\Core\Connections\BinaryConnection.cs:line 296 — End of stack trace from previous location where exception was thrown — at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at MongoDB.Driver.Core.Servers.ClusterableServer.d40.MoveNext() in D:\jenkins\workspace\mongo-csharp-driver-2.2.x-build\src\MongoDB.Driver.Core\Core\Servers\ClusterableServer.cs:line 242 — End of stack trace from previous location where exception was thrown — at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult() at MongoDB.Driver.Core.Operations.BulkMixedWriteOperation.d39.MoveNext() in D:\jenkins\workspace\mongo-csharp-driver-2.2.x-build\src\MongoDB.Driver.Core\Core\Operations\BulkMixedWriteOperation.cs:line 204 — End of stack trace from previous location where exception was thrown — at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at MongoDB.Driver.OperationExecutor.d3`1.MoveNext() in D:\jenkins\workspace\mongo-csharp-driver-2.2.x-build\src\MongoDB.Driver\OperationExecutor.cs:line 46 — End of stack trace from previous location where exception was thrown — at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at MongoDB.Driver.MongoCollectionImpl`1.d61`1.MoveNext() in D:\jenkins\workspace\mongo-csharp-driver-2.2.x-build\src\MongoDB.Driver\MongoCollectionImpl.cs:line 759 — End of stack trace from previous location where exception was thrown — at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at MongoDB.Driver.MongoCollectionImpl`1.d22.MoveNext() in D:\jenkins\workspace\mongo-csharp-driver-2.2.x-build\src\MongoDB.Driver\MongoCollectionImpl.cs:line 180 — End of stack trace from previous location where exception was thrown — at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at MongoDB.Driver.MongoCollectionBase`1.d39.MoveNext() in D:\jenkins\workspace\mongo-csharp-driver-2.2.x-build\src\MongoDB.Driver\MongoCollectionBase.cs:line 323 — End of stack trace from previous location where exception was thrown — at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Framework.Zhaogang.Job.Infrastructure.Data.MongoDB.Dao`1.d_a.MoveNext() in d:\VS Projects\git\JobCenter2\jobcenter\Framework.Zhaogang.Job.Infrastructure\Data\MongoDB\Dao.cs:line 137 — End of inner exception stack trace



 Comments   
Comment by Robert Stam [ 11/Jan/18 ]

This could be related to CSHARP-2011, in the sense that Connect might be returning without throwing an exception and yet the socket might end up in an invalid state (which could explain why subsequently attempting to set the ReceiveBufferSize fails).

Comment by Wouter De Keersmaecker [ 19/Sep/16 ]

I have encountered the same exception multiple times. My application runs pretty much all day, but occasionally it crashes due to this error.
The database is run on a server in the same local network as the client machine where the error occurs.

Platform: Windows 7 64bit
C# driver version: 2.2.4.26
.NET version: 4.6
MongoDB server version: 3.2.9

I use the following MongoClientSettings:

MongoClientSettings settings = new MongoClientSettings
            {
                Server = new MongoServerAddress(hostAddress),
                Credentials = new[] { credential },
                MaxConnectionIdleTime = TimeSpan.FromSeconds(1),
                ClusterConfigurator = builder => { 
                    builder.ConfigureCluster(clusterSettings => 
                        clusterSettings.With(serverSelectionTimeout: TimeSpan.FromMilliseconds(5000))
                    );
                }
            };

Perhaps the low MaxConnectionIdleTime is causing this?

Stacktrace:

MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.Net.Sockets.SocketException: An invalid argument was supplied
at System.Net.Sockets.Socket.SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, Int32 optionValue, Boolean silent)
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.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MongoDB.Driver.Core.Connections.BinaryConnection.<OpenHelperAsync>d__47.MoveNext()
— End of inner exception stack trace —
at MongoDB.Driver.Core.Connections.BinaryConnection.<OpenHelperAsync>d__47.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MongoDB.Driver.Core.Servers.ClusterableServer.<GetChannelAsync>d__40.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MongoDB.Driver.Core.Operations.FindOperation`1.<ExecuteAsync>d__107.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MongoDB.Driver.OperationExecutor.<ExecuteReadOperationAsync>d__1`1.MoveNext()
— End of stack trace from previous location where exception was thrown —
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MongoDB.Driver.MongoCollectionImpl`1.<ExecuteReadOperationAsync>d__59`1.MoveNext()

EDIT 2017:
I'm still getting this exception.

Comment by Craig Wilson [ 06/May/16 ]

I haven't heard of this before, particularly related to the driver. I'll need to do some research, but it feels that, other than setting the value, the driver isn't responsible here.

Comment by slolitor [ 06/May/16 ]

HI Craig Wilson :
it happens serveral times per day.and i dont change anything.
my app is work on .net framework 4.5.1 and it running on Windows Server 2008 R2. (i dont change any thing ,just use as samples )
i use .net reflector and find it happens when set

socket.ReceiveBufferSize = _settings.ReceiveBufferSize;

go on work and you will find it throws from here:

internal void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue, bool silent)
{
if (!silent || (!this.CleanedUp && !this.m_Handle.IsInvalid))
{
SocketError success = SocketError.Success;
try

{ success = UnsafeNclNativeMethods.OSSOCK.setsockopt(this.m_Handle, optionLevel, optionName, ref optionValue, 4); }

catch
{
if (!silent || !this.m_Handle.IsInvalid)

{ throw; }

return;
}
if (((optionName == SocketOptionName.PacketInformation) && (optionValue == 0)) && (success == SocketError.Success))

{ this.m_ReceivingPacketInformation = false; }

if (!silent && (success == SocketError.SocketError))
{
SocketException socketException = new SocketException();
this.UpdateStatusAfterSocketError(socketException);
if (s_LoggingEnabled)

{ Logging.Exception(Logging.Sockets, this, "SetSocketOption", socketException); }

throw socketException;
}
}
}

so finalily it turns out this method
[DllImport("ws2_32.dll", SetLastError=true)]
internal static extern SocketError setsockopt([In] SafeCloseSocket socketHandle, [In] SocketOptionLevel optionLevel, [In] SocketOptionName optionName, [In] ref int optionValue, [In] int optionLength);

set socket options failed.so a socket exception throw out.

it is very strange ,is the network unstabilize would cause this problem?

Comment by Craig Wilson [ 05/May/16 ]

Hi slolitor,

The translation simply says an invalid parameter was supplied. Here is the code line this points to; the ReceiveBufferSize. Our default is 65536.

1. Does this always happen, or is it sporadic?
2. Are you changing the default value to something else?
3. What version of the .NET Framework are you using?
4. Please confirm you are using Windows Server 2008 R2.
5. Is it possible you've changed the maximum/minimum values for SO_RCVBUF and SO_SNDBUF elsewhere in the operation system?

Thanks,
Craig

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