-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Reusing SmtpClient hangs in net50 #49340
Comments
Tagging subscribers to this area: @dotnet/ncl Issue DetailsCalling SendMailAsync on the same SmtpClient instance fails under some circumstances in net50. DescriptionThe following test succeeds when running under netcoreapp3.1, but fails by timing out in net50 [Fact(Timeout = 10000)]
public async Task Net50Repo()
{
var config = Configuration.Configure();
using var server = SimpleSmtpServer.Start(config.WithRandomPort().Port);
var inst = new SmtpClient
{
Host = "localhost",
Port = server.Configuration.Port,
EnableSsl = false
};
for (int messageNo = 0; messageNo < 2; messageNo++)
{
var mailMessage = new MailMessage("one@example.com", "two@example.com");
await inst.SendMailAsync(mailMessage);
Assert.Equal(messageNo + 1, server.ReceivedEmailCount);
}
} The issue seems to be reusing the SmtpClient, the first SendMailAsync succeeds, but the second call always just hangs indefinitely. Works in 3.1. Newing up a new client for each call also works. Testing against a different Smtp server (eg. Papercut) also works. netDumpster just ends up waiting inside SmtpContext on this line: count = this.socket.Receive(byteBuffer); ConfigurationWe are able to reproduce with netdumpster in unit tests, production is not yet running in net50 for us (partly because of this), so I'm unsure if other smtp server are effected, we are nervous about this. Regression?Yes, regression from 3.1 to 5 Other informationI originally raised the issue on netdumpster, there is some more discussion there, although not much. I am aware that other issues mention SmtpClient is not under active development, and it seems MS recommends migrating to MailKit, but it would still be nice if existing functionality would not break.
|
Triage: we should investigate here. Potentially improve our tests to cover this. |
I did quick check and I cannot reproduce it @chrisaut. Tested on Ubuntu18 agains Postfix. What OS do you use? Can you do packet captures with Wireshark? Perhaps we can see more there. |
I/we are on windows testing against netdumpster. Easiest to repro with this: https://github.com/cmendible/netDumbster/tree/dotnet5
Windows. I can probably get some captures if needed, but you should be able to repro with the branch above. Just to be clear again, we originally thought this was a netdumpster (a fake smtp server for tests) issue, however it functions perfectly in older frameworks, and against other smtp clients. Upgrading to dotnet 50 breaks when using SmtpClient. Like mentioned before, there was a bit of discussion earlier here |
@wfurt if needed I can modify the code in the branch removing everything that is not needed to repro the issue. Just let me know |
I'll try it next week on Windows. If I can reproduce triage should not be too hard IMHO. Part of it is also impact and that drives priority. |
Triage: If we have simple repro and idea what to fix, we should invest. If it proves difficult, I would punt it to Future. |
This issue has been automatically marked Please refer to our contribution guidelines for tips on what information might be required. |
This issue will now be closed since it had been marked |
The needs more info was bogus. Reopening. |
FYI because possibly connected: I'm reusing the same
EDIT: Changing the implementation to use a new instance for every e-mail fixed the timeouts. See comment below. |
I finally got around deploying this to production. And yes, once I've moved to disposing the |
I've done a small investigation, and the problem was introduced with #683. Or, to be precise, it was uncovered by it. The main problem is the fact that SmptClient attempts to open another connection every time runtime/src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpClient.cs Lines 651 to 653 in 2659885
runtime/src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpTransport.cs Lines 119 to 132 in 2659885
And compare this to runtime/src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpClient.cs Lines 499 to 507 in 2659885
runtime/src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpClient.cs Lines 982 to 988 in 2659885
Unlike sync implementation, async doesn't check whether |
do you want to contribute PR @vonzshik ? |
@wfurt sure, though it might take a bit of time (hopefully, building the runtime shouldn't be too hard 😄) |
Calling
SendMailAsync
multiple times on the sameSmtpClient
instance fails under some circumstances in net50.Description
The following test succeeds when running under netcoreapp3.1, but fails by timing out in net50
The issue seems to be reusing the
SmtpClient
, the firstSendMailAsync
succeeds, but the second call always just hangs indefinitely.Works in 3.1. Newing up a new client for each call also works. Testing against a different Smtp server (eg. Papercut) also works.
netDumpster just ends up waiting inside
SmtpContext
on this line:count = this.socket.Receive(byteBuffer);
It's like if the socket remains open it cannot be reused anymore for some reason. Any idea what changed?
Configuration
We are able to reproduce with netdumpster in unit tests, production is not yet running in net50 for us (partly because of this), so I'm unsure if other smtp server are effected, we are nervous about this.
Regression?
Yes, regression from 3.1 to 5
Other information
I originally raised the issue on netdumpster, there is some more discussion there, although not much.
I am aware that other issues mention
SmtpClient
is not under active development, and it seems MS recommends migrating to MailKit, but it would still be nice if existing functionality would not break.@cmendible @scalablecory
The text was updated successfully, but these errors were encountered: