Skip to content

SmtpClient DisconnectAsync may not exit with an improperly closed remote connection #827

Description

@tvalenta

Describe the bug
DisconnectAsync can block when packets to a previously functioning session are suddenly dropped. Despite many attempts, I'm unable to get a cancellation token to end the task by setting a timer with CancelAfter.

I have a process which terminates a client session as such:

      var client = new SmtpClient ();   
     .... mail processing here...
     // Send and close
      await client.SendAsync(mimeMessage).ConfigureAwait(false);
      await client.DisconnectAsync(true).ConfigureAwait(false);

Occasionally, this job will hang -- typically always while connecting to an office365.com MTA. I'm unable to reproduce the behavior in production, and resorted to creating an artificial experience with packet filtering on a FreeBSD-based mail server. When dropping packets from the client just prior to the invocation of DisconnectAsync, my client session never ends. I attempted to rectify by passing a Cancellation Token to DisconnectAsync, but the connection continued to hang indefinitely. Calling instead Disconnect(true), it appears the session will close when the stream timeout is reached. (i.e. to whatever value I've set client.Timeout)

To Reproduce
Steps to reproduce the behavior:

  1. Create a new SmtpClient connection to a remote mail server.
  2. Prior to calling DisconnectAsync(), sleep for 10 seconds
  3. While MailKit SMTP client process sleeps, use a firewall to drop packets from the client to the mail server.
    • For my tests, I used the FreeBSD packet filtering abilities, but I could have used any intermediate firewall. e.g.
    • sudo ipfw add 1000 drop ip from me to <client IP address> src-port 25
  4. See session not close

Expected behavior
Expected would be the session to close after the default 120 second timeout, or allow a cancellation token to abort the task.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: MacOS Mojave

Additional context
Add any other context about the problem here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions