Bug 2600 - HttpListenerResponse Close behavior doesn't match .NET
Summary: HttpListenerResponse Close behavior doesn't match .NET
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: 2.10.x
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2011-12-21 16:15 UTC by Aaron Bockover
Modified: 2017-03-21 21:08 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 2600 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Aaron Bockover 2011-12-21 16:15:28 UTC
HttpListenerResponse has a Close method, and it has an OutputStream with a Close method.

In .NET, you can call these two methods back to back, regardless of whether or not OutputStream was ever used/written to, and it succeeds (either closes any resources, or doesn't and is quiet about resources that don't need to be closed/were never open).

Pseudo snippet from my head:

try {
    var context = listener.EndGetContext (result);
    byte [] resource;

    if (resources.TryGetValue (context.Request.Url, out resource)) {
        context.Response.StatusCode = 200;
        context.Response.OutputStream.Write (resource, 0, resource.Length);
    } else {
        context.Response.StatusCode = 404;
} finally {
    context.Response.OutputStream.Close ();
    context.Response.Close ();

In the HTTP 200 case, I write something to Response.OutputStream, but in the 404 case I do not. In both cases, both the Response and the Response.OutputStream are closed.

In .NET this works as expected with no exceptions thrown. In Mono (specifically running on Android), I get an exception.
Comment 1 bramkamies 2017-03-21 21:08:48 UTC
Experiencing the same problem on Ubuntu with mono-complete installed.

~/webserv$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.2 LTS
Release:        16.04
Codename:       xenial

My code only had the `context.Response.OutputStream.Close()` in a finally clause.

Removing this no longer made my chrome produce ERR_CONNECTION_REFUSED but instead hang as the connection wasnt getting closed from the server side.

Is there a workaround/fix available at this time?

Also recently reported in http://stackoverflow.com/questions/23720440/connection-drop-in-httplistener-in-c-sharp-mono