[Bug] NullReferenceException in SocketConnectionInitiator for UnixDomainSocket when cannot connect to server instead of meaningful error
Describe the bug
In SocketConnectionInitiator when cannot connect to server SocketException occurs, but due to incorrect handling instead of processing that exception NullReferenceException is thrown which is confusing
lastException is null if there is any exception in CreateConnectionAsync:
So further in ConvertConnectException we shouldn't check for lastException or check if it's not null and check innerException instead:
To Reproduce Steps to reproduce the behavior:
- Run projects in attached solution(server and client) ExceptionSample.zip
- Press enter in client project to start call
- See
NullReferenceExceptionwhen using invalid path instead of more meaningful error
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at System.ServiceModel.Channels.SocketConnectionInitiator.ConvertConnectException(SocketException socketException, Uri remoteUri, TimeSpan timeSpent, Exception innerException)
at System.ServiceModel.Channels.SocketConnectionInitiator.ConnectAsync(Uri uri, TimeSpan timeout)
at System.ServiceModel.Channels.BufferedConnectionInitiator.ConnectAsync(Uri uri, TimeSpan timeout)
at System.ServiceModel.Channels.ConnectionPoolHelper.EstablishConnectionAsync(TimeSpan timeout)
at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpenAsync(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.OnOpenAsyncInternal(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.System.ServiceModel.IAsyncCommunicationObject.OpenAsync(TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.OnOpenAsync(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.OnOpenAsyncInternal(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.System.ServiceModel.IAsyncCommunicationObject.OpenAsync(TimeSpan timeout)
at System.Runtime.TaskHelpers.WaitForCompletion(Task task)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open()
at System.ServiceModel.Channels.ServiceChannelProxy.System.ServiceModel.ICommunicationObject.Open()
at Program.<Main>$(String[] args) in C:\Users\user\Desktop\ExceptionSample\ExceptionSample\Program.cs:line 15
Video demo: https://github.com/dotnet/wcf/assets/71182505/cf649b99-29c1-4804-b9ef-5b426e952a9f
Expected behavior NullReferenceException shouldn't happen and SocketException should be processed in some way.
Screenshots
If applicable, add screenshots to help explain your problem.
Exception in https://github.com/dotnet/wcf/blob/main/src/System.ServiceModel.UnixDomainSocket/src/System/ServiceModel/Channels/SocketConnection.cs#L792
Additional context Add any other context about the problem here.
@imcarolwang can you please investigate and come up with a PR? Please make sure it throws EndPointNotFound exception, the call to ConvertConnectException should pass in SocketException, not lastException, and add unit test as well.