App not exiting after exception?

.NET obfuscator and automated error reporting

Moderators: Luke Jefferson, Alex.Davies, melvyn.harbour

App not exiting after exception?

Postby Uniwares_AS » Tue Jul 27, 2010 1:50 am

Something really strange seems to happen in this little commandline app: when the exception is thrown, the app does not terminate when processed with SA.

This is just a really simple app which should just do its job while providing feedback in the background on a separate thread. No big design, just a hack. I reduced the code to the minimum to repro the case.

Without SA the app exits as expected when the exception occurs. With SA it seems to terminate only the main thread.

Code: Select all
namespace test
{
   public class Program
   {
      private Thread dummy = new Thread(new ThreadStart(DummyDisplay));
      
      public static void Main(string[] args)
      {
         Program me = new Program();
         me.dummy.Start();
         throw new ArgumentException();

// continue program here
      }

      private static void DummyDisplay()
      {
         while(true)
         {
// do some processing, and then sleep for a long time
         }
      }
   }
}

Uniwares_AS
 
Posts: 132
Joined: Thu Oct 11, 2007 3:29 pm

Postby Brian Donahue » Wed Jul 28, 2010 9:12 am

Hello,

If you are using SmartAssembly's error reporting feature, there is an option to "continue" on error. Did you use that option?
Brian Donahue
 
Posts: 6669
Joined: Mon Aug 23, 2004 10:48 am

Postby Uniwares_AS » Wed Jul 28, 2010 1:36 pm

No, not using this option.
Uniwares_AS
 
Posts: 132
Joined: Thu Oct 11, 2007 3:29 pm

Postby Brian Donahue » Fri Jul 30, 2010 11:32 am

It must be something to do with the error reporting. .NET Runtime's default behavior is to exit the process on an unhandled exception and SA must be changing the code so that the exception is "handled" as far as .NET is concerned. If you think about it, it would be impossible for SA's exception handling to work if it allowed the runtime to terminate the whole process when an exception is thrown. But you would think that the SA exception handling code would exit the process after displaying the dialog.
Brian Donahue
 
Posts: 6669
Joined: Mon Aug 23, 2004 10:48 am

Postby Brian Donahue » Sat Jul 31, 2010 10:54 am

I think my theory has some validity. SmartAssembly error reporting implements a ThreadExceptionEventHandler, which overrides what the runtime would do (kill the process).

I'll have a look at the code on Monday try to work out what it's actually doing.
Brian Donahue
 
Posts: 6669
Joined: Mon Aug 23, 2004 10:48 am

Postby Paul.Martin » Mon Aug 02, 2010 10:14 pm

I think the problems is that thread you are creating is a foreground thread.

There are two types of threads in .NET; background threads will automatically get terminated when an application ends, whereas all foreground threads must exit before the application will exit.

Without SA the exception will reach to the JIT debugger which is the only thing that can end all of a processes threads instantly.
SA has to add the code to handle the exception otherwise it would fall to the JIT debugger and the user would get two error messages. The most that SA can do is tell the application to exit in every way possible. It could add some really nasty hacky code which went through and terminated all of the threads in the current process, but this wouldn't handle all of the tidying up (e.g. of unmanaged resources) of the other threads.

Only thing I can really suggest is to make the thread a background thread (thread.IsBackground = true), as usually you don't need a foreground thread.
Paul.Martin
 
Posts: 83
Joined: Wed Feb 03, 2010 11:20 am
Location: Cambridgeshire

Postby Uniwares_AS » Mon Aug 02, 2010 10:30 pm

True, i wont need a foreground thread. Still its a nasty trap to fall into, even if its not SA's fault. One of those cases where its hard to draw the line. But might be worth at least a warning in the user manual (or even at the settings page).
Uniwares_AS
 
Posts: 132
Joined: Thu Oct 11, 2007 3:29 pm

Postby Brian Donahue » Tue Aug 03, 2010 9:40 am

Magic! Thanks Paul!
Brian Donahue
 
Posts: 6669
Joined: Mon Aug 23, 2004 10:48 am


Return to SmartAssembly 5

Who is online

Users browsing this forum: No registered users and 0 guests