Skip to content

TcpListener regression: SocketException at InputShare app #41585

@jiangzeng01

Description

@jiangzeng01

Application Name: Input Share
OS: Windows 10 RS5
CPU: X64
.NET Build Number: 5.0.100-rc.1.20430.2

Verify Scenarios:
1). Windows 10 RS5 X64 + .NET Core SDK build 5.0.100-rc.1.20430.2: Fail
2). Windows 10 RS5 X64 + .NET Core SDK build 5.0.100-preview.8 : Pass
3). Windows 10 RS5 X64 + .NET Core SDK build 3.1.300 Pass

DevDiv bug:
https://devdiv.visualstudio.com/DefaultCollection/DevDiv/_workitems/edit/1188270
Minimal Repro App:
https://github.com/jiangzeng01/socketexceptionrepro.git

Findings :
When we stop TcpListener, it BeingAccesSocket call back, it gets different exception in net5 rc1, it makes the app crash :
We start listener like this :

private static TcpListener listener;
listener = new TcpListener(IPAddress.Any, 4441);
listener.Start(6);
listener.BeginAcceptSocket(Listener_AcceptSocketCallback, null);
label1.Text = "started listening";

We stop like this :

listener.Stop();

In callBack function (Listener_AcceptSocketCallback) : 

private static void Listener_AcceptSocketCallback(IAsyncResult ar)
{
try
{
Socket client = listener.EndAcceptSocket(ar);
}
catch (ObjectDisposedException ex)
{
MessageBox.Show("ObjectDisposedException on 3.1 and 5 prev");
//caught on 3.1 and 5 prev, doesn't catch on .net5 rc1
}
catch (Exception ex)
{
 //since we stop it, we think ObjectDisposedException should be caught, but it comes to here in   //.net5 Rc1
  MessageBox.Show($"Catch on .net5 RC {ex.Message}");
 	listener.BeginAcceptSocket(Listener_AcceptSocketCallback, null);
}
}

Exception from InputShare app:

ISClientListener-> An error occurred while accepting client socket: The I/O operation has been aborted because of either a thread exit or an application request.
--------------------------------------
UNHANDLED EXCEPTION
Not listening. You must call the Start() method before calling this method.
   at System.Net.Sockets.TcpListener.AcceptSocketAsync()
   at System.Net.Sockets.TcpListener.BeginAcceptSocket(AsyncCallback callback, Object state)
   at InputshareLib.Net.ISClientListener.Listener_AcceptSocketCallback(IAsyncResult ar) in C:\Users\Appcompat\Documents\inputsharesource\Inputshare\InputshareLib\Net\ISClientListener.cs:line 72
   at System.Threading.Tasks.TaskToApm.TaskAsyncResult.InvokeCallback()
   at System.Threading.Tasks.AwaitTaskContinuation.<>c.<.cctor>b__17_0(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback callback, Object state, Task& currentTask)
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__140_1(Object state)
   at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
System.Net.Sockets

CC @dotnet-actwx-bot

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions