Socket.Select method of the .NET 2.0 framework returns immediately and does not block as expected when an infinite timeout value of -1 is specified.

Applies to: .NET Framework 3.5 Service Pack 1

Symptoms


You are developing a sockets based application using the System.Net.Sockets.Socket.Select method. When you provide an infinite value for the timeout by specifying a value -1, you will experience that the Socket.Select call returns immediately and does not block as expected. This occurs even if there are no sockets that meet the requested criteria of read, write or error. This issue only happens for applications based on the .NET 2.0 framework.

If you target this same application to run with the .NET 4.0 framework, the Socket.Select call behaves as expected when the timeout parameter is set to to -1 indicating an infinite timeout.

Cause


This happens because when the timeout is set to -1, the Socket.Select method of the .NET 2.0 framework does not correctly disable the timeout when calling into the winsock select function. The .NET 4.0 version of the Socket.Select method has however been changed to behave as expected.

Resolution


To workaround this issue, applications that are targeted to run with the .NET 2.0 framework are recommended to set a very high value of a timeout such as System.Int32.MaxValue to mimic an infinite timeout. 

Applications that are targeted to run with the .NET 4.0 framework do not need to use the workaround and can continue to use a value of -1 to indicate an infinite timeout and behave as expected.