开发者

Bug in MVC3 - requests never time out. Works fine for aspx pages in same project

I'm seeing this on our production site as well as a small test site I setup just to test this out...

Basically, it appears that requests handled by mvc never time out. I've set an executionTimeout in my web.config and turned off debug mode. I've then added an infinite loop of thread.sleeps to both a regular aspx page and an mvc page (the loop is in the controller of the mvc page). The aspx page reliably times out (HttpException (0x80004005): Request timed out.), but the mvc page just spins forever without timing out.

Are there separate settings for mvc (I've looked but haven't found them)? Do mvc requests not timeout by default?

Any help on this would be appreciated. I'll gladly email out my small test site if it would help anyone out.

Edit: I'm using MVC3.

Contents of my web.config:

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Enabled" value="true" />
  </appSettings>

  <system.web>
      <httpRuntime maxRequestLength="16384" executionTimeout="30" />
      <compilation debug="false" targetFramework="4.0">
          <assemblies>
          <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
          <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
          <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
          <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E3开发者_如何学C5" />
          <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
          </assemblies>
      </compilation>

    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>

    <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </providers>
    </membership>

    <profile>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
      </providers>
    </profile>

    <roleManager enabled="false">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>

  </system.web>

  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>


I found the cause for this, methinks:

This method is in the WrappedAsyncResult class, which the MvcHandler class uses via BeginProcessRequest:

public static IAsyncResult BeginSynchronous<TResult>(AsyncCallback callback, object state, Func<TResult> func, object tag)
{
    BeginInvokeDelegate beginDelegate = delegate (AsyncCallback asyncCallback, object asyncState) {
        SimpleAsyncResult result = new SimpleAsyncResult(asyncState);
        result.MarkCompleted(true, asyncCallback);
        return result;
    };
    EndInvokeDelegate<TResult> endDelegate = _ => func();
    WrappedAsyncResult<TResult> result = new WrappedAsyncResult<TResult>(beginDelegate, endDelegate, tag);
    result.Begin(callback, state, -1);
    return result;
}

where "Begin" is:

public void Begin(AsyncCallback callback, object state, int timeout)
{
    bool completedSynchronously;
    this._originalCallback = callback;
    lock (this._beginDelegateLockObj)
    {
        this._innerAsyncResult = this._beginDelegate(new AsyncCallback(this.HandleAsynchronousCompletion), state);
        completedSynchronously = this._innerAsyncResult.CompletedSynchronously;
        if (!completedSynchronously && (timeout > -1))
        {
            this.CreateTimer(timeout);
        }
    }
    if (completedSynchronously && (callback != null))
    {
        callback(this);
    }
}

EDIT: have come up with a ham-handed way of forcing MVC controller actions to "time out", although the mechanism is a bit brutish:

public class TimeoutController : Controller
{
    private bool _isExecuting = false;
    private int _controllerTimeout = 5000;
    private Thread _executingThread;
    private readonly object _syncRoot = new object();

    protected override void ExecuteCore()
    {
        _executingThread = Thread.CurrentThread;
        ThreadPool.QueueUserWorkItem(o =>
            {
                Thread.Sleep(_controllerTimeout);
                if (_isExecuting)
                {
                    _executingThread.Abort();
                }
            });
        base.ExecuteCore();
    }

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        _isExecuting = true;
        base.OnActionExecuting(filterContext);
    }

    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        _isExecuting = false;                
        base.OnActionExecuted(filterContext);
    }

    public int ControllerTimeout
    {
        get
        {
            int retVal;
            lock(_syncRoot)
            {
                retVal = _controllerTimeout;
            }
            return retVal;
        }
        set
        {
            lock(_syncRoot)
            {
                _controllerTimeout = value;                    
            }
        }
    }
}


It should work when these conditions are met:

1) Domain name is not localhost (to test timeout you should use "YourComputerName" instead of "localhost").

2) Project is compiled in Release mode.

3) compilation debug="false"

if not look here for an alternative (ScriptTimeOut): ASP.NET MVC and httpRuntime executionTimeout

Greetings,
Daddy


Still happening for me in MVC 4. I have submitted this to microsoft as a bug:

https://connect.microsoft.com/VisualStudio/feedback/details/781171/asp-net-mvc-executiontimeout-does-not-work

Update:

Microsoft commented with the following:

The execution timeout feature is not advised to be used in MVC applications. You can instead set HttpContext.Server.ScriptTimeout to the desired timeout value. Despite the name, this is a per-request setting and should apply to any ASP.NET request (the name "script" is misleading)

.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