WritingToResponse with nonexistent network connection

1,964 views
Skip to first unread message

Tymek

unread,
Sep 26, 2011, 1:25:02 AM9/26/11
to ServiceStack .NET Open Source REST Web Services Framework
Hi,

If the remote end drops the connection before the request is processed
then in
IHttpResponseExtensions' WriteToResponse calls WriteErrorToResponse
which always fail and throw.

The first exception is
[System.Net.HttpListenerException] {"An operation was attempted on a
nonexistent network connection"} System.Net.HttpListenerException

The second is
[System.InvalidOperationException] {"This operation cannot be
performed after the response has been submitted."}
System.InvalidOperationException

This is arguably a bug as these things will happen sooner or later in
a real life application.

I suggest
a) at least don't write to response if it is gone and rethrowing the
original exception
or
b) considering the first exception as 'normal' and just logging the
issue (I think this would be WARNING i think) without re-throwing.


It this list the best place to flag such issues?

Cheerio
Tymek

Demis Bellot

unread,
Sep 26, 2011, 10:31:03 AM9/26/11
to servic...@googlegroups.com
Hi Tymek,

Yeah either here or in https://github.com/ServiceStack/ServiceStack/issues are the best places to raise issues.

If you have working code to show how ideally you would want it to work, I can review it in a pull request?

Thanks,
Demis

Tymek Majewski

unread,
Sep 26, 2011, 11:11:54 AM9/26/11
to servic...@googlegroups.com
Haha! I knew you'll say that (pull request) :)

I have a workaround in place for now and I don't think I'll have time
to create a patch this week, but I'll try to do it next week.

Cheerio
Tymek

Tymek Majewski

unread,
Sep 26, 2011, 11:14:23 AM9/26/11
to servic...@googlegroups.com
BTW. Just for the record: the first exception on Mono is different (I
don't have my dev machine here, so I cannot be precise) and it
complains about
Headers not being settable once they're already have been sent.

On 26 September 2011 23:11, Tymek Majewski

Demis Bellot

unread,
Sep 26, 2011, 11:14:33 AM9/26/11
to servic...@googlegroups.com
Yep, I find it the best and fastest way to communicate what needs to be done :)

As a bonus you get contributor points with your name in the project history!

D

Demis Bellot

unread,
Sep 26, 2011, 11:17:09 AM9/26/11
to servic...@googlegroups.com
ok that's interesting, sounds like it's trying to write another HttpHeader after it has started writing the response body.

If you got a simple example/service that demonstrates the error, that would be a big help in tracking it down.

D

Tymek Majewski

unread,
Sep 28, 2011, 9:36:49 AM9/28/11
to servic...@googlegroups.com
I'll write a test code for it, but the basic use case is that the
remote end drops the connection before the processing of the request
is finished.

Demis Bellot

unread,
Sep 28, 2011, 1:30:14 PM9/28/11
to servic...@googlegroups.com
Yeah I understand the ideal goal, its just that particular code path in HttpListener is particularly tricky to work consistently across all environments.
So if you're able to post something that works for you I can test it out under the different supported environments.

Anyway I'll flag it as an issue to have a look at and check it after I get access to my dev pc's again. 

Cheers,
D

Tymek

unread,
Oct 17, 2011, 11:55:42 PM10/17/11
to ServiceStack .NET Open Source REST Web Services Framework
Hi,

I sent you a pull request for this a minute ago.

Cheers
Tymek

