Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Application Crashes during AppDomain.Unload [modified]

Application Crashes during AppDomain.Unload [modified]

Scheduled Pinned Locked Moved C#
help
10 Posts 5 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Benny_Lava
    wrote on last edited by
    #1

    Hi, I have a very strange problem, i have multiple AppDomains (plugin system). Now at one point i raise an event from one of the plugin domains and the main AppDomain catches the event and calls another function in a seperate thread that Unloads the calling AppDomain. The first thing that came to my mind is that the plugin domain somehow became the main application domain but it didn't i checked before the culprit line below for AppDomain.FriendlyName and it was different than the main application domain. code:

    private void UnloadDevicePluginDomain(DevicePlugin d)
    {
    Guid id = Guid.NewGuid();

            try {
                lock(locker\_) {
                    if(d == null)
                        return;
                    id = d.DevicePluginId;
                    if(d.PluginDomain != null) {
                        //--Just to be on the safe side call plugin Dispose before App domain unload
                        if(d.DeviceInterface != null) {
                            d.DeviceInterface.Dispose();
                        }
    
                        if(d.WatchDogTimer != null)
                            d.WatchDogTimer.Dispose();
    
                        d.PluginDomain.UnhandledException -= new UnhandledExceptionEventHandler(DevicePluginCrashed);
    
                        AppDomain.Unload(d.PluginDomain);   //THE WHOLE APPLICATION CRASHES AT THIS LINE
                        devicePlugins\_.Remove(d);
                    }
                }
            } catch (Exception ex) {
               // DOES NOT COME TO THIS PART
            }
        }
    

    So the weird thing is that there is an exception handler and the line nonetheless crashes the whole application. Any help greatly appreciated. Thanks.

    -- Modified Wednesday, April 13, 2011 3:39 PM

    G N B J 4 Replies Last reply
    0
    • B Benny_Lava

      Hi, I have a very strange problem, i have multiple AppDomains (plugin system). Now at one point i raise an event from one of the plugin domains and the main AppDomain catches the event and calls another function in a seperate thread that Unloads the calling AppDomain. The first thing that came to my mind is that the plugin domain somehow became the main application domain but it didn't i checked before the culprit line below for AppDomain.FriendlyName and it was different than the main application domain. code:

      private void UnloadDevicePluginDomain(DevicePlugin d)
      {
      Guid id = Guid.NewGuid();

              try {
                  lock(locker\_) {
                      if(d == null)
                          return;
                      id = d.DevicePluginId;
                      if(d.PluginDomain != null) {
                          //--Just to be on the safe side call plugin Dispose before App domain unload
                          if(d.DeviceInterface != null) {
                              d.DeviceInterface.Dispose();
                          }
      
                          if(d.WatchDogTimer != null)
                              d.WatchDogTimer.Dispose();
      
                          d.PluginDomain.UnhandledException -= new UnhandledExceptionEventHandler(DevicePluginCrashed);
      
                          AppDomain.Unload(d.PluginDomain);   //THE WHOLE APPLICATION CRASHES AT THIS LINE
                          devicePlugins\_.Remove(d);
                      }
                  }
              } catch (Exception ex) {
                 // DOES NOT COME TO THIS PART
              }
          }
      

      So the weird thing is that there is an exception handler and the line nonetheless crashes the whole application. Any help greatly appreciated. Thanks.

      -- Modified Wednesday, April 13, 2011 3:39 PM

      G Offline
      G Offline
      gavindon
      wrote on last edited by
      #2

      I don't have an answer for ya but to save you from the pros slapping your wrist. put the code in a code block so they can read it easier.. and then they wont have to focus their answer on your code not being in a block :-D

      Programming is a race between programmers trying to build bigger and better idiot proof programs, and the universe trying to build bigger and better idiots, so far... the universe is winning.

      B 1 Reply Last reply
      0
      • B Benny_Lava

        Hi, I have a very strange problem, i have multiple AppDomains (plugin system). Now at one point i raise an event from one of the plugin domains and the main AppDomain catches the event and calls another function in a seperate thread that Unloads the calling AppDomain. The first thing that came to my mind is that the plugin domain somehow became the main application domain but it didn't i checked before the culprit line below for AppDomain.FriendlyName and it was different than the main application domain. code:

        private void UnloadDevicePluginDomain(DevicePlugin d)
        {
        Guid id = Guid.NewGuid();

                try {
                    lock(locker\_) {
                        if(d == null)
                            return;
                        id = d.DevicePluginId;
                        if(d.PluginDomain != null) {
                            //--Just to be on the safe side call plugin Dispose before App domain unload
                            if(d.DeviceInterface != null) {
                                d.DeviceInterface.Dispose();
                            }
        
                            if(d.WatchDogTimer != null)
                                d.WatchDogTimer.Dispose();
        
                            d.PluginDomain.UnhandledException -= new UnhandledExceptionEventHandler(DevicePluginCrashed);
        
                            AppDomain.Unload(d.PluginDomain);   //THE WHOLE APPLICATION CRASHES AT THIS LINE
                            devicePlugins\_.Remove(d);
                        }
                    }
                } catch (Exception ex) {
                   // DOES NOT COME TO THIS PART
                }
            }
        

        So the weird thing is that there is an exception handler and the line nonetheless crashes the whole application. Any help greatly appreciated. Thanks.

        -- Modified Wednesday, April 13, 2011 3:39 PM

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        Please format your code using the code block toolbar item. Have you tried not removing the UnhandledException handler to see if the Exception is caught. Don't know if this is useful, it's an old article Application Suite Template[^]


        I know the language. I've read a book. - _Madmatt

        B 1 Reply Last reply
        0
        • B Benny_Lava

          Hi, I have a very strange problem, i have multiple AppDomains (plugin system). Now at one point i raise an event from one of the plugin domains and the main AppDomain catches the event and calls another function in a seperate thread that Unloads the calling AppDomain. The first thing that came to my mind is that the plugin domain somehow became the main application domain but it didn't i checked before the culprit line below for AppDomain.FriendlyName and it was different than the main application domain. code:

          private void UnloadDevicePluginDomain(DevicePlugin d)
          {
          Guid id = Guid.NewGuid();

                  try {
                      lock(locker\_) {
                          if(d == null)
                              return;
                          id = d.DevicePluginId;
                          if(d.PluginDomain != null) {
                              //--Just to be on the safe side call plugin Dispose before App domain unload
                              if(d.DeviceInterface != null) {
                                  d.DeviceInterface.Dispose();
                              }
          
                              if(d.WatchDogTimer != null)
                                  d.WatchDogTimer.Dispose();
          
                              d.PluginDomain.UnhandledException -= new UnhandledExceptionEventHandler(DevicePluginCrashed);
          
                              AppDomain.Unload(d.PluginDomain);   //THE WHOLE APPLICATION CRASHES AT THIS LINE
                              devicePlugins\_.Remove(d);
                          }
                      }
                  } catch (Exception ex) {
                     // DOES NOT COME TO THIS PART
                  }
              }
          

          So the weird thing is that there is an exception handler and the line nonetheless crashes the whole application. Any help greatly appreciated. Thanks.

          -- Modified Wednesday, April 13, 2011 3:39 PM

          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #4

          Now at one point i raise an event from one of the plugin domains and the main AppDomain catches the event and calls another function in a seperate thread that Unloads the calling AppDomain.

          That sounds like the problem to me. What's probably happening is the domain is getting unloaded while its event handler is still running, and that's confusing the CLR at a level below your exception handling. (What do you mean by 'crash', by the way?) If you want plugins to be able to unload themselves, I think you will have to set a timer in that event handler so that the plugin domain is no longer doing stuff when you try to unload it. (And obviously, once the plugin raises that event, it should also kill any threads it spawned and not do any more processing, since it's issuing a suicide note by raising it.)

          B 1 Reply Last reply
          0
          • B Benny_Lava

            Hi, I have a very strange problem, i have multiple AppDomains (plugin system). Now at one point i raise an event from one of the plugin domains and the main AppDomain catches the event and calls another function in a seperate thread that Unloads the calling AppDomain. The first thing that came to my mind is that the plugin domain somehow became the main application domain but it didn't i checked before the culprit line below for AppDomain.FriendlyName and it was different than the main application domain. code:

            private void UnloadDevicePluginDomain(DevicePlugin d)
            {
            Guid id = Guid.NewGuid();

                    try {
                        lock(locker\_) {
                            if(d == null)
                                return;
                            id = d.DevicePluginId;
                            if(d.PluginDomain != null) {
                                //--Just to be on the safe side call plugin Dispose before App domain unload
                                if(d.DeviceInterface != null) {
                                    d.DeviceInterface.Dispose();
                                }
            
                                if(d.WatchDogTimer != null)
                                    d.WatchDogTimer.Dispose();
            
                                d.PluginDomain.UnhandledException -= new UnhandledExceptionEventHandler(DevicePluginCrashed);
            
                                AppDomain.Unload(d.PluginDomain);   //THE WHOLE APPLICATION CRASHES AT THIS LINE
                                devicePlugins\_.Remove(d);
                            }
                        }
                    } catch (Exception ex) {
                       // DOES NOT COME TO THIS PART
                    }
                }
            

            So the weird thing is that there is an exception handler and the line nonetheless crashes the whole application. Any help greatly appreciated. Thanks.

            -- Modified Wednesday, April 13, 2011 3:39 PM

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            unix_master01 wrote:

            // DOES NOT COME TO THIS PART

            AppDomains, all of them even the main one, have an UnhandledException. You should always set that because unhandled exceptions terminate the domain or even the application. And that is your last chance to at least try to report what the exception was.

            1 Reply Last reply
            0
            • N Not Active

              Please format your code using the code block toolbar item. Have you tried not removing the UnhandledException handler to see if the Exception is caught. Don't know if this is useful, it's an old article Application Suite Template[^]


              I know the language. I've read a book. - _Madmatt

              B Offline
              B Offline
              Benny_Lava
              wrote on last edited by
              #6

              Hi, Thanks for your reply, i'll try removing the UhandledException unsubscription. P.S Sorry i didn't put the code in a code block, I know about it just forgot to use it :)

              1 Reply Last reply
              0
              • B BobJanova

                Now at one point i raise an event from one of the plugin domains and the main AppDomain catches the event and calls another function in a seperate thread that Unloads the calling AppDomain.

                That sounds like the problem to me. What's probably happening is the domain is getting unloaded while its event handler is still running, and that's confusing the CLR at a level below your exception handling. (What do you mean by 'crash', by the way?) If you want plugins to be able to unload themselves, I think you will have to set a timer in that event handler so that the plugin domain is no longer doing stuff when you try to unload it. (And obviously, once the plugin raises that event, it should also kill any threads it spawned and not do any more processing, since it's issuing a suicide note by raising it.)

                B Offline
                B Offline
                Benny_Lava
                wrote on last edited by
                #7

                Hi, That sounds like the problem to me. What's probably happening is the domain is getting unloaded while its event handler is still running, and that's confusing the CLR at a level below your exception handling. Yes, that crossed my mind the minute the bug appeared, but the event handler adds the method to a method execution queue I designed and that queue is run in a separate thread, so the event handler 100% exits. By crash I mean when i'm in debugging mode after i press step over over the AppDomain.Unload the debugging session ends and the application literally crashes... :) The plugins do not unload themselves, a dedicated method execution queue runned by a thread that is created in the main AppDomain terminates them by processing the method I posted in the original post. Thank you for your quick reply.

                B 1 Reply Last reply
                0
                • G gavindon

                  I don't have an answer for ya but to save you from the pros slapping your wrist. put the code in a code block so they can read it easier.. and then they wont have to focus their answer on your code not being in a block :-D

                  Programming is a race between programmers trying to build bigger and better idiot proof programs, and the universe trying to build bigger and better idiots, so far... the universe is winning.

                  B Offline
                  B Offline
                  Benny_Lava
                  wrote on last edited by
                  #8

                  Hehe, Thanks, I stand corrected and my post also... :)

                  1 Reply Last reply
                  0
                  • B Benny_Lava

                    Hi, That sounds like the problem to me. What's probably happening is the domain is getting unloaded while its event handler is still running, and that's confusing the CLR at a level below your exception handling. Yes, that crossed my mind the minute the bug appeared, but the event handler adds the method to a method execution queue I designed and that queue is run in a separate thread, so the event handler 100% exits. By crash I mean when i'm in debugging mode after i press step over over the AppDomain.Unload the debugging session ends and the application literally crashes... :) The plugins do not unload themselves, a dedicated method execution queue runned by a thread that is created in the main AppDomain terminates them by processing the method I posted in the original post. Thank you for your quick reply.

                    B Offline
                    B Offline
                    BobJanova
                    wrote on last edited by
                    #9

                    I'm not sure if you can unload a plugin from a different thread to the one that spawned it, come to think of it. Running a queue in a separate thread is actually a really good way of having the unload happen before it should, unless you're applying a delay or doing some cross-domain interlocking. I also agree with the other poster who said that you could try not unhooking the UnhandledException event handler. It should go out of scope and disappear anyway when the plugin reference is lost.

                    B 1 Reply Last reply
                    0
                    • B BobJanova

                      I'm not sure if you can unload a plugin from a different thread to the one that spawned it, come to think of it. Running a queue in a separate thread is actually a really good way of having the unload happen before it should, unless you're applying a delay or doing some cross-domain interlocking. I also agree with the other poster who said that you could try not unhooking the UnhandledException event handler. It should go out of scope and disappear anyway when the plugin reference is lost.

                      B Offline
                      B Offline
                      Benny_Lava
                      wrote on last edited by
                      #10

                      Hi, After a session of debugging using all advices I collected, here are my findings (So everybody can learn from my mistakes... :) ) -A plugin can be unloaded from a different thread (other thread then the one that spawned it -The problem was that I also handled UnhandledException event of the AppDomain, and a series of unfortunate events happened: *I have 2 mechanisms for protecting my core system from plugins: 1. A watchdog timer mechanism that terminates a plugin if the plugin hasn't responded (raised an watch dog timer event) for a certain period of time 2.A UnhandledException event handler for when an unhandled error occurs in the plugin, and in that event i call UnloadPlugin function and remove the plugin from the core system So a plugin spawned an unhandled exception the same time the watchdog timer called UnloadPlugin function... And in the UnhandledException handler it tried to use data and unload an already unloaded AppDomain So the solution is to develop some kind of synchronization mechanism so if one request for termination comes that no other request are processed. So the bottom line is "Threads are evil" ;) Thanks for your help.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups