cosullivan / SmtpServer

A SMTP Server component written in C#
MIT License
676 stars 160 forks source link

How to debug sessions that are never closed? #139

Closed large closed 3 years ago

large commented 3 years ago

Hi @cosullivan! I have tested all your examples and connected with telnet, sent rubbish and it disconnects as expected after 5 retries. Socket are marked as closed within netstat.

In my project (that I sadly can not share atm) the network stream is never closed in OnSessionCompleted(). When I quit my application the streams are released...

I cannot figure it out and a debug session has not helped me much either. Checking your code on sessions: https://github.com/cosullivan/SmtpServer/blob/b231b3e1a298795e1672427c3c510f4ce7775904/Src/SmtpServer/SmtpSession.cs#L83-L121

When retries == 0 the while-loop is just quitted, and I figured out that the completedAction(...) is called, but what happend next?

Also tried to clear all the context.properties and a little hack with e.Context.NetworkClient.Stream.Dispose() without luck. In netstat the connection is stated as "ESTABLISHED" until the application is closed. Any tips?

large commented 3 years ago

After massive debugging I finally found my bug for this. Based on your CustomEndpointListenerExample I had forgot to add_innerStream.Dispose(); That made the stream stay forever open, as expected. Case closed :)