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

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

Closed
mbukaiev opened this issue Feb 9, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@mbukaiev
Copy link

mbukaiev commented Feb 9, 2024

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)
@jstedfast jstedfast added need-info More information is needed in order to diagnose the issue. and removed need-info More information is needed in order to diagnose the issue. labels Feb 9, 2024
jstedfast added a commit that referenced this issue Feb 10, 2024
Always call socket.EndConnect(ar) and wait to dispose the socket
until any connection attempt has completed.

Previously, cancelling a socket.BeginConnect() would dispose the socket
more-or-less immediately after canel, and then the "EndConnect" handler
would get called and expect socket.EndConnect(ar) to be called.

Fixes issue #1703
@jstedfast jstedfast added the bug Something isn't working label Feb 10, 2024
@mbukaiev
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

Which version of package can I try?

@jstedfast
Copy link
Owner

The latest CI build on myget.org

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants