init: Move Meta main loop into JavaScript after GJS context is initialized

gjs now has an internal mainloop that it can spin to resolve
module imports. That loop uses the thread default context,
so its possible that other sources, namely from mutter, get
dispatched when iterating the context. If that happens before
mutter is properly initialized, this will lead to a crash.

GjsContext needs to iterate its internal mainloop when initializing
to resolve internal modules, to avoid iterating Meta's mainloop and
triggering events before Meta is ready we will initialize the Shell
global and thus the GjsContext (js_context) before Meta.

Once GjsContext is initialized, we can call meta_context_setup().
Once Meta is setup and started, we'll run init.js which uses GJS'
internal promises API to set a "mainloop hook". The mainloop hook
is run immediately after the module returns so GJS will not attempt
to iterate the main loop again before exiting.

Also adjust the 'headlessStart' test to not wait for the
MetaContext::started signal, as that signal has now already
been emitted when the code is executed.

https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/6691

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2781>
This commit is contained in:
Evan Welsh
2023-06-07 08:58:08 -07:00
committed by Florian Müllner
parent 5d8e860a48
commit 8fb8f7f827
4 changed files with 79 additions and 63 deletions

View File

@ -23,15 +23,12 @@ function init() {
global.connect('shutdown', () => {
_testMonitor?.destroy();
});
global.context.connect('started',
GLib.timeout_add_seconds(
GLib.PRIORITY_LOW, 2,
() => {
GLib.timeout_add_seconds(
GLib.PRIORITY_LOW, 2,
() => {
log('Connecting 1280x720 test monitor');
_testMonitor = MetaTest.TestMonitor.new(
global.context, 1280, 720, 60.0);
});
log('Connecting 1280x720 test monitor');
_testMonitor = MetaTest.TestMonitor.new(
global.context, 1280, 720, 60.0);
});
Scripting.defineScriptEvent('monitorsChanged', 'Monitors changed');

View File

@ -1,6 +1,25 @@
import { setConsoleLogDomain } from 'console';
import {setConsoleLogDomain} from 'console';
import GLib from 'gi://GLib';
import {exit} from 'system';
setConsoleLogDomain('GNOME Shell');
imports.ui.environment.init();
imports.ui.main.start();
// Run the Mutter main loop after
// GJS finishes resolving this module.
imports._promiseNative.setMainLoopHook(() => {
// Queue starting the shell
GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
try {
imports.ui.main.start();
} catch (e) {
logError(e);
exit(1);
}
return GLib.SOURCE_REMOVE;
});
// Run the meta context's main loop
global.context.run_main_loop();
});