Timers firing inconsistently when window loses focus

Godot Version

v4.3

Question

I am trying to create a dedicated server for my game using Godot and its networking API. I have separate projects for the client and server. On the server, I want to set up a health check ping to a discovery service (written in Go) using a simple HTTPRequest fired by a timer, say every 10 seconds. My scene tree looks like this:

Node [+script]
|
+- HTTPRequest
|
+- Timer

The Timer is set through the editor to fire every 10 seconds, with the autostart property enabled. In the script, I connect to its timeout signal and send the XHR request to the discovery service, passing the server ID.

Everything works fine until I switch to another window. Then, based on the logs from the discovery service, I notice that the health check requests are sent every 20 seconds, sometimes even longer. When I switch back to the editor window, it returns to the expected behavior. I also print timestamps in the debug output and can confirm that the Timer is firing inconsistently with longer intervals when the window is not focused.

I assume this behavior is some kind of optimization, but considering the nature of my project, is there any way to disable it? Or should I approach this differently?

It is not optimization, it is how your OS works. It prioritizes resources for what you are currently focused on. If you want to prioritize a particular task there are steps you need to take to ensure that.

  1. What OS are you running this on?
  2. Are you running this server in headless mode?
  3. How are you starting your dedicated server?
  4. Have you read and are you following these instructions: Exporting for dedicated servers — Godot Engine (stable) documentation in English
  5. What is the reason you are trying to run a dedicated server on a desktop that you are actively using for other things?
2 Likes

Thank you! Indeed, it was OS. I’m on Fedora, and power mode was switched to “balanced”. After switching it to “performance” the issue disappeared. In case someone would end up here from web search, check the power mode of OS or throttling settings.

1 Like

That’s a partial solution. What @dragonforge-dev said about a dedicated server running in the background on a desktop is always going to face that kind of issues, even on Linux, whatever the power settings.

Once your CPU load gets to the point where your OS scheduler must prioritize something, it will usually be the desktop app in the foreground.

Thus the recommendations on headless, etc.

It works right now cause you most likely are not trying to do more per second than your CPU can handle. What are you going to do when a player of your game files a bug report about timers not working even though he is in performance mode ?

Food for thought,

Cheers !

1 Like