On Sep 29, 1:30 am, Demis Bellot <demis.bel...@gmail.com> wrote:
> Yeah I understand the ideal goal, its just that particular code path in
> HttpListener is particularly tricky to work consistently across all
> environments.
> So if you're able to post something that works for you I can test it out
> under the different supported environments.
>
> Anyway I'll flag it as an issue to have a look at and check it after I get
> access to my dev pc's again.
>
> Cheers,
> D
>
> On Wed, Sep 28, 2011 at 9:36 AM, Tymek Majewski <
>
>
>
>
>
>
>
> letssellsomebana...@gmail.com> wrote:
> > I'll write a test code for it, but the basic use case is that the
> > remote end drops the connection before the processing of the request
> > is finished.
>
> > On 26 September 2011 23:17, Demis Bellot <demis.bel...@gmail.com> wrote:
> > > ok that's interesting, sounds like it's trying to write another
> > HttpHeader
> > > after it has started writing the response body.
> > > If you got a simple example/service that demonstrates the error, that
> > would
> > > be a big help in tracking it down.
> > > D
>
> > > On Mon, Sep 26, 2011 at 11:14 AM, Tymek Majewski
> > > <letssellsomebana...@gmail.com> wrote:
>
> > >> BTW. Just for the record: the first exception on Mono is different (I
> > >> don't have my dev machine here, so I cannot be precise) and it
> > >> complains about
> > >> Headers not being settable once they're already have been sent.
>
> > >> On 26 September 2011 23:11, Tymek Majewski
> > >> <letssellsomebana...@gmail.com> wrote:
> > >> > Haha! I knew you'll say that (pull request) :)
>
> > >> > I have a workaround in place for now and I don't think I'll have time
> > >> > to create a patch this week, but I'll try to do it next week.
>
> > >> > Cheerio
> > >> > Tymek
>
> > >> > On 26 September 2011 22:31, Demis Bellot <demis.bel...@gmail.com>
> > wrote:
> > >> >> Hi Tymek,
> > >> >> Yeah either here or in
> > >> >>https://github.com/ServiceStack/ServiceStack/issuesare the best
> > places
> > >> >> to
> > >> >> raise issues.
> > >> >> If you have working code to show how ideally you would want it to
> > work,
> > >> >> I
> > >> >> can review it in a pull request?
> > >> >> Thanks,
> > >> >> Demis
>
> > >> >> On Mon, Sep 26, 2011 at 1:25 AM, Tymek <
> > letssellsomebana...@gmail.com>

Demis Bellot

unread,
Oct 17, 2011, 11:58:32 PM10/17/11
to servic...@googlegroups.com
Cool, thanks - I'll test it out tomorrow at work (no win/dev pc here).

Tymek Majewski

unread,
Oct 19, 2011, 4:20:33 AM10/19/11
to servic...@googlegroups.com
Cool,

Please remember that the environment I'm working on is mono-2.6.7 on
Linux. The output on Windows may, and most likely be different.

Cheerio
Tymek

Demis Bellot

unread,
Oct 19, 2011, 1:25:55 PM10/19/11
to servic...@googlegroups.com
Yep, it's most likely an issue with mono I should be able to replicate on OSX.

Tymek Majewski

unread,
Oct 19, 2011, 9:45:53 PM10/19/11
to servic...@googlegroups.com
Hmmm...

It's not a really a mono 'issue' as in 'bug'.

Anyway the testcase
ServiceStack.WebHost.Endpoints.Tests.RemoteEndDropsConnectionTests and
the code changes included in the pull request are confirmed to show
the code flow on Windows too.

The test shows really one one possible network error - in reality I
can see variations in the errors returned by Write*ToResponse methods.
The code changes I proposed in the pull request deal with the class of
errors not particular scenario.


Just for paranoia reasons, in case you'll get something else, here
are my test results from Windows Vista:

NUnit version 2.5.10.11092
Copyright (C) 2002-2009 Charlie Poole.
Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov.
Copyright (C) 2000-2002 Philip Craig.
All Rights Reserved.

Runtime Environment -
OS Version: Microsoft Windows NT 6.0.6002 Service Pack 2
CLR Version: 2.0.50727.4216 ( Net 2.0 )

ProcessModel: Default DomainUsage: Single
Execution Runtime: Default
Selected test(s):
ServiceStack.WebHost.Endpoints.Tests.RemoteEndDropsConnectionTests
ExampleAppHost Created at 20/10/2011 9:11:19 AM, listening on
http://localhost:82/
***** ServiceStack.WebHost.Endpoints.Tests.RemoteEndDropsConnectionTests.TestClientDropsConnection(True)
TEST: ERROR: Error occured while Processing Request:
[HttpListenerException] The specified network name is no longer
available, Exception: The specified network name is no longer
available
TEST: ERROR: Could not WriteTextToResponse: This operation cannot be
performed after the response has been submitted., Exception: This


operation cannot be performed after the response has been submitted.

TEST: ERROR: Could not WriteTextToResponse: This operation cannot be
performed after the response has been submitted., Exception: This


operation cannot be performed after the response has been submitted.

TEST: INFO: Failed to write error to response: {0}, Exception: This


operation cannot be performed after the response has been submitted.

TEST: ERROR: Error in HttpListenerResponseWrapper: An operation was
attempted on a nonexistent network connection, Exception: An operation


was attempted on a nonexistent network connection

TEST: ERROR: Error occured while Processing Request: The specified
network name is no longer available, Exception: The specified network
name is no longer available
***** ServiceStack.WebHost.Endpoints.Tests.RemoteEndDropsConnectionTests.TestClientDropsConnection(False)
TEST: ERROR: Error in HttpListenerResponseWrapper: An operation was
attempted on a nonexistent network connection, Exception: An operation


was attempted on a nonexistent network connection

TEST: ERROR: Error this.ProcessRequest(context):
[HttpListenerException]: The specified network name is no longer
available

Tests run: 2, Errors: 0, Failures: 0, Inconclusive: 0, Time: 6.909 seconds
Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0


Hit <enter> key to continue

Reply all
Reply to author
Forward
0 new messages