« MBS Plugin Advent cal… | Home | Parallel network tran… »

Xojo Web signal handling

For a Xojo web project, the framework registers signal handlers to catch various signals from the OS. This seems to include at least KILL and INT signals, which are used to force quit an application or interrupt it. In both cases Xojo framework quits the application.

Let's check the event order:

App.Opening
App.Stopping: False
App.Closed
App.Stopping: False

Looks confusing?
Yes, it is. So the app launches and we log the call to Opening event. Then the web app runs and waits for work. Then the signal happens and a signal handler is invoked. We made a stack trace by raising an exception and got this:

RuntimeRaiseException
App.Event_Stopping%%ob
WebApplication._AppSignalHandler%%oi8
XojoCloud._SignalHandling.HandleSignal%%i8
_sigtramp
_pthread_cond_wait
_ZN4xojo17ConditionVariable7WaitForERNS_10UniqueLockENSt3__16chrono8durationIxNS3_5ratioILl1ELl1000EEEEE
_Z13SleepToSysteml
_Z25UnsafeDetachCurrentThreadv
_ZN20ThreadBackgroundTask11PerformTaskEv
ConsoleApplication.DoEvents%%oi8
WebApplication.Event_Run%i8%oA1s
_Z36CallConsoleApplicationRunEventHelperv
ConsoleApplication._CallFunctionWithExceptionHandling%%op
_Z33CallFunctionWithExceptionHandlingPFvvE
RuntimeRun
REALbasic._RuntimeRun
_Main
main

You see that the Xojo app runs and comes to the _pthread_cond_wait system method, which means the web app waits for something to happen with the application. Like some other thread runs and wakes this one due to new work coming in, like a new connection. The signal handler from Xojo is called, which then forwards to the application and there calls Stopping event with parameter false. Once that happened, we get the Close event called and then the Stopping event is called again. We got a new stack trace there:

RuntimeRaiseException
App.Event_Stopping%%ob
WebApplication.Event_Stop%%ob
_Z15ApplicationQuitxh
RuntimeRun
REALbasic._RuntimeRun
_Main
main

As you see this time the stop event is called from Quit method, which is probably what the framework does to quit the application. You may need to be prepared for Stopping event running twice. And of course your web app may need to handle situations where the app may be killed at any time. Like using transactions when dealing with databases and making sure that either nothing happens or the change is saved.

Is this signal handling in the framework good? Well,if you need to catch the signals yourself, you could reset them using MBS Xojo Util Plugin if needed with our SignalHandlerMBS class. You could decide between default handler, a plugin provided handler setting a flag for you to query in a timer or raise an event. We can set default handlers like this:

Sub RestoreDefaultSignalHandlers() Call SignalHandlerMBS.SetDefaultHandler(2) Call SignalHandlerMBS.SetDefaultHandler(15) End Sub

And if you wonder how to catch the stack trace in the event, you can just run a few lines like this:

Dim StackTrace() As String Try Raise New RuntimeException Catch r As RuntimeException StackTrace = r.Stack End Try Break

This feature has been in the Xojo Web framework since 2020, but it needed three years for me to write about it.

Let us know if you have question or need help.

18 12 23 - 08:22