Windows – Will IIS finish pending requests on Stop-WebSite

iisiis-8powershellwindowswindows-server-2012

Lets' say my IIS8 hosted web site is up and running and happily serving requests. Now, from an elevated Powershell, I run:

Stop-WebSite <MyWebSite>

My assumption until recently was that IIS would stop serving new requests, finish up current ones, and then gracefully stop the web site. However, I suspect that this is not always the case: there's a code path in my app like this:

Log("Begin");
try
{
  DoSomething();  // can take a few seconds
}
finally
{
 Log("End");
}

Occasionally, there would be "Begin" entries in my log without matching "End". Any chance Stop-WebSite forcefully kills the host process?

Best Answer

When running this simple test:

int ms = 2000;

try
{
    Log("1");
    Thread.Sleep(ms);

    Log("2");
    Thread.Sleep(ms);

    Log("3");
    Thread.Sleep(ms);

    Log("4");
    Thread.Sleep(ms);

    Log("5");
}
finally
{

    Log("6");
}

and stopping the web site a second after I called the page, I always get all six log entries.

So, in general your (and mine) original assumption is correct, the running process is finished before the site is shut down.

Is it impossible that a certain request may not finish? What if it runs for hours? what if something goes wrong and the worker process hangs?

I would look for other reasons why your 'Log("End");' does not work.