Skip to content

Ensure the correct realm is entered in the async executor

Evan Welsh requested to merge ewlsh/fix-realm-in-async into master

When porting GNOME Shell to ESM I discovered that if code contains dynamic imports which are executed by a mainloop after gjs_context_eval_module returns, we hit a segfault. This is not possible in the gjs CLI as its lifecycle is tied to the mainloop, but in GNOME Shell the mainloop outlives a single call to gjs_context_eval_module.

Thread 1 "gnome-shell" received signal SIGSEGV, Segmentation fault.
JSLinearString::new_<(js::AllowGC)1, unsigned char> (cx=cx@entry=0x555555d79040, chars=..., length=length@entry=33531)
    at /home/evan/jhbuild/checkout/mozjs-78.11.0/js/src/vm/StringType-inl.h:266
266	  if (cx->zone()->isAtomsZone()) {
(gdb) bt
#0  JSLinearString::new_<(js::AllowGC)1, unsigned char> (cx=cx@entry=0x555555d79040, chars=..., length=length@entry=33531)
    at /home/evan/jhbuild/checkout/mozjs-78.11.0/js/src/vm/StringType-inl.h:266
#1  0x00007ffff3a82c7b in js::NewStringCopyNDontDeflate<(js::AllowGC)1, unsigned char> (cx=cx@entry=0x555555d79040, s=<optimized out>, 
    n=33531) at /home/evan/jhbuild/checkout/mozjs-78.11.0/js/src/vm/StringType.cpp:1707
#2  0x00007ffff3a834ff in js::NewStringCopyN<(js::AllowGC)1, unsigned char> (cx=0x555555d79040, s=0x7fffffffd080 "\020\344\352UUU", 
    n=33531) at /home/evan/jhbuild/checkout/mozjs-78.11.0/js/src/vm/StringType.cpp:1764
#3  js::NewStringCopyUTF8N<(js::AllowGC)1> (cx=cx@entry=0x555555d79040, utf8=...)
    at /home/evan/jhbuild/checkout/mozjs-78.11.0/js/src/vm/StringType.cpp:1797
#4  0x00007ffff374a768 in JS_NewStringCopyUTF8N (cx=cx@entry=0x555555d79040, s=...)
    at /home/evan/jhbuild/checkout/mozjs-78.11.0/js/src/jsapi.cpp:4183
#5  0x00007ffff70d173f in gjs_string_from_utf8_n (cx=0x555555d79040, utf8_chars=<optimized out>, len=<optimized out>, out=out@entry=...)
    at /home/evan/jhbuild/install/include/mozjs-78/mozilla/RangedPtr.h:54
#6  0x00007ffff709e8fe in load_async_callback (file=0x5555555c9840, res=0x55555594f1a0, data=0x555555aa5dc0)
    at ../../../../jhbuild/checkout/gjs/gjs/internal.cpp:531
#7  0x00007ffff7c0bcb9 in g_task_return_now (task=0x55555594f1a0) at ../../../../jhbuild/checkout/glib/gio/gtask.c:1219
#8  0x00007ffff7c0c7fb in g_task_return (type=<optimized out>, task=0x55555594f1a0) at ../../../../jhbuild/checkout/glib/gio/gtask.c:1289
#9  g_task_return (task=0x55555594f1a0, type=<optimized out>) at ../../../../jhbuild/checkout/glib/gio/gtask.c:1245
#10 0x00007ffff7bb8902 in load_contents_close_callback (obj=<optimized out>, close_res=0x7fffd8007ca0, user_data=0x5555560846d0)
    at ../../../../jhbuild/checkout/glib/gio/gfile.c:7177
#11 0x00007ffff7bd6a1e in async_ready_close_callback_wrapper (source_object=0x7fffd8001b80, res=0x7fffd8007ca0, user_data=0x5555560846d0)
    at ../../../../jhbuild/checkout/glib/gio/ginputstream.c:577
#12 0x00007ffff7c0bcb9 in g_task_return_now (task=0x7fffd8007ca0) at ../../../../jhbuild/checkout/glib/gio/gtask.c:1219
#13 0x00007ffff7c0bcf9 in complete_in_idle_cb (task=0x7fffd8007ca0) at ../../../../jhbuild/checkout/glib/gio/gtask.c:1233
#14 0x00007ffff7a1dac4 in g_main_dispatch (context=0x5555555a8a60) at ../../../../jhbuild/checkout/glib/glib/gmain.c:3344
#15 g_main_context_dispatch (context=0x5555555a8a60) at ../../../../jhbuild/checkout/glib/glib/gmain.c:4062
#16 0x00007ffff7a1de68 in g_main_context_iterate (context=0x5555555a8a60, block=block@entry=1, dispatch=dispatch@entry=1, 
    self=<optimized out>) at ../../../../jhbuild/checkout/glib/glib/gmain.c:4138
#17 0x00007ffff7a1e153 in g_main_loop_run (loop=0x555555e3f2c0) at ../../../../jhbuild/checkout/glib/glib/gmain.c:4336
#18 0x00007ffff6d4a686 in meta_context_run_main_loop (context=context@entry=0x5555555a5090, error=error@entry=0x7fffffffd5b0)
    at ../../../../jhbuild/checkout/mutter/src/core/meta-context.c:403
#19 0x00005555555568fb in main (argc=<optimized out>, argv=<optimized out>) at ../../../../jhbuild/checkout/gnome-shell/src/main.c:563

Merge request reports