Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore exception compatibility in TcpListener.EndAccept*** #41745

Merged
merged 4 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,13 @@ public IAsyncResult BeginAcceptSocket(AsyncCallback? callback, object? state) =>
TaskToApm.Begin(AcceptSocketAsync(), callback, state);

public Socket EndAcceptSocket(IAsyncResult asyncResult) =>
TaskToApm.End<Socket>(asyncResult);
EndAcceptCore<Socket>(asyncResult);

public IAsyncResult BeginAcceptTcpClient(AsyncCallback? callback, object? state) =>
TaskToApm.Begin(AcceptTcpClientAsync(), callback, state);

public TcpClient EndAcceptTcpClient(IAsyncResult asyncResult) =>
TaskToApm.End<TcpClient>(asyncResult);
EndAcceptCore<TcpClient>(asyncResult);

public Task<Socket> AcceptSocketAsync()
{
Expand Down Expand Up @@ -280,5 +280,19 @@ private void CreateNewSocketIfNeeded()
_allowNatTraversal = null; // Reset value to avoid affecting more sockets
}
}

private TResult EndAcceptCore<TResult>(IAsyncResult asyncResult)
{
try
{
return TaskToApm.End<TResult>(asyncResult);
}
catch (SocketException) when (!_active)
{
// Socket.EndAccept(iar) throws ObjectDisposedException when the underlying socket gets closed.
// TcpClient's documented behavior was to propagate that exception, we need to emulate it for compatibility:
throw new ObjectDisposedException(typeof(Socket).FullName);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,36 @@ public void ExclusiveAddressUse_SetStartAndStopListenerThenRead_ReadSuccessfully
Assert.True(listener.ExclusiveAddressUse);
}

[Fact]
public void EndAcceptSocket_WhenStopped_ThrowsObjectDisposedException()
{
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();

IAsyncResult iar = listener.BeginAcceptSocket(callback: null, state: null);

// Give some time for the underlying OS operation to start:
Thread.Sleep(50);
listener.Stop();

Assert.Throws<ObjectDisposedException>(() => listener.EndAcceptSocket(iar));
}

[Fact]
public void EndAcceptTcpClient_WhenStopped_ThrowsObjectDisposedException()
{
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();

IAsyncResult iar = listener.BeginAcceptTcpClient(callback: null, state: null);

// Give some time for the underlying OS operation to start:
Thread.Sleep(50);
listener.Stop();

Assert.Throws<ObjectDisposedException>(() => listener.EndAcceptTcpClient(iar));
}

private sealed class DerivedTcpListener : TcpListener
{
#pragma warning disable 0618
Expand Down