X11 redirection and Electron apps

Published (last updated )

TL;DR: add –ignore-cpu-blacklist –no-xshm

Explanation regarding –no-xshm:

I was running into this problem and for my situation I found a solution. I was also able to solve @alexanderadam’s reproduction of the bug, so hopefully this solves other people’s problems, too!

TLDR; The problem is shared memory is trying to be used, but can’t. Below are three different ways to fix it:

The first solution tells chromium not to use shared memory via a flag, and should work in all environments.

The second solution tells chromium not to use shared memory via the environment variable QT_X11_NO_MITSHM=1, and should work in all environments.

The third solution, exclusive to docker containers, is to add –ipc=host - as mentioned in the third bullet point here: https://github.com/mviereck/x11docker/wiki/Short-setups-to-provide-X-display-to-container. I am not sure of the security consequences of using this flag. It allows docker and the host machine to use shared memory and therefore should be faster.

In Depth Explanation: I believe the underlying problem resides in the newer versions of chromium that electron uses. I suspect in the newer versions of chromium, chromium is trying to use shared memory and can’t, while in the older versions of chromium, it never tried to use shared memory, or if it did, it correctly handled the fallback to not use shared memory. In the newer versions of chromium source code, there is a new file x11_shm_image_pool.cc(electron 9+ uses chromium 83.0.4103.16) or x11_shm_image_pool_base.cc (electron 8+ uses chromium 80.0.3987.163), that tries to use a shared memory extension (MIT-SHM: https://en.wikipedia.org/wiki/MIT-SHM). It tries to figure out what GUI toolkit is being used, and enable/disable shared memory based on what that GUI toolkit tells us:

bool ShouldUseMitShm(XDisplay* display) {
    // MIT-SHM may be available on remote connetions, but it will be unusable.  Do
    // a best-effort check to see if the host is remote to disable the SHM
    // codepath.  It may be possible in contrived cases for there to be a
    // false-positive, but in that case we'll just fallback to the non-SHM
    // codepath.
...
...
...
    // Used by QT.
    if (env->HasVar("QT_X11_NO_MITSHM"))
      return false;

    // Used by JRE.
    std::string j2d_use_mitshm;
    if (env->GetVar("J2D_USE_MITSHM", &j2d_use_mitshm) &&
      ¦ (j2d_use_mitshm == "0" ||
      ¦  base::LowerCaseEqualsASCII(j2d_use_mitshm, "false"))) {
      return false;
    }

    // Used by GTK.
    if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoXshm))
      return false;

    return true;
  }

Per the wikipedia article on MIT-SHM, shared memory only works if the X client and the X server are on the same machine. Running electron in docker (or remotely) isolates the X client from the X server, so they don’t have access to shared memory.

So, there are two possible solutions now: