Skip to content

Socket.Select throws ObjectDisposedException when one of the sockets is disposed #34886

Description

@drieseng

In (desktop) .NET Framework, Socket.Select(....) throws a SocketException when one of the sockets that you specify is disposed:

System.Net.Sockets.SocketException (10038): An operation was attempted on something that is not a socket.

In .NET Core 2.1 and 3.1, this is also the exception that is thrown on Windows. On Linux, no exception is thrown.

Now in .NET 5.0 Preview 2, an ObjectDisposedException is thrown on both Windows and Linux:

System.ObjectDisposedException: Safe handle has been closed.
Object name: 'SafeHandle'.
at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
at System.Net.Sockets.SocketPal.AddToPollArray(PollEvent* arr, Int32 arrLength, IList socketList, Int32& arrOffset, PollEvents events, Int32& refsAdded)
at System.Net.Sockets.SocketPal.SelectViaPoll(IList checkRead, Int32 checkReadInitialCount, IList checkWrite, Int32 checkWriteInitialCount, IList checkError, Int32 checkErrorInitialCount, PollEvent* events, Int32 eventsLength, Int32 microseconds)
at System.Net.Sockets.SocketPal.Select(IList checkRead, IList checkWrite, IList checkError, Int32 microseconds)
at System.Net.Sockets.Socket.Select(IList checkRead, IList checkWrite, IList checkError, Int32 microSeconds)

To reproduce, compile and run the following code fragment:

using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;

class SelectDisposed
{
    private static void Main()
    {
        var endPoint = new IPEndPoint(IPAddress.Loopback, 8080);
        var client = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        client.Dispose();

        Socket.Select(new List<object> { client }, null, null, -1);
    }
}

Metadata

Metadata

Assignees

Labels

area-System.Net.Socketsbreaking-changeIssue or PR that represents a breaking API or functional change over a previous release.documentationDocumentation bug or enhancement, does not impact product or test codetenet-compatibilityIncompatibility with previous versions or .NET Framework

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions