0

I use HttpListener class for simple multitheading HTTP server for Long Polling actions. I use AutoResetEvents and WaitHandle.WaitAny for signal messaging between threads. Application works fine on .NET 4.5. But we have some problem with multithreading after porting application on Mono (version 3.2.8 (Debian 3.2.8+dfsg-4ubuntu1)).

We expected when second HTTP request is received by server handling of first HTTP request should be finish. And so on. But in Mono handling of first request finish only by timeout. The simplified code of my application is below.

class Program
{
    private static AutoResetEvent autoEvent = new AutoResetEvent(false);
    private static AutoResetEvent autoEvent2 = new AutoResetEvent(false);

    public static void Main()
    {
        HttpListener listener = new HttpListener();

        listener.Prefixes.Add("http://*:2999/");

        listener.Start();
        Console.WriteLine("Listening...");
        BeginGetContext(listener);
        Console.ReadLine();
    }

    public static void BeginGetContext(HttpListener listener)
    {
        Console.WriteLine("BeginGetContext...");
        while (listener.IsListening)
        {
            var asyncResult = listener.BeginGetContext(ListenerCallback, listener);
            asyncResult.AsyncWaitHandle.WaitOne();
        }
    }

    public static void ListenerCallback(IAsyncResult result)
    {
        Console.WriteLine("ListenerCallback...");

        autoEvent.Set();
        autoEvent.Reset();

        HttpListener listener = (HttpListener)result.AsyncState;
        HttpListenerContext context = listener.EndGetContext(result);

        // some code
        //...

        int res = WaitHandleNext();

        if (res == 0 || res == 1)
        {
            // Never get this line
            Console.WriteLine("Done...");
        }
        else if (res == WaitHandle.WaitTimeout)
        {
            // Always here
            Console.WriteLine("Done by timeout");
        }
    }

    private static int WaitHandleNext()
    {
        Console.WriteLine("WaitHandleNext...");
        int waitResult = WaitHandle.WaitAny(new[] { autoEvent, autoEvent2 }, TimeSpan.FromSeconds(30), false);
        return waitResult;
    }
}

If I put Thread.Sleep(10) between autoEvent.Set and autoEvent.Reset I get expected behaviour but not always.

autoEvent.Set();
Thread.Sleep(10);
autoEvent.Reset();

I don't know it is expected behaviour of Mono, bug in Mono or my application bug. How can I reach behaviour of my application on Mono such on .Net? Any workarounds or other help would be great.

6
  • What exactly is the behavior you're getting? Why are you manually calling Reset() on an AutoResetEvent? I suspect this may be resetting the signal before the waiting thread has a chance to be released - not sure if this is by design or what though.
    – lc.
    Jul 29, 2014 at 14:37
  • 1
    autoresetevent.set will unblock only one waiting thread. If none are waiting, the first thread to wait will pass immediately. Calling Reset right after Set will basically stop the second part from happing. (i.e. if no threads are waiting, the next thread to wait will block). is that the behaviour you are expecting? Jul 29, 2014 at 14:47
  • @PeterRitchie, yes, you are right. Jul 29, 2014 at 14:51
  • 1
    If it doesn't do that, I'd suggest it's a bug (I haven't looked that closely at your code) and logging it with the Mono team Jul 29, 2014 at 15:01
  • 1
    The bug was created: bugzilla.xamarin.com/show_bug.cgi?id=21682 Jul 30, 2014 at 7:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.