1

We are seeing a very strange issue with our signalR client that results in complete port exhaustion on the machine running the code. Over 16000 ports end up in the Close_Wait state.

The logs show that the Closed handle is firing several hundred times a second.

The program can run fine for hours or days before the issue occurs.

How is it possible to make the Closed handle in the code below fire continuously until all ports are exhausted?

using Microsoft.AspNet.SignalR.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestApp
{

    public class TestEvents
    {

        private HubConnection _connection;
        private IHubProxy<ITestHub, ITestClientContract> Proxy;

        public TestEvents()
        {
            string url = "http://myserver/MySignalRHub";
            _connection = new HubConnection(url);

            _connection.Closed += HubConnectionOnClosed;
            _connection.Reconnected += HubConnectionOnReconnected;
            _connection.Reconnecting += HubConnectionOnReconnecting;

            Proxy = _connection.CreateHubProxy<ITestHub, ITestClientContract>("TestHub");
        }

        public async Task Start()
        {
            await _connection.Start();

            var result = await Proxy.CallAsync(h => h.TestMethod("test"));

            Console.WriteLine($"Got {result} from connection {_connection.ConnectionId}");
        }

        private async void HubConnectionOnClosed()
        {
            Console.WriteLine("Closed");
            try
            {
                await _connection.Start();
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error");
            }
            
        }

        private async void HubConnectionOnReconnected()
        {
            Console.WriteLine("Reconnected");
        }

        private async void HubConnectionOnReconnecting()
        {
            Console.WriteLine("Reconnecting");
        }
    }
}
Q-bertsuit
  • 3,223
  • 6
  • 30
  • 55
  • 1
    You are not stopping nor disposing the connection, see https://stackoverflow.com/questions/64449506/what-is-the-correct-way-of-closing-disconnecting-an-asp-net-core-signalr-clien – Peter Bons Dec 04 '20 at 09:15
  • Thank you for your answer @PeterBons! Do i need to stop/dispose the connection in the HubConnectionOnClosed handler? – Q-bertsuit Dec 04 '20 at 09:20
  • How is the class `TestEvents` called? – Peter Bons Dec 04 '20 at 14:08
  • Main creates an instance of TestEvent, calls Start() and waits forever – Q-bertsuit Dec 04 '20 at 14:35
  • I would suspect some kind of loop or something, otherwise you are only making a single connection & call to the proxy and that wouldn't result in 16000 ports to be used, Can you share the code you have in `Main`? – Peter Bons Dec 04 '20 at 14:39
  • What ended up working, @Q-bertsuit? – oakfish56 Oct 17 '22 at 20:08
  • @oakfish56 I ended up re-writing it in .Net Core. I can't remember if we ever figured out what the root cause was I'm afraid. – Q-bertsuit Oct 17 '22 at 20:14

0 Answers0