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 (show other bugs)
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)

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


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

Note You need to log in before you can comment on or make changes to this bug.