Skip to content

Instantly share code, notes, and snippets.

@Inok

Inok/repro.cs Secret

Created December 6, 2023 14:46
Show Gist options
  • Save Inok/9b17282532bca8a01cc5fc0ab9872433 to your computer and use it in GitHub Desktop.
Save Inok/9b17282532bca8a01cc5fc0ab9872433 to your computer and use it in GitHub Desktop.
UnobservedTaskException with sockets
static void Main(string[] args)
{
TaskScheduler.UnobservedTaskException += (sender, args) =>
{
Console.WriteLine("TaskScheduler.UnobservedTaskException event:");
Console.WriteLine(args.Exception.ToString());
args.SetObserved();
};
DoConnect();
GC.Collect(2, GCCollectionMode.Forced, true);
GC.Collect(2, GCCollectionMode.Forced, true);
GC.WaitForPendingFinalizers();
}
private static void DoConnect()
{
var address = IPAddress.Parse("127.0.0.1");
var socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
var result = socket.BeginConnect(new IPEndPoint(address, 12345), null, null);
result.AsyncWaitHandle.WaitOne(5_000, true);
if (!socket.Connected)
{
Console.WriteLine("socket.Connected == false");
socket.Close();
}
else
{
Console.WriteLine("socket.Connected == true");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment