Skip to content

Undocumented NullReferenceException is Raised by ClientWebSocket.ReceiveAsync #47582

@codybartfast

Description

@codybartfast

The ReceiveAsync and SendAsync methods of System.Net.WebSockets.ClientWebSocket (System.Net.WebSockets.Client.dll) can raise Null Reference Exceptions. This is not in the documentation.

Should an Invalid Operation Exception be raised instead?

It can happen when ReceiveAsync or SendAsync are called after an unsuccessful call to OpenAsync.

It happens with .net 5.0.2 (current release version on x64 Windows 20H2), but also happens with 5.0.1.

Sample Code:

    using System;
    using System.Net.WebSockets;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;

    #nullable enable

    namespace HelloWebSocket
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                using (var webSocket = new ClientWebSocket())
                {
                    var encoding = UTF8Encoding.UTF8;
                    var address = "wss://echo.websocket.NON-EXISTENT/";  // <-- Bad Address (should be: echo.websocket.org)
                    var uri = new Uri(address);

                    try
                    {
                        await webSocket.ConnectAsync(uri, CancellationToken.None);
                    }
                    catch (WebSocketException)
                    {
                        Console.WriteLine($"EXCEPTION while connecting to: {address}");
                        Console.WriteLine("But carrying on regardless ...");
                    }

                    var rcvBuff = new byte[100];   
                    var rcvTask = webSocket.ReceiveAsync(rcvBuff, CancellationToken.None);  // <-- Ln 32 NullReference Exception

                    var sndText = "Hello, WebSocket!";
                    var sndBuff = encoding.GetBytes(sndText);
                    await webSocket.SendAsync(sndBuff, WebSocketMessageType.Text, true, CancellationToken.None);

                    var rcvMessage = await rcvTask;
                    switch (rcvMessage.MessageType)
                    {
                        case WebSocketMessageType.Close:
                            Console.WriteLine("Received Close");
                            break;
                        case WebSocketMessageType.Binary:
                            Console.WriteLine("Received Binary");
                            break;
                        case WebSocketMessageType.Text:
                            var rcvText = encoding.GetString(rcvBuff, 0, rcvMessage.Count);
                            Console.WriteLine($"Received Text: {rcvText}");
                            break;
                    }
                }
            }

        }
    }

Sample output:

    ...
    
    Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\5.0.2\System.IO.FileSystem.dll'. Cannot find or open the PDB file.
    Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\5.0.2\System.Collections.Immutable.dll'. Cannot find or open the PDB file.
    Exception thrown: 'System.Net.Sockets.SocketException' in System.Private.CoreLib.dll
    Exception thrown: 'System.Net.Sockets.SocketException' in System.Net.Sockets.dll
    Exception thrown: 'System.Net.Sockets.SocketException' in System.Private.CoreLib.dll
    Exception thrown: 'System.Net.Sockets.SocketException' in System.Net.Http.dll
    Exception thrown: 'System.Net.Sockets.SocketException' in System.Private.CoreLib.dll
    Exception thrown: 'System.Net.Http.HttpRequestException' in System.Net.Http.dll
    Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll
    Exception thrown: 'System.Net.Http.HttpRequestException' in System.Net.Http.dll
    Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll
    Exception thrown: 'System.Net.WebSockets.WebSocketException' in System.Net.WebSockets.Client.dll
    Exception thrown: 'System.Net.WebSockets.WebSocketException' in System.Private.CoreLib.dll
    Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\5.0.2\System.Text.Encoding.Extensions.dll'. Cannot find or open the PDB file.
    EXCEPTION while connecting to: wss://echo.websocket.NON-EXISTENT/
    But carrying on regardless ...
    Exception thrown: 'System.NullReferenceException' in System.Net.WebSockets.Client.dll
    Exception thrown: 'System.NullReferenceException' in System.Private.CoreLib.dll
    Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
       at System.Net.WebSockets.ClientWebSocket.ReceiveAsync(ArraySegment`1 buffer, CancellationToken cancellationToken)
       at HelloWebSocket.Program.Main(String[] args) in C:\Stuff\Code\bug\Program.cs:line 32
       at HelloWebSocket.Program.<Main>(String[] args)
    The program '[3772] bug.dll' has exited with code 0 (0x0).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions