Kevin Coughlin. Blog

A gamemoded restart dropped the GameMode hold

In godot-xdg-portals, gamemoded, not the portal, holds GameMode registrations. A daemon restart dropped them, and request_game_mode() still returned true.

request_game_mode() records a pid after RegisterGame returns 0. A later call for that pid returns true and does not call the portal again. A portal restart still works that way: a second RegisterGame for a pid the daemon already knows is rejected. A gamemoded restart drops every registration. The facade still had the pid, and the game ran without GameMode until it exited. The change is godot-xdg-portals#51 (bf3e41c), which builds on the portal name watch in godot-xdg-portals#44. #49 had two options. This one takes the watch.

The portal name watch cannot see the daemon

#44 watches org.freedesktop.portal.Desktop. When that name is lost, cached capabilities drop and GameMode holds stay. gamemoded tracks the game process, so a portal restart does not drop them, and registering again would be rejected.

The daemon's own name is com.feralinteractive.GameMode. #44 does not watch it. Before this change the pid stayed in _game_mode_pids, so the next request returned before it reached the bus:

    if _game_mode_pids.has(resolved):
        return true

query_game_mode() asks the backend and does not read that dictionary. #49 says that call would report NOT_REGISTERED after the daemon drops the pid. request_game_mode() never asks.

Watch the daemon, and take the hold again

The native backend watches com.feralinteractive.GameMode and only watches it. The portal still forwards RegisterGame. There is no auto-start flag, so the watch does not activate the daemon.

    // gamemoded holds GameMode registrations, not the portal. When it restarts
    // they are gone, and the facade has to register again. No auto-start flag:
    // watching must not activate the daemon.
    game_mode_daemon_watch = g_bus_watch_name_on_connection(connection,
            GAME_MODE_DAEMON_BUS_NAME, G_BUS_NAME_WATCHER_FLAGS_NONE,
            &XdgPortalNative::on_game_mode_daemon_appeared,
            &XdgPortalNative::on_game_mode_daemon_vanished, this, nullptr);

The first report is the daemon as found at startup. Nothing was registered with an earlier instance, so that report is ignored. A later owner is a new instance. A replacement with no unowned gap is reported as lost, then back. The facade keeps those pids as lost. The game still wants them, and the next instance gets them back:

func _on_game_mode_daemon_presence_changed(available: bool) -> void:
    if available:
        _retake_lost_game_mode()
        return
    for pid: int in _game_mode_pids.keys():
        _game_mode_lost_pids[pid] = true
    _game_mode_pids.clear()

_retake_lost_game_mode() calls RegisterGame for each lost pid once a daemon owns the name and the portal can be reached. If the portal name is unowned, it waits and runs again when the portal returns. A pid the new daemon rejects is dropped, and portal_error is emitted with context GameMode.RegisterGame:

            portal_error.emit(
                "GameMode.RegisterGame",
                "Could not register pid %d with GameMode again after gamemoded restarted." % pid
            )

release_game_mode() for a lost hold returns true with no bus call, and the hold is not taken again. UnregisterGame would otherwise D-Bus-activate a daemon that never had the pid:

    if _game_mode_lost_pids.has(resolved):
        _game_mode_lost_pids.erase(resolved)
        return true

request_game_mode() for a lost hold erases that entry and registers immediately. The outcome of that call decides it. Leaving the tree clears lost holds and does not unregister them. gamemoded already dropped the pid.

Inside Flatpak the name is not visible

A Flatpak sandbox cannot see com.feralinteractive.GameMode, so the watch never fires and nothing changes there. No extra permission is requested. A gamemoded restart is recovered only when the game exits. The same note is in docs/compatibility.md and docs/api.md. The constant says why a missing name is left alone:

// The GameMode daemon's own name. Only watched, never called: the portal
// forwards registrations to it. A sandbox that cannot see the name never
// reports it, which is the same as a daemon that never restarts.
inline constexpr const char *GAME_MODE_DAEMON_BUS_NAME = "com.feralinteractive.GameMode";

