gtkglibvalagioflatpak

GLib.NetworkMonitor incorrect initial network availability in Flatpak app


I'm packaging a Flatpak application that checks the network availability on startup. Since switching to Flatpak packaging I've noticed that the GLib.NetworkMonitor is initially reporting no network availability, but very shortly thereafter the network-changed signal is emitted to indicate that there is a network connection.

This appears to be Flatpak related - maybe due to the sandbox not having a connection initially?

I don't see this behavior documented anywhere, so I don't want to assume that there will always be a signal emitted initially.

public class MyApplication : Gtk.Application {

    private bool is_network_available;

    construct {
        // After the application is fully started, UI initialized, etc. this gets
        // triggered and reports change to `true`
        network_monitor.network_changed.connect (() => {
            debug ("Network availability changed: %s", network_monitor.get_network_available ().to_string ());
        });
    }
    
    protected override void activate () {
        // Check the initial state of the network connection
        is_network_available = network_monitor.get_network_available ();
        debug (is_network_available); // Reports false
    }
    
}

Is this expected for Flatpak packaged applications? Is there a more reliable method to perform this check on startup?


Solution

  • It’s a bug in GLib: https://gitlab.gnome.org/GNOME/glib/-/issues/1718

    For the moment you will have to work around it some way. Assuming that a signal will always be emitted on startup is not guaranteed behaviour, so if you use that as a workaround you should make it conditional on the GLib version, and disable your workaround code if built against a version of GLib which is fixed. (No versions of GLib are fixed yet, though.)