TalAloni / SMBLibrary

Free, Open Source, User-Mode SMB 1.0/CIFS, SMB 2.0, SMB 2.1 and SMB 3.0 server and client library
GNU Lesser General Public License v3.0
733 stars 185 forks source link

UnobservedTaskException's caused by Smb2Client.Disconnect method #256

Closed raufismayilov closed 5 months ago

raufismayilov commented 5 months ago

Calling Smb2Client.Disconnect causes UnobservedTaskExceptions. This happens even if just Smb2Client.Connect and then Smb2Client.Disconnect is called. On Linux the error is as follows:

System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. (Operation canceled)
 ---> System.Net.Sockets.SocketException (125): Operation canceled
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
   at System.Threading.Tasks.ValueTask`1.ValueTaskSourceAsTask.<>c.<.cctor>b__4_0(Object state)
   --- End of inner exception stack trace ---

On Windows the error is System.Net.Sockets.SocketException (995): The I/O operation has been aborted because of either a thread exit or an application request.

This is most likely caused by the fact that when the socket is closed Socket.EndReceive is not called in OnClientSockerReceived. The same applies to Smb1Client as well

TalAloni commented 5 months ago

Thank you Rauf for working on this issue. It was also reported in #246 but you've provided more technical information that better explained this issue and a possible solution which is always helpful.

TalAloni commented 5 months ago

This is most likely caused by the fact that when the socket is closed Socket.EndReceive is not called in OnClientSockerReceived. The same applies to Smb1Client as well

Can this be because Socket.Close is not called in OnClientSockerReceived? I appreciate this theory, but a more concrete analysis will be much more helpful.

raufismayilov commented 5 months ago

You can ensure that Socket.EndReceive is always called and you will see that Unobserved exceptions are gone. This is your current check that prevents from this to happen:

if (!clientSocket.Connected)
{
        state.ReceiveBuffer.Dispose();
        return;
}
TalAloni commented 5 months ago

Thanks again Rauf, I have removed that piece of code and ran several tests - but I did not suffer from the issue to begin with - so I'm not sure that I can declare that the fix is verified. Can please you run it as well verify that this specific issue is solved in the latest master?

raufismayilov commented 5 months ago

Hello TelAloni, I will do the test and let you know, in the meantime, if you want I can set up a call and demo the issue.

TalAloni commented 5 months ago

Thanks, I'm waiting to hear if there is an issue following the latest change. if there is no issue anymore - no need to demo it.

raufismayilov commented 5 months ago

Hello TalAloni, We have tested and can confirm that your changes fixed UnobservedTaskException's. Just in case, NullReferenceException's have also gone and don't cause application crashes anymore.

TalAloni commented 5 months ago

Thank you Rauf for the testing and assistance!

TalAloni commented 5 months ago

This issue has been solved in v1.5.2