PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 4, 2022

[FIXED] what happens to ASP.NET exceptions from created non-threadpool thread?

 August 04, 2022     asp.net, c#, exception, multithreading     No comments   

Issue

In ASP.NET web application a worker thread creates a non-threadpool thread like the below:

private static bool _refreshInProcess = false;
public delegate void Refresher();
private static Thread _refresher;

private static void CreateAndStartRefreshThread(Refresher refresh)
{
    _refresher = new Thread(new ThreadStart(refresh));
    _refresher.Start();
}

private static void Refresh()
{
    LoadAllSystemData();
}

static public void CheckStatus()
{
    DateTime timeStamp = DateTime.Now;
    TimeSpan span = timeStamp.Subtract(_cacheTimeStamp);
    if (span.Hours >= 24)
    {
        if (Monitor.TryEnter(_cacheLock))
        {
            try
            {
                if (!_refreshInProcess)
                {
                    _refreshInProcess = true;
                    CreateAndStartRefreshThread(Refresh);
                }
            }
            finally
            {
                Monitor.Exit(_cacheLock);
            }
        }
    }
}

static public void LoadAllSystemData()
{
    try
    {
        if (!Database.Connected)
        {
            if (!OpenDatabase())
              throw new Exception("Unable to (re)connect to database");  
        }
        SystemData newData = new SystemData();
        LoadTables(ref newData);
        LoadClasses(ref newData);
        LoadAllSysDescrs(ref newData);
        _allData = newData;
        _cacheTimeStamp = DateTime.Now; // only place where timestamp is updtd 
    }
    finally
    {
        _refreshInProcess = false;
    }
}

and LoadAllSystemData() is also called elsewhere from the same lock-guarded section as CheckStatus(). Both calls are in their try-blocks that also have catch-block.

Now my questions are

  1. If LoadAllSystemData throws an exception when it's called from a non-threadpool thread in method Refresh, what will happen? Nobody can catch it.

  2. What happens when it's done 1000 times? Are these exceptions stored somewhere and thus stress the system and ultimately crash it due to memory exhaustion or something?

  3. Is there a good solution to catch them without waiting in the creating thread pool thread for the created thread to finish?

Thanks a lot!


Solution

  1. If exception is raised in the background non-threadpool thread and eventually it is not handled by any catch block, it is propagated up until it reaches beginning of the stack. Then thread finishes its execution - exception is not stored anywhere. Then the thread is dropped as well as its stack containing reference to the exception.
  2. Exception throwing is expensive in .NET so if you anticipate 1000 times it to occur, probably it is not an exception but just a part of your application flow and you should not raise an exception here.
  3. If you want to catch the exceptions from background activity, as an option you can use delegate's BeginInvoke / EndInvoke methods. If there is any exception in the background operation, it will be delivered in the EndInvoke call.


Answered By - Vitaliy Liptchinsky
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing