简体   繁体   中英

.Net SMTPClient leaves connection in WAIT_CLOSE

I have an issue plaguing me. Every 2 to 3 days, I end up getting connections to our SMTP provider that don't finish closing. Running netstat on the server shows the connection with a status of CLOSE_WAIT. Researching I thought I had it narrowed down to an object not being disposed of properly.

Specifically the .Net SMTPClient class that in previous version of the framework did not implement IDisposable. When we moved to the 4.0 framework we never needed to go back and update this, meaning we've been along time without these errors. Then a few weeks ago we started having this problem. It started when our provider had an outage on their servers. In theory they could still be having intermittent issues that they are not broadcasting. But in either case my code needs to be able to recover and keep going.

I've been through every piece of code that sends emails, and they've all been fixed. Every MailMessage object and every SMTPClient object is in a using statement. However I still get this issue randomly. When it does happen is seems to happen multiple times very close together, then doesn't happen for days.

The worst of the problem is that once I end up with two connections to the same server with this state, subsequent messages pool waiting for an additional resource, but ultimately just time out. Resetting the service immediately fixes the issue.

I know there are posts out there stating that the configuration can be adjusted to allow more simultaneous connections. But that doesn't fix the problem, just masks it a bit.

My statement above was incorrect.

"I know there are posts out there stating that the configuration can be adjusted to allow more simultaneous connections. But that doesn't fix the problem, just masks it a bit."

I adjusted the allowed connections in the config file expecting it to just give me more time to actively monitor the problem and proactively address it while continuing to look for a solution.

As soon as I added the configuration value the problem has stopped completely.

<system.net>
  <connectionManagement>
    <add address="{AddressGoesHere}" maxconnection="10" />
  </connectionManagement>
</system.net>

In fact, one of our lesser used services I didn't apply the configuration update immediately as it processed fewer emails and wasn't as susceptible. Ultimately that one broke while the more heavily used ones still working like charms.

I'd love to know more of the why. My best understanding is that we are trying to open more connections to the server than was allowed and it was causing some to get hung up. Default of 2 connections was piling them up behind and in some cases .NET seemed to timeout while the connection was open but not disconnecting at the OS level. (eg. 28 seconds waiting for a connection, then needed 3 seconds to process but was killed at the 30 second time.)

Now that I've allowed 10 connections they are processing more threads simultaneously and not backing up waiting for a slot to open up. I haven't had a single problem with the new configuration in the past week.

I hope this can help someone sleep better than I did for a few nights :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM