Skip to content
This repository has been archived by the owner on Aug 2, 2019. It is now read-only.

Commit

Permalink
Fixed race condition when stopping TCP server.
Browse files Browse the repository at this point in the history
Connections coming in while stopping the TCP server were not correctly handled.

Bug: #9
  • Loading branch information
pvginkel committed Dec 5, 2015
1 parent e20ecdd commit 202309b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -8,3 +8,4 @@ Thumbs.db
/_ReSharper.*
*.nupkg
/packages
/.vs
18 changes: 11 additions & 7 deletions NHttp/HttpServer.cs
Expand Up @@ -230,17 +230,13 @@ private void StopClients()

private void BeginAcceptTcpClient()
{
if (_state != HttpServerState.Started)
return;

_listener.BeginAcceptTcpClient(AcceptTcpClientCallback, null);
var listener = _listener;
if (listener != null)
listener.BeginAcceptTcpClient(AcceptTcpClientCallback, null);
}

private void AcceptTcpClientCallback(IAsyncResult asyncResult)
{
if (_state != HttpServerState.Started)
return;

try
{
var listener = _listener; // Prevent race condition.
Expand All @@ -250,6 +246,14 @@ private void AcceptTcpClientCallback(IAsyncResult asyncResult)

var tcpClient = listener.EndAcceptTcpClient(asyncResult);

// If we've stopped already, close the TCP client now.

if (_state != HttpServerState.Started)
{
tcpClient.Close();
return;
}

var client = new HttpClient(this, tcpClient);

RegisterClient(client);
Expand Down

0 comments on commit 202309b

Please sign in to comment.