Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

NRE on starting TcpClient.BeginReceive #77

Closed
Aaronontheweb opened this issue Apr 13, 2016 · 0 comments
Closed

NRE on starting TcpClient.BeginReceive #77

Aaronontheweb opened this issue Apr 13, 2016 · 0 comments
Labels

Comments

@Aaronontheweb
Copy link
Member

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant