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 3 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 TcpClient AcceptTcpClient()
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 static TResult EndAcceptCore<TResult>(IAsyncResult asyncResult)
{
try
{
return TaskToApm.End<TResult>(asyncResult);
}
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.OperationAborted)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There could be other cases that cause this exception.

I think we should check explicitly for !_active before changing to ODE. That's what other routines here do.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering what are those other cases?

Anyways, I changed the line to catch (SocketException) when (!_active). Want to be equivalent with the old code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering what are those other cases?

Possibly there aren't any. But it seems better to explicitly check _active, rather than just assume this is the only way that exception can happen.

{
// 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