Search

List.Sort() throws InvalidOperationException (instead of re-throw ThreadAbortException) while thread is being aborted by Andrzej P

Closed
as Won't Fix Help for as Won't Fix

2
0
Sign in
to vote
Type: Bug
ID: 605536
Opened: 9/27/2010 4:27:15 AM
Access Restriction: Public
0
Workaround(s)
1
User(s) can reproduce this bug
List.Sort() throws InvalidOperationException (instead of re-throw ThreadAbortException) when thread is being aborted and ThreadAbortException occurs in Comparer while sorting.
Details (expand)

Visual Studio/Silverlight/Tooling version

Visual Studio 2010

What category (if any) best represents this feedback?

 

Steps to reproduce

Build and run following Hello Word Console application:

using System;
using System.Collections.Generic;
using System.Threading;

namespace HelloWordSortIssue
{
    class Item : IComparable<Item>
    {
        ManualResetEvent threadSortingStartedEvent;
        public int ID { get; private set; }

        public Item(int id, ManualResetEvent threadSortingStartedEvent)
        {
            this.ID = id;
            this.threadSortingStartedEvent = threadSortingStartedEvent;
        }

        public int CompareTo(Item other)
        {
            threadSortingStartedEvent.Set();
            System.Threading.Thread.Sleep(1); // ThreadAbortException will be thrown here
            if (other == null)
                return 1;
            return this.ID.CompareTo(other.ID);
        }
    }

    class Program
    {
        ManualResetEvent threadSortingStartedEvent = new ManualResetEvent(false);

        static void Main(string[] args)
        {
            new Program().Main();
        }

        void Main()
        {
            Thread thread = new Thread(this.ThreadProc);
            thread.Start();
            threadSortingStartedEvent.WaitOne(); // wait to start sorting
            thread.Abort();
            thread.Join();
        }

        void ThreadProc()
        {
            try
            {
                Random rand = new Random((int)DateTime.Now.Ticks);
                List<Item> items = new List<Item>();
                for (int i = 0; i < 1000; i++)
                    items.Add(new Item(rand.Next(), this.threadSortingStartedEvent));

                // List.Sort() will throw here System.InvalidOperationException instead of ThreadAbortException                
                items.Sort();
            }
            catch (ThreadAbortException)
            {
                Console.WriteLine("Thread aborted.");
            }
            catch (ThreadInterruptedException)
            {
                Console.WriteLine("Thread interrupted.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error in background thread: {0}", ex);
            }
        }
    }
}

Product Language

English

Operating System

Windows 7

Operating System Language

Polish

Actual results

InvalidOperationException thrown by List.Sort

Expected results

ThreadAbortException re-thrown by List.Sort
File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 11/22/2010 at 3:28 PM
Thanks for reporting this issue. I wanted to let you know that we have looked into this and have decided not to change the current behavior. If a Comparer throws an unexpected exception when List<T>.Sort is used, it throws InvalidOperationException and set the inner exception to the unexpected error (in this case ThreadAbortException). This behavior is by design. Also note that it is generally a bad idea to rely on Thread.Abort. Some more info is available here: http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation

Therefore, I've gone ahead and resolved this as Won't Fix. Please let me know if you have any questions or concerns at justinv at microsoft dot com.

Regards,

Justin Van Patten
CLR Base Class Libraries
Posted by Microsoft on 9/27/2010 at 5:01 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.