runtime: Disposing Socket then rebinding fails with SocketError.AddressAlreadyInUse on Unix
.NET Core 2.0.0 runtime macOS 10.12.6 Seirra and Ubuntu 16.04.2 LTS (Xenial Xerus)
using System;
using System.Net;
using System.Net.Sockets;
namespace SocketRebind
{
class Program
{
static void Main(string[] args)
{
for (var i = 0;; i++)
{
Console.WriteLine("Iteration #{0}", i);
var serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(IPAddress.Loopback, 5000));
serverSocket.Listen(512);
var clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect(IPAddress.Loopback, 5000);
var acceptSocket = serverSocket.Accept();
acceptSocket.Dispose();
clientSocket.Dispose();
serverSocket.Dispose();
}
}
}
}
Expected behavior:
Program should iterate indefinitely. This is what happens on Windows.
Actual behavior:
After the first iteration, Socket.Bind throws “SocketException: Address already in use”
Output (identical on macOS and Ubuntu):
$ dotnet run
Iteration #0
Iteration dotnet/corefx#1
Unhandled Exception: System.Net.Sockets.SocketException: Address already in use
at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at SocketRebind.Program.Main(String[] args) in /Users/shalter/source/halter73/SocketRebind/Program.cs:line 16
$
$
$ dotnet run
Iteration #0
Unhandled Exception: System.Net.Sockets.SocketException: Address already in use
at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at SocketRebind.Program.Main(String[] args) in /Users/shalter/source/halter73/SocketRebind/Program.cs:line 16
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 51 (40 by maintainers)
Commits related to this issue
- Set SO_REUSEADDR on managed listen sockets on Unix - https://github.com/dotnet/corefx/issues/24562 — committed to aspnet/KestrelHttpServer by halter73 7 years ago
- Set SO_REUSEADDR on managed listen sockets on Unix - https://github.com/dotnet/corefx/issues/24562 — committed to aspnet/KestrelHttpServer by halter73 7 years ago
- Set SO_REUSEADDR on managed listen sockets on Unix - https://github.com/dotnet/corefx/issues/24562 — committed to aspnet/KestrelHttpServer by halter73 7 years ago
- Set SO_REUSEADDR on managed listen sockets on Unix - https://github.com/dotnet/corefx/issues/24562 — committed to aspnet/KestrelHttpServer by halter73 7 years ago
- Set SO_REUSEADDR on managed listen sockets on Unix - https://github.com/dotnet/corefx/issues/24562 — committed to aspnet/KestrelHttpServer by halter73 7 years ago
- Set SO_REUSEADDR on managed listen sockets on Unix (#2111) - https://github.com/dotnet/corefx/issues/24562 — committed to aspnet/KestrelHttpServer by halter73 7 years ago
- Remove workaround for https://github.com/dotnet/corefx/issues/24562. This issue caused a bind to a recently used endpoint to fail on macOS and Linux. Addresses #2820 — committed to drieseng/KestrelHttpServer by drieseng 6 years ago
- Remove workaround for dotnet/corefx#24562 (#2863) This issue caused a bind to a recently used endpoint to fail on macOS and Linux. Addresses #2820 — committed to aspnet/KestrelHttpServer by drieseng 6 years ago
- Remove workaround for dotnet/corefx#24562 (#2863) This issue caused a bind to a recently used endpoint to fail on macOS and Linux. Addresses #2820 — committed to aspnet/KestrelHttpServer by drieseng 6 years ago
Ended up just grabbing the 2.1.302 tarball and manually installing it, and I’m happy to say it fixed the HttpListener socket issue. Thanks!
@doyouevensunbro I noticed that the debian/ubuntu repository is missing the latest SDK version yesterday. I adapted the debian dockerfile to work on Ubuntu Xenial.
Sorry about that, we are using
<TargetFramework>netcoreapp2.0</TargetFramework>
and I’m guessing that should be<TargetFramework>netcoreapp2.1</TargetFramework>
?@joshgarnett This should be fixed with .NET Core 2.1. If you need a workaround for now, it’s pretty ugly, but here it is. Just call
EnableRebinding(socket)
prior tosocket.Bind(...)
.@Mga-Denmark port stealing is still possible by setting
ReuseAddress
.