Skip to content

stp.WaitForIdle() does not work (rare) #32

@vnayak-mdsol

Description

@vnayak-mdsol

SmartThreadPool v2.2.3

This finding is very rare, I've encountered this problem only a couple of times so far. Therefore, It's very hard to reproduce.

On some occasions the WaitForIdle doesn't wait with callbacks on the queue. It seems like the ManualResetEvent was Set() or the underlying state changed somehow when it shouldn't have been.

To simulate this problem uncomment the call to StpChangeState().

The behavior with StpChangeState() uncommented is consistent with what I'm occasionally encountering.

Any idea what may be causing this? Has this problem been reported before?


         using (var stp = new SmartThreadPool(new STPStartInfo
                       { MinWorkerThreads = 0, MaxWorkerThreads = 2, StartSuspended = false }))
            {
                do
                {
                    stp.QueueWorkItem(JobOne, stp);

                    stp.QueueWorkItem(JobTwo, stp);

                    Thread.Sleep(20);
                    
                    // Uncomment this line to simulate the problem
                    // StpChangeState(stp); 

                    stp.WaitForIdle();

                } while (stp.InUseThreads > 0);
            }

            Console.WriteLine("Enter any key to close");
            Console.ReadKey();
        }

        private static void JobOne(SmartThreadPool obj)
        {
            Thread.Sleep(TimeSpan.FromSeconds(10));
            Console.WriteLine(MethodBase.GetCurrentMethod());
        }

        private static void JobTwo(SmartThreadPool obj)
        {
            Thread.Sleep(TimeSpan.FromSeconds(10));
            Console.WriteLine(MethodBase.GetCurrentMethod());
        }
        
        private static void StpChangeState(SmartThreadPool stp)
        {
            FieldInfo field = stp.GetType().GetField("_isIdleWaitHandle", BindingFlags.NonPublic | BindingFlags.Instance);
            var value = (ManualResetEvent)field?.GetValue(stp);
            value?.Set();
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions