Is a web application expected to be regularly restarted?

Hi,

I have a web application, which is always running. Among its tasks, it monitors a specific emails mailbox; every 15 minutes, it checks whether there are messages corresponding to a set of rules, handles them, and moves them to another mailbox.

What I’m finding is that the app usually runs fine for several weeks only. It’s a somewhat complex app which I can’t entirely debug in real conditions, so I’m not sure what the problem is.
Once the app ceases to work (by this, I mean it won’t handle new emails anymore, but the other functions still work), I can kill and relaunch the app and it will work again for some weeks.
The server doesn’t seem to be much loaded when the trouble occurs, so I’m not sure of the true issue. Should it be expected that a web app must be relaunched at specific intervals?

I’ve some not frequent errors in a web app like you.
I added this event in the App class
you will be able to see when there are silent exceptions when and where they came from.

Function UnhandledException(error As RuntimeException) Handles UnhandledException as Boolean
  // Timestamp + error info
  Var now As DateTime = DateTime.Now
  Var msg As String = now.SQLDateTime + " | Error " + Error.ErrorNumber.ToString + ": " + Error.Message
  
  dim s() as String = error.Stack
  If s <> Nil and ubound(s)>=0 Then
    msg = msg + EndOfLine + "  " + String.FromArray(s, EndOfLine)
  End If
  
  // Print to terminal stdout
  Print(msg)
  
  // Append to error.log next to the executable
  //AppendToErrorLog(msg)
  Var logFile As FolderItem = App.ExecutableFile.Parent.Child("error.log")
  Try
    Var bs As BinaryStream = BinaryStream.Open(logFile, True)
    bs.Position = bs.Length
    bs.Write(msg + EndOfLine)
    bs.Close
  Catch
    Try
      Var tos As TextOutputStream = TextOutputStream.Create(logFile)
      tos.Write(msg + EndOfLine)
      tos.Close
    Catch
    End Try
  End Try
  
  ' prevent the app to quit
  Return True
  
End Function

Thanks for your answer.
I have already filled this event, where I can only see one NilObjectException happening “recently” (on the 29th of September). I’ll keep watching that.

The question is whether the eventual failure is due to something in the Xojo framework versus something changing to an unexpected state in your own code.

If it was a framework issue it would likely be happening to a lot more people. That doesn’t necessarily rule it out but I’d wager that something in your code (or perhaps a third party component you’re using) is probably more likely.

If you are running multiple instances of your web app you might be able to design a scheduled restart of each instance without causing any downtime, but this would really just be a band-aid versus finding and fixing the actual problem.

Is a web application expected to be regularly restarted?

Yes, there are several posts already, not everyone has this problem but it looks like some kind of memory leak that depends on the number of users, traffic, etc.

Easier to just add a task to your server to restart the app every week or less

I tend to agree with you; my guess is some kind of memory leak, but it’s still possible, albeit unlikely, that it’s a framework bug which my app is using more often than most other apps, thus went unnoticed.

Granted. I’m not even sure it’s some kind of leak; if it’s not, scheduling to restart the web app at some weeks of interval won’t solve the issue at all.
On the other hand, it’s perhaps worth giving it a try.

Not easier for me :sweat_smile:
I chose Xojo also because I’m not a Linux guy; handling servers isn’t my cup of tea.
But I’m wondering if simply calling WebApplication.Quit (or the Quit shortcut) would just work… I’ll check.

Thanks for your answers.

The excellent Lifeboat from Strawberry software will let you schedule app restarts easily without being a Linux/unix person. It does much else besides and takes all the pain out of running your own server(s).

I believe he also - someone on here - has a web app status monitor that will trigger a restart if functionality decreases or stops.

NOTE: for everyone else wanting to EDIT to correct something by strikethrough-and-add… wrap a phrase in double-tilde to create the strikethrough effect. For italics, a single asterisk is what you wrap italicized text in. A double tilde looks like this: ~~

You can also set a number of instances of an app to allow the server to start or run. Another cool feature is it can allow customized sub-domains and error pages.

I have had a web email app running for many months under Lifeboat without issue. In my case it is not Timer-based, but I use a URLConnection (from any Desktop, Web or Mobile app) with a parameter to get it to check for new emails and send all unsent emails. It never creates a Web Session, as it checks and sends the emails entirely in the URLHandler Event then exits.

I’m not sure if this is helpful for you.

And I’m already using Lifeboat quite a lot… just wasn’t aware of this option.
Thank you; I’ll check that.

Hello @Arnaud_N,

I have some long running Xojo web apps.

I normally start from the assumption the framework is working well, and that it is something I’ve done - which is how things normally play out.

Exception handling, unhandled exceptions and resource connection management are all things to explore. Maybe, refreshing timers as well.

Better logging on web helps as well, see Log4Xojo: A more powerful way to manage your app logging – Xojo Programming Blog which was helpful in spotting slow downs.

Most of us tend to code in two styles:

First pass, “vibe coding” / “outcome coding” / “burst coding” - new feature, proof of concept etc (fun).

Second pass “coding for resilience” - yep, code hardening, this feature must work as expected, always (a grind).

More recently, I’ve leaned on AI to check the resilience of some code - which is really impressive in making tangible improvement. It’s definitely improved my coding as well.

Good luck!

Kind regards, Andrew

We have many of these “monitoring” apps that handle various checks etc, and we use a web app running as a Windows service. What we do is set the timer off on the App.Opening event and all the processes run at the Application level. We also have a form that can be launched to monitor what is happening at the application level but that is not required for the processes to run. These all run without issue for months at a time. Give that a go!

As per previous posts, your error trapping paradigm also needs to be very robust!

Thanks for your answers; greatly appreciated.

I also got an answer from Tim Parnell (the developper of Lifeboat), whom I thank, too.
As he said, and like other users here did too, the answer to the question of this thread is really “no”, which is reassuring (regarding Xojo, not my code :sweat_smile:).
Lifeboat has indeed a way to restart apps at given schedules. I’ll use that.

Of course, I’ll still monitor my app and try to fix this, but since it’s a rather complex one which I can’t debug in true conditions, I don’t have much hope in that regard.

Thank, all.

If you ever find what could be triggering the cause pushing you to restart the app, please open a bug report so we can fix it.

Using a coding agent may help, they are getting really good for finding this kind of issues affecting only a specific project under your very specific deployment circumstances.

Thanks for the suggestion. I’ve not yet tried coding agents; I think they’re paid services and I can’t afford that for now. But there are alternatives.
I’ll happily open a bug report if I find something, of course.

Claude Pro is only 20€ per month.

If this is running in MS-Windows I would suspect a memory leak in Windows, not necessarily your app. I’ve seen similar before and configured windows to automatically shutdown, reboot and restart the server nightly. Worked like a charm after that, for years.

It’s running on Linux, but the same may apply.
Thanks for your reply.

This was my solution to the same problem.