[Mono-bugs] [Bug 683519] New: garbage collector waking up threads that are sleeping infinite

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Tue Mar 29 14:47:48 EDT 2011


https://bugzilla.novell.com/show_bug.cgi?id=683519

https://bugzilla.novell.com/show_bug.cgi?id=683519#c0


           Summary: garbage collector waking up threads that are sleeping
                    infinite
    Classification: Mono
           Product: Mono: Runtime
           Version: 2.10.x
          Platform: i386
        OS/Version: Other
            Status: NEW
          Severity: Major
          Priority: P5 - None
         Component: misc
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: michael.jaskiewicz at gdc4s.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: Third Party Developer/Partner
           Blocker: No


I build and compile the program below. I'm running it on Red Hat Enterprise
Linux 5.3 with Mono 2.10.1. 

What I'm doing is creating 10 threads that all sleep for an infinite amount of
time. These threads are woken up by the main thread which calls Interrupt() on
each of the children threads. 

What I'm expecting is that each worker thread throws an exception which I catch
and prints a log. This isn't happening all the time. Sometimes threads make the
Sleep call and seem to just continue executing right after that. No exception
is then fired. Since I wrap the sleep in a tight loop, it goes through this
many, many times.

I discovered that if I set the GC_DONT_GC environment variable, the problem
goes away. 

Sincerely,
mj



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace SleepTest
{
  class Program
  {
    private static int s_threadID = 0;
    private static int s_threadCount = 2;
    private static List<Thread> s_threads = new List<Thread>();
    private static List<ManualResetEvent> s_resetEvents = new
List<ManualResetEvent>();
    private static object s_lock = new object();

    private static void ThreadProc()
    {
      ManualResetEvent mre = null;
      int threadID = 0;

      lock (s_lock)
      {
        threadID = s_threadID++;
        mre = s_resetEvents[threadID];
      }

      while (true)
      {        
        try
        {
          Console.WriteLine("{0}: Pre", threadID);
          Thread.Sleep(Timeout.Infinite);
          //mre.WaitOne();
          //mre.Reset();
          Console.WriteLine("{0}: Post", threadID);
        }
        catch (Exception e)
        {
          try
          {
            Console.WriteLine("{0}: Exception", threadID);
          }
          catch { }
        }
      }
    }

    static void Main(string[] args)
    {
      for (int i = 0; i < s_threadCount; i++)
      {
        Thread t = new Thread(new ThreadStart(ThreadProc));
        s_threads.Add(t);
        ManualResetEvent mre = new ManualResetEvent(false);
        s_resetEvents.Add(mre);
        t.Start();
      }

      Random r = new Random();
      while (true)
      {
        int index = r.Next(0, s_threadCount-1);
        //ManualResetEvent mre = s_resetEvents[index];
        Console.WriteLine("Main: signaling {0}", index);
        //mre.Set();
        Thread.SpinWait(100000);
        Thread t = s_threads[index];
        t.Interrupt();
      }

    }
  }
}

-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list