0

I have an .NET MVC site which spins up child processes for doing background work. I'd like to ensure that those processes are shut down when IIS spins up a new app domain (e. g. on deployment or any change to Web.config).

For this purpose, I've created a CriticalFinalizerObject as follows:

public class ProcessHandle : CriticalFinalizerObject, IDisposable
    {
        private readonly int _processId;
        private int _disposed;

        public ProcessHandle(int processId)
        {
            this._processId = processId;
        }

        // dispose is called if we shut down the process normally, so
        // we don't need to kill it here
        public void Dispose()
        {
            if (Interlocked.Exchange(ref this._disposed, 1) == 0)
            {
                GC.SuppressFinalize(this);
            }
        }

        ~ProcessHandle()
        {
            if (Interlocked.Exchange(ref this._disposed, 1) == 0)
            {
                try
                {
                    using (var process = Process.GetProcessById(this._processId))
                    {
                        process.Kill();
                    }
                }
                catch
                {
                }
            }
        }
    }

Whenever I create a process, I store a ProcessHandle alongside. The hope is that when a new app domain spins up, IIS will unload the old app domain (after some timeout). This will run the finalizers for my handles, which will kill any processes.

However, I'm observing that changing Web.config does not seem to reliably cause the finalizers to run. What am I doing wrong? Is there another way to achieve this?

Note: I'd love to have the child process watch for the parent process, but I don't control the child process code. I could create a wrapper process for this purpose, but I was hoping not to need that extra layer of complexity.

ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152
  • I don't get the point of killing the process in the finaliser. – MatteoSp May 06 '15 at 18:24
  • @MatteoSp Otherwise, the process hangs around after the app restarts. Eventually, the system is full of processes that aren't doing anything – ChaseMedallion May 06 '15 at 18:26
  • But who (or what) should dispose your `ProcessHandle' instances? – MatteoSp May 06 '15 at 18:31
  • @MatteoSp: it's my understanding that CriticalFinalizerObjects get finalized on shutdown of the application (not stackoverflow or things like that of course) – ChaseMedallion May 07 '15 at 20:03
  • So you have code to force the shutdown of process, and you expect this code to be called when the process shutdown? Man, you really need to clarify the serie of event you expect to happen, I really can't understand. – MatteoSp May 07 '15 at 20:43

0 Answers0