SharpCifs.Std icon indicating copy to clipboard operation
SharpCifs.Std copied to clipboard

Allow SocketException in socket.Endpoint

Open reb3lzrr opened this issue 4 years ago • 6 comments

Problem

We've only recently been using the software and this problem popped up multiple times. It appears that an exception occurs while trying to rethrow and handle another exception. This causes the sockets to not be cleaned up properly and (in our case) Tasks to run indefinitely.

Chosen solution

This PR allows the Socket.RemoteEndPoint and Socket.LocalEndPoint properties to throw a SocketException . I've chosen this solution as it appears that both the local and remote variable were allowed to be null

Background Info

Below is the stacktrace of the situation as described

System.Net.Sockets.SocketException (107): Transport endpoint is not connected
   at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
   at System.Net.Sockets.Socket.get_RemoteEndPoint()
   at SharpCifs.Smb.SmbTransport.Connect()
   at SharpCifs.Smb.SmbTree.TreeConnect(ServerMessageBlock andx, ServerMessageBlock andxResponse)
   at SharpCifs.Smb.SmbFile.DoConnect()
   at SharpCifs.Smb.SmbFile.Connect()
   at SharpCifs.Smb.SmbFile.Connaect0()
   at SharpCifs.Smb.SmbFile.ResolveDfs(ServerMessageBlock request)
   at SharpCifs.Smb.SmbFile.Send(ServerMessageBlock request, ServerMessageBlock response)
   at SharpCifs.Smb.SmbFile.DoFindFirstNext(List`1 list, Boolean files, String wildcard, Int32 searchAttributes, ISmbFilenameFilter fnf, ISmbFileFilter ff)
   at SharpCifs.Smb.SmbFile.DoEnum(List`1 list, Boolean files, String wildcard, Int32 searchAttributes, ISmbFilenameFilter fnf, ISmbFileFilter ff)
   at SharpCifs.Smb.SmbFile.List(String wildcard, Int32 searchAttributes, ISmbFilenameFilter fnf, ISmbFileFilter ff)

reb3lzrr avatar Aug 26 '20 13:08 reb3lzrr

@reb3lzrr My org just ran into this too. It doesn't happen frequently, but it appears we sometimes get disconnected from the remote endpoint of the Socket. Microsoft docs say "The RemoteEndPoint is set after a call to either Accept or Connect. If you try to access this property earlier, RemoteEndPoint will throw a SocketException." https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.remoteendpoint?view=net-5.0

Then when we try to validate whether we are connected, we hit this exception.

The code you have written seems completely fine, since the code only uses those properties in the exception message

GoldenCave avatar Mar 12 '21 17:03 GoldenCave

I am not sure how your team gets around this, but currently when this happens we have to restart our application in order to reconnect to the same smb directory. I am going to try using one of these methods from (https://github.com/ume05rw/SharpCifs.Std/issues/24) to try removing that old connection in the mean time. SmbFile.Initialize(); SmbSession.ClearCachedConnection(); SmbTransport.ClearCachedConnections(); SmbTransport.ClearCachedConnections(true);

GoldenCave avatar Mar 12 '21 17:03 GoldenCave

Our stack is a follows:

System.Net.Sockets.SocketException (107): Transport endpoint is not connected at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName) at System.Net.Sockets.Socket.get_RemoteEndPoint() at SharpCifs.Smb.SmbTransport.Connect() at SharpCifs.Smb.SmbTree.TreeConnect(ServerMessageBlock andx, ServerMessageBlock andxResponse) at SharpCifs.Smb.SmbFile.DoConnect() at SharpCifs.Smb.SmbFile.Connect() at SharpCifs.Smb.SmbFile.Connect0() at SharpCifs.Smb.SmbFile.Exists()

GoldenCave avatar Mar 12 '21 17:03 GoldenCave

Side note: It turns out for our case using SmbTransport.ClearCachedConnections(true); lead to an InvalidOperationException, possibly since it has a foreach over the collection, and then calls to remove it from there. We weren't able to remove the "bad" connection without the force flag set to true.. that means that the problematic SmbTransports believes it has a Socket that is connected

System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.Collections.Generic.List`1.Enumerator.MoveNextRare() at SharpCifs.Smb.SmbTransport.ClearCachedConnections(Boolean force)

GoldenCave avatar Apr 01 '21 15:04 GoldenCave

For the above mentioned InvalidOperationException, I have created this github issue to track it outside of this one: #40

GoldenCave avatar Apr 01 '21 15:04 GoldenCave

I am not sure how your team gets around this, but currently when this happens we have to restart our application in order to reconnect to the same smb directory.

Sorry I didn't have time to get back to you sooner; we choose to abandon this package as it appears that the owners is not maintaining it actively.

We used this package in a Kubernetes-hosted AspNetCore 3.1 application. Thanks to kubernetes we could switch to a CIFS-based driver and rely on the IO tools provided by System.IO namespace.

I hope this answers your question,

reb3lzrr avatar Apr 02 '21 15:04 reb3lzrr