What I ran

./scripts/run_tests.sh reported 116 passed and 0 failed, on Godot 4.7.2, a macOS build, headless. Seven of the tests are new, in tests/test_game_mode_daemon.gd. They cover taking the hold again after a restart, not taking a hold released before the restart, a local release while the daemon is gone, a request during the gap, a rejected re-take, a daemon restart during a portal gap, and leaving the tree after the daemon went away.

I did not run ./scripts/run_native_smoke.sh locally. It now has a gamemode-restart mode, in which the fake portal also owns the daemon name and restarts it after the first RegisterGame. The checks on bf3e41c passed, including the Godot suites and the fake-portal smoke (run 46081122884). I did not run row 16 of docs/native-testing.md, systemctl --user restart gamemoded, for that pull request. I have since killed the daemon on a desktop. That run is below.

The respawned daemon saved the wrong originals

GameMode 1.8.2 (gamemode-1.8.2-5.fc45) on Fedora 46 Rawhide, headless Godot 4.7.2, with the addon at 1a159c5. Before the game: governor powersave on 16 CPUs, EPP balance_performance, split_lock_mitigate 1. With the game holding GameMode: performance, performance, and 0.

I sent kill -9 while the game still held it. systemd started a new daemon about 150 ms later. The old process was status 9 at 19:45:46.68, and the new one started at 19:45:46.83. The addon registered again on its own. Status went 0, then 1, within a second.

A clean quit, a clean start and quit, and a clean daemon restart followed by a start and quit all left performance / performance / 0. The values from before the game did not come back.

GameMode 1.8.2 reads those originals from the live system when it activates, and keeps them in memory. There is no state file. A new process starts empty: memset of initial_cpu_mode at line 144, and initial_split_lock_mitigate = -1 at line 159. When the addon registered again, that new daemon stored the live performance and 0 as the values to restore.

The governor is captured only on the way out of the default, in daemon/gamemode-context.c. get_gov_state() reads scaling_governor (line 48):

    if (self->current_govenor == GAME_MODE_GOVERNOR_DEFAULT) {
        /* Read the initial governor state so we can revert it correctly */
        const char *initial_state = get_gov_state();
        if (initial_state == NULL) {
            return 0;
        }

        /* store the initial cpu governor mode */
        strncpy(self->initial_cpu_mode, initial_state, sizeof(self->initial_cpu_mode) - 1);
        self->initial_cpu_mode[sizeof(self->initial_cpu_mode) - 1] = '\0';

On restore, defaultgov wins when it is set (line 288):

        gov_str = gov_config_str[0] != '\0' ? gov_config_str : self->initial_cpu_mode;

Split-lock is the same idea, still in gamemode-context.c. Tag 1.8.2 has no common-splitlock.c. The first activation reads /proc/sys/kernel/split_lock_mitigate (lines 220–242):

        FILE *f = fopen("/proc/sys/kernel/split_lock_mitigate", "r");
        if (f == NULL) {
            if (errno == ENOENT)
                return 0;

            LOG_ERROR("Couldn't open /proc/sys/kernel/split_lock_mitigate : %s\n", strerror(errno));
            return 1;
        }

        if (fgets(value_str, sizeof value_str, f) == NULL) {
            LOG_ERROR("Couldn't read from /proc/sys/kernel/split_lock_mitigate : %s\n",
                      strerror(errno));
            fclose(f);
            return 1;
        }

        self->initial_split_lock_mitigate = strtol(value_str, NULL, 10);

tuned-adm profile balanced put the governor and EPP back, and left split_lock_mitigate at 0. pkexec /usr/libexec/procsysctl split_lock_mitigate 1 set it to 1. That is GameMode's polkit helper. No reboot.

defaultgov= in gamemode.ini is what line 288 prefers, so a set value would come back even after a respawn. I have not tried that. Split-lock has no key for the value to restore. disable_splitlock only turns the change on or off.

The addon did what it was written to do. The lost originals are the daemon's.