GTK Screenshot Works on Main Thread but Not ChromeWorker

I wrote up this simple js-ctypes to take a screenshot on Linux/GTK systems based on StackOverflow :: Taking a screenshot with C\GTK. This code can be copied and pasted in scratchpad and run in browser environment and it works fine, we see that console.info('screenshot:', screenshot.toString()); logs a string with address pointing to somewhere:

Cu.import('resource://gre/modules/ctypes.jsm');
var gdk2 = ctypes.open('libgdk-x11-2.0.so.0');

var _void = ctypes.void_t;
var gint = ctypes.int;
var GdkPixbuf = ctypes.StructType('GdkPixbuf');
var GdkDrawable = ctypes.StructType('GdkDrawable');
var GdkColormap = ctypes.StructType('GdkColormap');
var GdkWindow = ctypes.StructType('GdkWindow');
var int = ctypes.int;

// https://developer.gnome.org/gdk3/stable/gdk3-Windows.html#gdk-get-default-root-window
var gdk_get_default_root_window = gdk2.declare('gdk_get_default_root_window', ctypes.default_abi, GdkWindow.ptr);

// https://developer.gnome.org/gdk3/stable/gdk3-Windows.html#gdk-window-get-origin
var gdk_drawable_get_size = gdk2.declare('gdk_drawable_get_size', ctypes.default_abi,
    gint,            // return
    GdkWindow.ptr,    // *window
    gint.ptr,        // *x
    gint.ptr        // *y
);

// https://developer.gnome.org/gdk3/stable/gdk3-Windows.html#gdk-window-get-origin
var gdk_window_get_origin = gdk2.declare('gdk_window_get_origin', ctypes.default_abi,
    gint,            // return
    GdkWindow.ptr,    // *window
    gint.ptr,        // *x
    gint.ptr        // *y
);

// https://developer.gnome.org/gdk2/stable/gdk2-Pixbufs.html#gdk-pixbuf-get-from-drawable
var gdk_pixbuf_get_from_drawable = gdk2.declare('gdk_pixbuf_get_from_drawable', ctypes.default_abi,
    GdkPixbuf.ptr,    // return
    GdkPixbuf.ptr,    // *dest
    GdkDrawable.ptr,    // *src
    GdkColormap.ptr,    // *cmap
    int,                // src_x
    int,                // src_y
    int,                // dest_x
    int,                // dest_y
    int,                // width
    int                // height
);

var rootGdkWin = gdk_get_default_root_window();
console.info('rootGdkWin:', rootGdkWin.toString());

var x_orig = gint();
var y_orig = gint();
var width = gint();
var height = gint();

var rez_gdkDrawGetSiz = gdk_drawable_get_size(rootGdkWin, width.address(), height.address());
console.info('width:', width.value, 'height:', height.value);

var rez_gdkWinGetOrg = gdk_window_get_origin(rootGdkWin, x_orig.address(), y_orig.address());
console.info('x:', x_orig.value, 'y:', y_orig.value);

var rootGdkDrawable = ctypes.cast(rootGdkWin, GdkDrawable.ptr);
var screenshot = gdk_pixbuf_get_from_drawable(null, rootGdkDrawable, null, x_orig.value, y_orig.value, 0, 0, width.value, height.value);
console.info('screenshot:', screenshot.toString());

gdk2.close();

Now my goal is to run this from a ChromeWorker (off of the main thread) but the call to gdk_pixbuf_get_from_drawable causes it to crash. Here is a super simple ChromeWorker with this code: https://github.com/yajd/ChromeWorker/tree/patch-1 you can install it directly from that github repo with AMO :: GitHub Extension Installer.

I posted asking why it’s crashing in GTK tag section of Stackoverflow here: http://stackoverflow.com/questions/30343641/gdk2-and-gdk3-methods-to-get-pixbuff-of-root-window-failing-with-error-message

I posted there the terminal dump too during crash and Ill paste it here too:

[xcb] Unknown sequence number while processing queue
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
firefox: ../../src/xcb_io.c:274: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed.
firefox: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.

They told me in the comments of that topic that they said probably running in a thread is casuing the issue, and they were right, but I don’t know how to fix this, I thought to try XInitThreads but this is redundant as this is already called when a ChromeWorker is made: https://dxr.mozilla.org/mozilla-central/search?q=XInitThreads&case=true&=mozilla-central&redirect=true

Does anyone have any ideas on how to run this gdk_pixbuf_get_from_drawable from ChromeWorker? I had same issue with GTK3 method of gdk_pixbuf_get_from_window in terminal on crash it threw this:

(firefox:2624): GLib-GObject-WARNING **: cannot register existing type 'GdkWindow'
(firefox:2624): GLib-CRITICAL **: g_once_init_leave: assertion 'result != 0' failed
(firefox:2624): Gdk-CRITICAL **: gdk_pixbuf_get_from_window: assertion 'GDK_IS_WINDOW (src)' failed

Anyone know how to fix?
Thanks very much I’ve been struggling with it for awhile.

Apparently certain GTK callback mechanisms dont look for the js callback in the thread, but instead look for it on the main thread: https://bugzilla.mozilla.org/show_bug.cgi?id=1181960

I’m thinking of passing a pointer or something to the mainthread after creating the js function in the worker, didnt test this yet, might work. I switched to x11 though that works great from threads.