Public bug reported:

There are various bugs here:

 - Requests are not really performed in async, but queued
 - The cancellable passed to the requests may be ignored
 - It's impossible to use a cancellable for each request

A side effect of this is the fact that as reported in
https://www.omgubuntu.co.uk/2026/03/ubuntu-2604-gnome-search-extensions
the search results are not correct when cancelling requests and doing
new ones.

Test case:

```
#!/usr/bin/gjs -m
/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

import Gio from 'gi://Gio'
import GLib from 'gi://GLib'
import Snapd from 'gi://Snapd?version=2'


Gio._promisify(Snapd.Client.prototype, 'find_async');

const searches = [
    "telegram",
    "spotify",
    "slack",
    "discord",
    "vlc",
]

const repeatedSearches = 3;

const findAsync = async (client, terms, cancellable) => {
    try {
        const [results,] = await client.find_async(Snapd.FindFlags.NONE,
            terms, cancellable);
        print(`Search for "${terms}" returned ${results.length} ` +
            `results: ${results.map(r => r.name).join(', ')}`);
    } catch (e) {
        if (e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED)) {
            print(`Search for "${terms}" cancelled`);
            return;
        }
        throw e;
    }
};

async function orderedRequests() {
    const client = new Snapd.Client();

    for (let i = 0; i < searches.length * repeatedSearches; i++) {
        const cancellable = new Gio.Cancellable();
        if (i % 2 === 0) {
            GLib.timeout_add(GLib.PRIORITY_DEFAULT, 10, () => {
                cancellable.cancel();
                return GLib.SOURCE_REMOVE;
            });
        }

        const terms = searches[i % searches.length];
        await findAsync(client, terms, cancellable);
    }
}

async function batchRequests() {
    const client = new Snapd.Client();

    let promises = [];

    for (let i = 0; i < searches.length * repeatedSearches; i++) {
        const cancellable = new Gio.Cancellable();
        if (i % 2 === 0) {
            GLib.timeout_add(GLib.PRIORITY_DEFAULT, 10, () => {
                cancellable.cancel();
                return GLib.SOURCE_REMOVE;
            });
        }

        const terms = searches[i % searches.length];
        promises.push(findAsync(client, terms, cancellable));
    }

    await Promise.all(promises);
}

print("Running ordered requests...");
let loop = new GLib.MainLoop(null, false);
orderedRequests().catch(logError).finally(() => loop.quit());
loop.run();

print("--------------------------------");

print("Running batch requests...");
loop = new GLib.MainLoop(null, false);
batchRequests().catch(logError).finally(() => loop.quit());
loop.run();
```

** Affects: snapd-glib (Ubuntu)
     Importance: High
     Assignee: Marco Trevisan (Treviño) (3v1n0)
         Status: In Progress

-- 
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/2144644

Title:
  snapd-glib does not handle properly multiple parallel requests (with
  cancellation)

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/snapd-glib/+bug/2144644/+subscriptions


-- 
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to