Skip to content

SmtpClient not sending QUIT on Dispose? #912

Closed

Description

@mguinness

Per the documentation at SmtpClient.Dispose this method should send QUIT:

Sends a QUIT message to the SMTP server, gracefully ends the TCP connection, and releases all resources used by the current instance of the SmtpClient class.

However when using in .NET Core, this does not appear to be the case.

In the .NET Reference Source, the underlying SmtpTransport class does this:
https://github.com/Microsoft/referencesource/blob/master/System/net/System/Net/mail/SmtpTransport.cs#L35-L58

In the .NET Core Libraries, the same code isn't present in SmtpTransport class:
https://github.com/dotnet/corefx/blob/master/src/System.Net.Mail/src/System/Net/Mail/SmtpTransport.cs

Was there a reason this code wasn't ported over to .NET Core?

Activity

  1. davidsh commented on Jan 26, 2019

    @davidsh
    Contributor

    Was there a reason this code wasn't ported over to .NET Core?

    This is a likely a product bug in .NET Core.

  2. nil4 commented on Jan 26, 2019

    @nil4
    Contributor

    @davidsh would it make sense to implement DisposeAsync on SmtpClient, given that network packets would be sent (and flushed) when disposed? /cc @stephentoub

  3. davidsh commented on Jan 26, 2019

    @davidsh
    Contributor

    There is no SmtpClient.DisposeAsync() method on the API. In fact, this method was added to .NET Framework long before Task async pattern was available. So, it would need to be implemented as a blocking/synchronous API.

  4. nil4 commented on Jan 26, 2019

    @nil4
    Contributor

    @davidsh sure, but since dotnet/corefx#32665 introduced IAsyncDisposable, could SmtpClient be updated to implement the async disposal API for frameworks that support it?

  5. davidsh commented on Jan 26, 2019

    @davidsh
    Contributor

    Could SmtpClient be updated to implement the async disposal API for frameworks that support it?

    Yes, but that would be a separate issue that would need to be opened. And it requires an API change/review process as well.

  6. mguinness commented on Jan 29, 2019

    @mguinness
    Author

    Prior to this being worked on, is it possible to change/remove the XML documentation from the SmtpClient.Dispose() method? Right now it's misleading and adds to confusion.

  7. davidsh commented on Jan 29, 2019

    @davidsh
    Contributor

    Prior to this being worked on, is it possible to change/remove the XML documentation from the SmtpClient.Dispose() method? Right now it's misleading and adds to confusion.

    Can you be specific about what is wrong with the current documentation? Are you referring to documentation here? Or someplace else?

  8. mguinness commented on Jan 30, 2019

    @mguinness
    Author

    From the <summary> tag used by Intellisense.

    image

  9. davidsh commented on Jan 30, 2019

    @davidsh
    Contributor

    From the

    tag used by Intellisense.

    Are you showing the screenshot from Visual Studio or some other tool?

    In general, the documentation is correct. That is how the API should work. And on .NET Framework, the API works correctly. We don't change the documentation for a bug like this. This is a product bug with .NET Core and will be fixed in a future release.

  10. mguinness commented on Jan 30, 2019

    @mguinness
    Author

    From Visual Studio. I understand that this is how the API is supposed to work, but if it doesn't and it's a known bug it should be documented/referenced somewhere. Especially if the fix doesn't make it into the 3.0 release. Just my 2¢ worth.

  11. wfurt commented on Aug 20, 2019

    @wfurt
    Member

    is there any practical impact of this @mguinness ?
    If so, could you post simple repro with description?

  12. davidsh commented on Aug 20, 2019

    @davidsh
    Contributor

    is there any practical impact of this @mguinness ?

    We need to fix this same as .NET Framework. Without sending the QUIT, the TCP connection to the server is not closed cleanly. So, this results in connections with CLOSE_WAIT and can eat up local ephemeral ports.

  13. wfurt commented on Aug 20, 2019

    @wfurt
    Member

    OK. We can look at .NET Framework. Interesting part is when Dispose() is called while still sending email body.

  14. mguinness commented on Oct 22, 2019

    @mguinness
    Author

    You can see the ports that are tied up using the following PowerShell command. Is stopping the process the only way to release these?

    Get-NetTCPConnection -State CloseWait -RemotePort 587 | Format-Table -Property OwningProcess,LocalPort,CreationTime

    Is there any workaround to explicitly close connections before disposing? Is it possible to use the CloseConnectionGroup(string connectionGroupName) method of the ServicePoint property to achieve this?

  15. davidsh commented on Oct 22, 2019

    @davidsh
    Contributor

    Is stopping the process the only way to release these?

    Most likely.

    Is there any workaround to explicitly close connections before disposing? Is it possible to use the CloseConnectionGroup(string connectionGroupName) method of the ServicePoint property to achieve this?

    Are you using an explicit 'connectionGroupName' with the SmtpClient API? If not, then you won't be able to close the connections easily. And some of those .NET Framework ServicePoint related APIs are internal and not public. And I don't think attempting to use reflection is possible/recommended with Windows PowerShell.

  16. 51 remaining items

  17. MarcoRossignoli commented on Dec 17, 2019

    @MarcoRossignoli
    Member

    Given that, it should be difficult to get into a situation where sending QUIT would block.

    It's not clear to me why inflight data send and quit are "related" to blocking...I mean AFAIK every data send to network can block, unrelated to previous send data, am I wrong?I mean I can send message and at the end block on send quit. My concern is not related to dispose that wait for the inflight message, but the normal workflow send->quit.
    Block in the way that an async send could scale better than sync one. FYI fix is ready if you want take a look #683

  18. scalablecory commented on Dec 17, 2019

    @scalablecory
    Contributor

    It's not clear to me why inflight data send and quit are "related" to blocking...I mean AFAIK every data send to network can block, unrelated to previous send data, am I wrong?

    @Tratcher's assumption is that SmtpClient will not be used in a way which would fill up your TCP send buffer. In this case, writing a QUIT will just write to the same buffer without blocking.

  19. tmds commented on Dec 17, 2019

    @tmds
    Member

    If you want to send QUIT in Dispose, set Socket.Blocking to false before sending. That avoids the blocking behavior.

  20. scalablecory commented on Dec 17, 2019

    @scalablecory
    Contributor

    Note, the spec actually calls for more than QUIT -- it calls for us to wait for the reply. So, if we really wanted to be compatible, we would be blocking regardless of SmtpClient usage.

  21. MarcoRossignoli commented on Dec 17, 2019

    @MarcoRossignoli
    Member

    Yep per spec we should wait reply.

  22. removed
    api-suggestion
    untriaged
    on Feb 20, 2020
  23. modified the milestones: Future, 5.0 on Feb 26, 2020
  24. davidsh commented on Feb 26, 2020

    @davidsh
    Contributor

    Fixed by PR #683

  25. mguinness commented on Feb 27, 2020

    @mguinness
    Author

    Thanks to Marco for the PR. @davidsh Are there plans to backport this to 3.1, or will we need to wait for .NET 5?

  26. davidsh commented on Feb 27, 2020

    @davidsh
    Contributor

    Are there plans to backport this to 3.1, or will we need to wait for .NET 5?

    There are no plans for backport to 3.1. But I'll let @karelz or @danmosemsft comment on that. We would need to see many customers impacted in order to considering a backport.

  27. karelz commented on Feb 27, 2020

    @karelz
    Member

    As @davidsh mentioned, it depends on impact - how big, how many customers, how hard to workaround.
    So far, this didn't seem to be absolutely terrible adoption blocker IMO.

  28. ghost locked as resolved and limited conversation to collaborators on Dec 14, 2020
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

    area-System.Netbugtenet-compatibility

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions