0

I have an C# app which works fine for a long time. But suddenly it starts throw ThreadAbortException. The error message is that:

Error when logging System.Threading.ThreadAbortException: Thread was being aborted. at System.Threading.Monitor.Enter(Object obj) at XXX.getLogContent() in XXX\SynchronizedQueue.cs:line 71 at XXX.SynchronizedQueue.waitForNextContent() in XXX\SynchronizedQueue.cs:line 51 at XXX.ThreadLogger.logThreadDB() in XXX.ThreadLogger.cs:line 97

At first I thought it is IIS issue, we have reset the recycle pool, but it does not help. And there is sufficient disk space for log. The code (line 51 and 71 is marked) is:

 public Queue<LogContent> waitForNextContent()
    {
        Queue<LogContent> lCurrentLogContent;
        while (true)
        {
            lCurrentLogContent = getLogContent();  # line 51
            if (lCurrentLogContent.Count == 0)
            {
                //wait
                lock (mThreadMonitorLock)
                {
                    Monitor.Wait(mThreadMonitorLock);
                }
            }
            else
            {
                break;
            }
        }
        return lCurrentLogContent;
    }

private Queue<LogContent> getLogContent()
    {
        Queue<LogContent> lLogContent = new Queue<LogContent>();
        lock (mQueueLock)         # line 71
        {
            while (mLogContentQueue.Count > 0)
            {
                lLogContent.Enqueue(mLogContentQueue.Dequeue());
            }
        }
        return lLogContent;
    }

It seems it is related to C# lock(). But why it works fine in the past? Any help is greatly appreciated. Thanks.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Yuanfei Bi
  • 51
  • 7
  • Possible duplicate of [Why am i getting "Thread was being aborted" in asp.net?](https://stackoverflow.com/questions/7629986/why-am-i-getting-thread-was-being-aborted-in-asp-net) – 500 - Internal Server Error Nov 22 '18 at 16:36
  • 1
    That's not caused by `lock`. It occurs because the application or the thread is getting terminated. If the thread is blocked by `lock`, that's where the abort will surface. Is this a long-running background thread in a Web application perhaps? In this case the application never worked reliably. It was only a matter of time until that background thread got killed for any number of reasons. Check [How to run Background Tasks in ASP.NET](https://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx) – Panagiotis Kanavos Nov 22 '18 at 16:36
  • @PanagiotisKanavos, it is a long-running app hosted in IIS. Although the way I am running it is not reliable, I should be able to make it work at least. But now I keeps throwing exception. Why? – Yuanfei Bi Nov 22 '18 at 17:13
  • 1
    @YuanfeiBi because it's not reliable. The linked article explains why - if ASP.NET and IIS don't know about background threads and tasks, eventually they kill them. An application pool will be recycled periodically, also killing any threads. If such a background thread throws, it terminates the app pool. The article explains how to use background tasks properly – Panagiotis Kanavos Nov 22 '18 at 17:21
  • Locking and long running app on IIS are both bad ideas. That’s why you can find tons of posts on SO. Try to move them out to another proper application (like Windows service) and use lock free approaches in the long term. – Lex Li Nov 23 '18 at 00:19

0 Answers0