hibri / HttpMock

A library for creating Http servers on the fly in tests and stubbing responses
MIT License
128 stars 44 forks source link

SocketException: An existing connection was forcibly closed by the remote host #17

Closed georgehemmings closed 10 years ago

georgehemmings commented 10 years ago

Hi

I'm getting a SocketException at the end of my test run. Is this normal?

Can you confirm that I should be Disposing the IHttpServer instance?

Thanks George

hibri commented 10 years ago

Yes you should be disposing it. Are you seeing the exception from test code or from the code being rested ?

On 20 Nov 2013, at 19:20, George Hemmings notifications@github.com wrote:

Hi

I'm getting a SocketException at the end of my test run. Is this normal?

Can you confirm that I should be Disposing the IHttpServer instance?

Thanks George

— Reply to this email directly or view it on GitHub.

georgehemmings commented 10 years ago

Test code. On 20 Nov 2013 23:57, "Hibri Marzook" notifications@github.com wrote:

Yes you should be disposing it. Are you seeing the exception from test code or from the code being rested ?

On 20 Nov 2013, at 19:20, George Hemmings notifications@github.com wrote:

Hi

I'm getting a SocketException at the end of my test run. Is this normal?

Can you confirm that I should be Disposing the IHttpServer instance?

Thanks George

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHubhttps://github.com/hibri/HttpMock/issues/17#issuecomment-28945330 .

georgehemmings commented 10 years ago

I should add, I only see that this exception is fired when I run my tests with the debugger attached.

hibri commented 10 years ago

Hey George It's hard to debug the issue without seeing your code. Can you reproduce this in a test case ? Your code could be still in the middle of making a request or hanging on to a connection, when the mock server closes.

georgehemmings commented 10 years ago

Thanks for your help with this, Hibri. I'll try and replicate the issue in a sample project and upload it on Monday.

hibri commented 10 years ago

Glad to help. Socket issues are hard to debug, without seeing the exact interactions.

georgehemmings commented 10 years ago

I've tried to recreate the issue in a sample project and I can't! It's very frustrating!

I did find a workaround, changing the KeepAlive property on my HttpWebRequest instance to false prevents the problem.

hibri commented 10 years ago

If you like you can describe the problem in more detail, feel free to email me on hibri at hibri dot net. It helps to know a bit more about your system.

georgehemmings commented 10 years ago

Thanks, Hibri. It turns out the sample was throwing the exception but VS wasn't set to break when it occurred. I'll push the sample tomorrow, once I've checked it over.

georgehemmings commented 10 years ago

I've pushed my sample to https://github.com/georgehemmings/HttpMockSocketExceptionSample. I'd be very grateful if you could take a look at it.

hibri commented 10 years ago

I changed the stub port to 9009, and got it working. Port 5009 is used by something else. According to google it's winfs. Lower number ports are usually taken. I tend to go for port numbers above 8000

georgehemmings commented 10 years ago

I get the same problem on 9009.

On 27 November 2013 10:45, Hibri Marzook notifications@github.com wrote:

I changed the stub port to 9009, and got it working. Port 5009 is used by something else. According to google it's winfs. Lower number ports are usually taken. I tend to go for port numbers above 8000

— Reply to this email directly or view it on GitHubhttps://github.com/hibri/HttpMock/issues/17#issuecomment-29375046 .

hibri commented 10 years ago

Can you run netstat -na | find "LIST" ?

georgehemmings commented 10 years ago

Yeah, there's nothing on 9009. After you changed the port in test and production, did the test still pass?

On 27 November 2013 11:25, Hibri Marzook notifications@github.com wrote:

Can you run netstat -na | find "LIST" ?

— Reply to this email directly or view it on GitHubhttps://github.com/hibri/HttpMock/issues/17#issuecomment-29377339 .

hibri commented 10 years ago

Yes it did. I pushed my changes to my fork https://github.com/hibri/HttpMockSocketExceptionSample

On Wed, Nov 27, 2013 at 11:28 AM, George Hemmings notifications@github.comwrote:

Yeah, there's nothing on 9009. After you changed the port in test and production, did the test still pass?

On 27 November 2013 11:25, Hibri Marzook notifications@github.com wrote:

Can you run netstat -na | find "LIST" ?

— Reply to this email directly or view it on GitHub< https://github.com/hibri/HttpMock/issues/17#issuecomment-29377339> .

— Reply to this email directly or view it on GitHubhttps://github.com/hibri/HttpMock/issues/17#issuecomment-29377461 .

georgehemmings commented 10 years ago

Please check that in DEBUG > Exceptions.... you have Common Language Runtime Exceptions ticked for Thrown.

On 27 November 2013 11:29, Hibri Marzook notifications@github.com wrote:

Yes it did. I pushed my changes to my fork https://github.com/hibri/HttpMockSocketExceptionSample

On Wed, Nov 27, 2013 at 11:28 AM, George Hemmings notifications@github.comwrote:

Yeah, there's nothing on 9009. After you changed the port in test and production, did the test still pass?

On 27 November 2013 11:25, Hibri Marzook notifications@github.com wrote:

Can you run netstat -na | find "LIST" ?

— Reply to this email directly or view it on GitHub< https://github.com/hibri/HttpMock/issues/17#issuecomment-29377339> .

— Reply to this email directly or view it on GitHub< https://github.com/hibri/HttpMock/issues/17#issuecomment-29377461> .

— Reply to this email directly or view it on GitHubhttps://github.com/hibri/HttpMock/issues/17#issuecomment-29377537 .

georgehemmings commented 10 years ago

Did you manage to recreate the problem @hibri?

hibri commented 10 years ago

Yes, I was able to reproduce it in debug mode. The exception is throw when the client that was requesting data from the mock server disconnects before the request completes. In your case this bit of code

using (var webClient = new WebClient()) { result = webClient.DownloadString(" http://localhost:9009/Stub"); }

Webclient could be behaving differently in debug mode. When using the MSTest runner in debug mode, when the exception is thrown the test runner process is still active and hangs on to the old socket instead of quitting and releasing the socket. This will cause a socket in use exception the next time you run the test.

This doesn't happen when using the Resharper Nunit test runner. In both cases the tests run fine when not debugging.

On Thu, Nov 28, 2013 at 9:33 AM, George Hemmings notifications@github.comwrote:

Did you manage to recreate the problem @hibri https://github.com/hibri?

— Reply to this email directly or view it on GitHubhttps://github.com/hibri/HttpMock/issues/17#issuecomment-29450037 .

georgehemmings commented 10 years ago

I have the same problem with the Resharper Nunit test runner. Would you push your changes please?

georgehemmings commented 10 years ago

The same problem being the "An existing connection was forcibly closed by the remote host" exception. Not the socket in use exception, that is.

hibri commented 10 years ago

"An existing connection was forcibly closed by the remote host" is the error that's caused by the Webclient disconnecting. I've pushed the change, but I've only added an NUnit test.

georgehemmings commented 10 years ago

I'm still getting the same problem. Thanks for all your help though.