Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server.ScriptTimeout setting global in scope?

Tags:

asp.net

In all my experience, both as a Classic ASP and ASP.NET deveoper, I've always understood that calls to set the Server.ScriptTimeout value were local in scope to the current request. In other words, calling Server.ScriptTimeout = 600 would set the processing time for the current request to 10 minutes. Subsequent or even concurrent requests to other resources will utilize the default setting for Server.ScriptTimeout.

Recently in a code review, I was informed that setting Server.ScriptTimeout to a value sets the processing time for every page in the site, until the application pool is recycled. The suggested "fix" was something like the following:

public class MyPage : Page {
  private const int desiredTimeout = 600;
  private int cachedTimeout;

  private void Page_Load(object sender, EventArgs e) {
    // cache the current timeout in a private store.
    cachedTimeout = Server.ScriptTimeout;
    Server.ScriptTimeout = desiredTimeout;
  }

  private void Page_Unload(object sender, EventArgs e) {
    // restore the previous setting for the timeout
    Server.ScriptTimeout = cachedTimeout;
  }
}

This seems strange to me, as a developer calling Server.ScriptTimeout = 1 in a page could bring down the site as every other page will only be allowed to process for one second. Additionally, this behaviour would affect any current requests that may occur between the current Page_Load and Page_Unload events - which seems like a concurrency nightmare.

To be thorough, however, I made a test harness consisting of two pages - Page One that sets Server.ScriptTimeout to some really high number and Page Two that simply displays the current value for Server.ScriptTimeout. No matter what value I set on Page One, Page Two always shows the default value. So, my test appears to verify that Server.ScriptTimeout is local in scope.

I did note that if my web.config has debug="true", Server.ScriptTimeout has no effect - and MSDN states this explicitly on their page. In this mode, all calls to read the value for Server.ScriptTimeout return an absurdly large number, no matter what I set it to.

So my question is, and to be absolutely sure that I am not missing something, is there an instance that setting a value for Server.ScriptTimeout affects the processing time for the entire site (global in scope), or is my belief valid that the effect is local only to the current context? I've Googled this question to no avail, and MSDN appears to be silent on the issue.

Any links and/or experiences - one way or another - will be greatly appreciated! Documentation covering this seems scarce, and I would appreciate any authoritative information.

like image 302
BradBrening Avatar asked Aug 11 '12 20:08

BradBrening


1 Answers

It indeed is request-specific:

public int ScriptTimeout
{
    get
    {
        if (this._context != null)
        {
            return Convert.ToInt32(this._context.Timeout.TotalSeconds, CultureInfo.InvariantCulture);
        }
        return 110;
    }
    [AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Medium)]
    set
    {
        if (this._context == null)
        {
            throw new HttpException(SR.GetString("Server_not_available"));
        }
        if (value <= 0)
        {
            throw new ArgumentOutOfRangeException("value");
        }
        this._context.Timeout = new TimeSpan(0, 0, value);
    }
}

where _context is HttpContext

like image 132
twoflower Avatar answered Oct 15 '22 14:10

twoflower