dotnet / dotnet/wcf

[Bug] NullReferenceException in SocketConnectionInitiator for UnixDomainSocket when cannot connect to server instead of meaningful error

Open
#5,433 1 comment 0 reactions 1 assignee Claimed by @imcarolwang View on GitHub
triaged
Dominant language
C#
Stars
1.8k
Forks
576
Avg merge
6d 9h
Merged PRs (30d)
2

Description

**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`:
![image](https://github.com/dotnet/wcf/assets/71182505/69a7472f-d798-4e39-82ae-bee05339bcf3)

So further in ConvertConnectException we shouldn't check for `lastException` or check if it's not null and check `innerException` instead:
![image](https://github.com/dotnet/wcf/assets/71182505/f66976dc-eaf4-4fb4-bdbb-c8664df85c63)

**To Reproduce**
Steps to reproduce the behavior:
1. Run projects in attached solution(server and client)
[ExceptionSample.zip](https://github.com/dotnet/wcf/files/14417654/ExceptionSample.zip)
2. Press enter in client project to start call
3. See `NullReferenceException` when 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.$(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
![image](https://github.com/dotnet/wcf/assets/71182505/ac905d0c-d3ad-40a9-80eb-a7ea6a4e1317)

**Additional context**
Add any other context about the problem here.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.