reactjs.net - 'potential stack overflow detected' 错误

reactjs.net - 'potential stack overflow detected' error

自从我决定从客户端 React 切换到服务器端渲染,我开始创建我的组件并在应用程序中使用它们。

但是我遇到了这个错误:

Unknown error (RangeError); potential stack overflow detected

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: Microsoft.ClearScript.ScriptEngineException: Unknown error (RangeError); potential stack overflow detected

这是 stack-trace

的一部分
[ScriptEngineException: Unknown error (RangeError); potential stack overflow detected]
   V8Exception.ThrowScriptEngineException(V8Exception* ) +169
   Microsoft.ClearScript.V8.V8ContextProxyImpl.Execute(String gcDocumentName, String gcCode, Boolean evaluate, Boolean discard) +462
   Microsoft.ClearScript.V8.<>c__DisplayClass1b.<Execute>b__19() +197
   Microsoft.ClearScript.ScriptEngine.ScriptInvoke(Func`1 func) +70
   Microsoft.ClearScript.V8.V8ScriptEngine.BaseScriptInvoke(Func`1 func) +49
   Microsoft.ClearScript.V8.<>c__DisplayClass25`1.<ScriptInvoke>b__24() +45
   Microsoft.ClearScript.V8.?A0x792c8756.LockCallback(Void* pvArg) +9
   Microsoft.ClearScript.V8.V8ContextProxyImpl.InvokeWithLock(Action gcAction) +176
   Microsoft.ClearScript.V8.V8ScriptEngine.ScriptInvoke(Func`1 func) +118
   Microsoft.ClearScript.V8.V8ScriptEngine.Execute(String documentName, String code, Boolean evaluate, Boolean discard) +118
   JavaScriptEngineSwitcher.V8.V8JsEngine.InnerEvaluate(String expression) +89

所以我不知道是什么导致了这个错误,但我认为这是一些循环中的代码或类似的东西。此外,如果我刷新页面,这个错误就会消失,如果我继续密集刷新,它会再次出现,这非常令人沮丧。

这是一个已知问题,请参阅 https://github.com/reactjs/React.NET/issues/190

解决方法是不使用 V8 进行渲染,即:

app.UseReact(config =>
        {
            config
                // ..other configuration settings
                .SetAllowMsieEngine(true);
        });

我遇到了同样的错误(天蓝色的网络应用程序),经过一些调查和测试,将 SetAllowMsieEngine 设置为 true 实际上也解决了我的问题。

正如 Luke McGregor 所说,这似乎是 V8ScriptEngine 的一个问题,使用 SetAllowMsieEngine 可以完成这项工作,但是这种方法是 react.net 的最新版本,已被弃用,建议 "managed in the JavaScriptEngineSwitcher configuration" 使用。

所以目前我找到的解决方案是通过直接在代码中设置来切换js引擎切换器:

JsEngineSwitcher engineSwitcher = JsEngineSwitcher.Instance;
engineSwitcher.EngineFactories
    .AddChakraCore()
    .AddMsie( new MsieSettings() { EngineMode = JsEngineMode.Auto } );

engineSwitcher.DefaultEngineName = ChakraCoreJsEngine.EngineName;

就像这样,我使用的是 ChakraCore 引擎,而不是导致错误的默认 V8。

到目前为止,在我们对大约 250 个并发请求进行性能测试期间,我们不再遇到此错误,而以前在相同条件下肯定会发生此错误。