Multiple errors related to callbacks and garbage collection

I’m developing an extension and periodically get errors related to callbacks and garbage collection:

Attempting to run a JS callback during garbage collection. This is most likely 
caused by destroying a Clutter actor or GTK widget with ::destroy signal 
connected, or using the destroy(), dispose(), or remove() vfuncs. Because it 
would crash the application, it has been blocked.
The offending callback was BindingTransformFunc().

and

Attempting to call back into JSAPI during the sweeping phase of GC. This is most 
likely caused by not destroying a Clutter actor or Gtk+ widget with ::destroy 
signals connected, but can also be caused by using the destroy(), dispose(), or 
remove() vfuncs. Because it would crash the application, it has been blocked and 
the JS callback not invoked.

After reading this thread with a similar problem, I came to the conclusion that in my case, the cause of these errors is asynchronous loading of files:

_loadFile(file) {
    return new Promise((resolve, reject) => {
        file.load_contents_async(null, (...[, result]) => {
            try {
                const [ok, bytes] = file.load_contents_finish(result);
                if (!ok) {
                    throw new Error(`Uknnown error`);
                }
                const text = new TextDecoder().decode(bytes);
                resolve(text);
            } catch (error) {
                reject(error);
            }
        });
    });
}
//...
async _loadMenuItemFromFile(file) {
    const text = await this._storage.loadFile(file);
    if (!text) {
        return;
    }
    const menuItem = this._createMenuItem(text);
    this.menu.addMenuItem(menuItem);
}

If I read files not asynchronously, no errors occur, but such code can cause the shell to hang for a while, so I need to do it asynchronously.

Is there any way around this?

This indicates it’s one of the getter/setting functions for a GObject.Binding made with GObject.Object.bind_property_full(). Do you have on of those?

Yes, I use bind_property_full in my code.

Perhaps, it’s worth adding more details:

  • I’m currently using GNOME 42 (Ubuntu 22.04), but I tested this code on GNOME 45 as well and I got the same errors there
  • here is the source code
  • here is the log

This seems to be another symptom of Timeout callback tries to run during garbage collection (#327) · Issues · GNOME / gjs · GitLab.

Yes, looks very similar.

Is there a way to know what GC is currently running and when it will finish?

Any workaround on this?

If there was a workaround known, it would be discussed in that ticket. Sorry!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.