Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow SocketException in socket.Endpoint #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

reb3lzrr
Copy link

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)

@GoldenCave
Copy link

@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
Copy link

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 (#24) to try removing that old connection in the mean time.
SmbFile.Initialize(); SmbSession.ClearCachedConnection(); SmbTransport.ClearCachedConnections(); SmbTransport.ClearCachedConnections(true);

@GoldenCave
Copy link

GoldenCave commented Mar 12, 2021

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
Copy link

GoldenCave commented Apr 1, 2021

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
Copy link

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

@reb3lzrr
Copy link
Author

reb3lzrr commented Apr 2, 2021

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,

JSGInray added a commit to JSGInray/SharpCifs.SMBv1 that referenced this pull request May 14, 2024
BoBiene added a commit to BoBiene/SharpCifs.SMBv1 that referenced this pull request May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants