helios-io / helios

reactive socket middleware for .NET

Home Page:http://helios-io.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NRE on starting TcpClient.BeginReceive

Aaronontheweb opened this issue · comments

image

Hilarious race condition with this code...

 protected void StartClient()
        {
            if (!_server.IsOpen()) throw new HeliosException("Server is not started yet. Cannot start client yet.");
            _client = _clientConnectionFactory.NewConnection(_server.Local);
            _client.Receive += (data, channel) =>
            {
                if (!HighPerformance)
                {
                    ClientReceiveBuffer.Add(data);
                }
                ClientReceived.GetAndIncrement();
            };
            _client.OnConnection += (address, channel) => channel.BeginReceive();
            _client.OnError += (exception, connection) => _clientExecutor.Exceptions.Add(exception);
            _client.Open();
        }

While the _client.Open() call is still being executed, the _client.OnConnection event fires and channel.BeginReceive() fires before _client.Open() has a chance to finished.