Viir / Kalmit

Moved to https://github.com/elm-fullstack/elm-fullstack
MIT License
2 stars 0 forks source link

Help avoid stack overflow ('Error: Out of stack space') #9

Open Viir opened 5 years ago

Viir commented 5 years ago

Today I saw the process host crashing on processing an event:

crit: Microsoft.AspNetCore.Hosting.Internal.WebHost[6]
      Application startup exception
JavaScriptEngineSwitcher.Core.JsRuntimeException: Error: Out of stack space
   at 1:1 -> undefined

Engine name: ChakraCoreJsEngine
Engine version: 1.11.5
Category: Runtime error
Description: Out of stack space
Type: Error
Line number: 1
Column number: 1
Source fragment: undefined ---> JavaScriptEngineSwitcher.ChakraCore.JsRt.JsScriptException: Script threw an exception.
   at JavaScriptEngineSwitcher.ChakraCore.JsRt.JsErrorHelpers.ThrowIfError(JsErrorCode error)
   at JavaScriptEngineSwitcher.ChakraCore.JsRt.JsContext.RunScript(String script, JsSourceContext sourceContext, String sourceUrl, JsParseScriptAttributes& parseAttributes)
   at JavaScriptEngineSwitcher.ChakraCore.ChakraCoreJsEngine.<>c__DisplayClass22_0.<InnerEvaluate>b__0()
   at JavaScriptEngineSwitcher.ChakraCore.ChakraCoreJsEngine.<>c__DisplayClass22_0.<InnerEvaluate>b__0()
   at JavaScriptEngineSwitcher.ChakraCore.ScriptDispatcher.<>c__DisplayClass10_0`1.<Invoke>b__0()
   at JavaScriptEngineSwitcher.ChakraCore.ScriptDispatcher.StartThread()
--- End of stack trace from previous location where exception was thrown ---
   at JavaScriptEngineSwitcher.ChakraCore.ScriptDispatcher.InnnerInvoke(Func`1 del)
   at JavaScriptEngineSwitcher.ChakraCore.ScriptDispatcher.Invoke[T](Func`1 func)
   at Kalmit.ProcessHostedWithChakraCore.EvaluateInJsEngineAndReturnResultAsString(String expressionJavascript) in K:\Source\Repos\Kalmit\implement\PersistentProcess\PersistentProcess.Common\Process.cs:line 92
   at Kalmit.ProcessHostedWithChakraCore.ProcessEvent(String serializedEvent) in K:\Source\Repos\Kalmit\implement\PersistentProcess\PersistentProcess.Common\Process.cs:line 66
   at Kalmit.PersistentProcess.PersistentProcessWithHistoryOnFileFromElm019Code..ctor(IProcessStoreReader storeReader, Byte[] elmAppFile) in K:\Source\Repos\Kalmit\implement\PersistentProcess\PersistentProcess.Common\PersistentProcess.cs:line 106
   at Kalmit.PersistentProcess.WebHost.Startup.BuildPersistentProcess(IServiceProvider services) in K:\Source\Repos\Kalmit\implement\PersistentProcess\PersistentProcess.WebHost\Startup.cs:line 86

As far as I understand this case, I used elm code which caused a large stack depth, leading to the crash.

For such cases, it would be helpful to have the framework report the stack in the javascript engine.

Also, maybe the javascript engine can be configured to support a larger stack.

(In this case, I worked around the problem by replacing Sha256.sha256 from billstclair/elm-sha256 with another hash function (SHA1.fromString from TSFoster/elm-sha1).)

As an application developer, given the error message of 'Out of stack space', I can already understand that I added code which is causing a stack overflow. But I also want to know: Which part of the code is involved here, which functions are involved?