Bug 41131 - Stopping a HttpListener throws ObjectDisposedException
Summary: Stopping a HttpListener throws ObjectDisposedException
Status: RESOLVED NOT_REPRODUCIBLE
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: master
Hardware: PC Linux
: --- normal
Target Milestone: 4.6.0 (C8)
Assignee: marcos.henrich
URL:
Depends on:
Blocks:
 
Reported: 2016-05-17 13:57 UTC by Robert van der Boon
Modified: 2016-08-15 14:15 UTC (History)
6 users (show)

Tags: C8Beta1
Is this bug a regression?: Yes
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 on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NOT_REPRODUCIBLE

Description Robert van der Boon 2016-05-17 13:57:11 UTC
Every time I Close() a HttpListener it will throw an ObjectDisposedException from Socket.AcceptAsync, which brings down the process as it is thrown from the ThreadPool.

This is a huge problem for us as we start and stop HttpListeners from our unittests, and this fails with current trunk. (4.4.0-branch @fa2bacc works without error)

Example code:
using System;
using System.Net;
using System.Threading;
namespace TestHttpListener {
   class Program {
      static void Main() {
            HttpListener listener = new HttpListener();
            listener.Prefixes.Add("http://localhost:8765/Test/");
            listener.Start();
            Console.WriteLine("Press enter to stop this");
            Console.ReadLine();
            Console.WriteLine("Close()ing");
            listener.Close();
            Console.WriteLine("Close()d");
            Thread.Sleep(1000); // This sleep is important. Without it the process will (sometimes) have exited.
            Console.WriteLine("Waited and done");
      }
   }
}
The output is:
Press enter to stop this

Close()ing
Close()d

Unhandled Exception:
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Sockets.Socket'.
  at System.Net.Sockets.Socket.ThrowIfDisposedAndClosed () <0x40f880e0 + 0x0004b> in <filename unknown>:0 
  at System.Net.Sockets.Socket.AcceptAsync (System.Net.Sockets.SocketAsyncEventArgs e) <0x40fb3c00 + 0x0001f> in <filename unknown>:0 
  at System.Net.EndPointListener.Accept (System.Net.Sockets.Socket socket, System.Net.Sockets.SocketAsyncEventArgs e) <0x40fb3b60 + 0x00063> in <filename unknown>:0 
  at System.Net.EndPointListener.ProcessAccept (System.Net.Sockets.SocketAsyncEventArgs args) <0x40fb97e0 + 0x00077> in <filename unknown>:0 
  at System.Net.EndPointListener.OnAccept (System.Object sender, System.Net.Sockets.SocketAsyncEventArgs e) <0x40fb97c0 + 0x00013> in <filename unknown>:0 
  at System.Net.Sockets.SocketAsyncEventArgs.OnCompleted (System.Net.Sockets.SocketAsyncEventArgs e) <0x40fb9780 + 0x0002e> in <filename unknown>:0 
  at System.Net.Sockets.SocketAsyncEventArgs.Complete () <0x40fb9760 + 0x00013> in <filename unknown>:0 
  at System.Net.Sockets.Socket.<AcceptAsyncCallback>m__0 (System.IAsyncResult ares) <0x40fb9070 + 0x00383> in <filename unknown>:0 
  at System.Net.Sockets.SocketAsyncResult+<Complete>c__AnonStorey0.<>m__0 (System.Object _) <0x40fb9040 + 0x0001d> in <filename unknown>:0 
  at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () <0x2aaaac7c39a0 + 0x0002f> in <filename unknown>:0 
  at System.Threading.ThreadPoolWorkQueue.Dispatch () <0x2aaaac7c1f20 + 0x001d6> in <filename unknown>:0 
  at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () <0x2aaaac7c3820 + 0x00008> in <filename unknown>:0
Comment 1 Robert van der Boon 2016-05-19 11:19:46 UTC
With branch mono-4.4.0-branch/39e87b8 the test program also works.
With master/85c642b it fails.
Comment 2 Romain Flechner 2016-06-08 14:31:13 UTC
Hi,

I have the same issue in a mobile App.

Xamarin Studio Enterprise
Version 6.1 (build 817)
Installation UUID: 8f939fc0-59c8-428a-aeef-34c176097954
Runtime:
	Mono 4.4.0 (mono-4.4.0-branch/fcf7a6d) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 404000148

Xamarin.iOS
Version: 9.9.0.719 (Xamarin Enterprise)
Hash: 3afb4af
Branch: master
Build date: 2016-04-24 15:13:41-0400

Build Information
Release ID: 601000817
Git revision: 2335763551f9db8296b08542035977b899b7f3b7
Build date: 2016-04-25 10:45:36-04
Xamarin addins: 7f8c9ab2a981143a87fbd5adbde3f5890a838fde
Build lane: monodevelop-lion-cycle8-preview
Comment 3 marcos.henrich 2016-08-09 15:29:53 UTC
I was unable to reproduce this issue on OSX using master e70a8aca1cd575582459db406ab3410e53074b73, mono-4.6.0-branch 5110c098303da1eb8aa08ebad499facc3cc2f56b, mono-4.4.0-branch e6576f8ee1da277d97a5386f13ade6068768245f.

For sanity check I also tried to reproduced with 85c642b5219fe7e7584447766d2d3679303ea8d9 and it failed so my setup should be able to reproduce this, but as it does not fail with recent commits I assume this was fixed on latest C7, C8 and master commits.
Comment 4 akihiko.odaki.4i 2016-08-12 07:41:21 UTC
Reproduced with Xamarin for Visual Studio 4.1.2.18.

I'm not sure how to know the version of Mono runtime, but it says the cause is at:
/Users/builder/data/lanes/3540/1cf254db/source/mono/mcs/class/System/System.Net.Sockets/Socket.cs
"1cf254db" is not a revision number in Mono repository, but I hope it is a build ID or something kind of to find the source code.
Comment 5 marcos.henrich 2016-08-12 13:18:07 UTC
@akihiko thanks for reporting that,

1cf254db is a monodroid revision.

I assume this happens while running on android, can you share a simple project where the issue occurs? and the stack trace with the failure?
Comment 6 akihiko.odaki.4i 2016-08-15 14:15:13 UTC
Yes, the code is really simple:

var listener = new System.Net.HttpListener ();
listener.Prefixes.Add ("http://*:8080/");
listener.Start ();
var bytes = System.Text.Encoding.ASCII.GetBytes ("A Proof of Concept for Bug 41131");
var context = listener.GetContext ();
context.Response.OutputStream.Write (bytes, 0, bytes.Length);
context.Response.Close ();
listener.Close ();

Here is the project including binaries. It's about 500MB.
https://github.com/akihikodaki/Bug4113

Stack trace:
>	0x21 in System.Net.Sockets.Socket.ThrowIfDisposedAndClosed at /Users/builder/data/lanes/3540/1cf254db/source/mono/mcs/class/System/System.Net.Sockets/Socket.cs:3376,5	C#
 	0x1 in System.Net.Sockets.Socket.Accept at /Users/builder/data/lanes/3540/1cf254db/source/mono/mcs/class/System/System.Net.Sockets/Socket.cs:888,4	C#
 	0x1A in System.Net.Sockets.Socket.AnonymousMethod__1 at /Users/builder/data/lanes/3540/1cf254db/source/mono/mcs/class/System/System.Net.Sockets/Socket.cs:997,6	C#
 	0xC in System.IOSelectorJob.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem at /Users/builder/data/lanes/3540/1cf254db/source/mono/mcs/class/System/System/IOSelector.cs:143,4	C#
 	[External Code]