jstedfast / MailKit

A cross-platform .NET library for IMAP, POP3, and SMTP.
http://www.mimekit.net
MIT License
6.04k stars 809 forks source link

SocketException (995): The I/O operation has been aborted because of either a thread exit or an application request. #1703

Closed mbukaiev closed 5 months ago

mbukaiev commented 5 months ago

After connection failed task unhandled exception raised in TaskScheduler. Don't forget to subscribe to TaskScheduler.UnobservedTaskException to reproduce the test

[Test]
    public async Task Test()
    {
        try
        {
            var client = new SmtpClient { Timeout = 1000 };
            await client.ConnectAsync("smtp.gmail.com", 466, MailKit.Security.SecureSocketOptions.None, new CancellationTokenSource().Token);
        }
        catch (Exception)
        {
        }
    }

stack trace

Exception: System.Net.Sockets.SocketException (995): The I/O operation has been aborted because of either a thread exit or an application request.
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
   at System.Threading.Tasks.ValueTask.ValueTaskSourceAsTask.<>c.<.cctor>b__4_0(Object state)
TearDown : NUnit.Framework.AssertionException : Unhandled exceptions [count = 1] detected, probably in non-awaited async void method:
Possible place: System.Threading.Tasks.ValueTask+ValueTaskSourceAsTask_1
Exception: System.Net.Sockets.SocketException (995): The I/O operation has been aborted because of either a thread exit or an application request.
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
   at System.Threading.Tasks.ValueTask.ValueTaskSourceAsTask.<>c.<.cctor>b__4_0(Object state)
mbukaiev commented 4 months ago

I consider this a bug because I've encountered this exception in production. This test only reproduces it. The problem is that after calling the ConnectAsync method, an unhandled exception in TaskScheduler appears, even if I handle the exception returned by the method. It seems like some non-awaited async void method or non-awaited task raised an exception during the execution of the ConnectAsync method.

jstedfast commented 4 months ago

I was confused earlier because of the stack trace and being on my phone.

When I looked again after getting to my computer, I saw the issue.

It's fixed now.

mbukaiev commented 4 months ago

Which version of package can I try?

jstedfast commented 4 months ago

The latest CI build on myget.org

See the download links at the top of the README.md