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:
- use the flag –no-xshm when running electron:
./electron --no-sandbox --no-xshm - or set the environment variable QT_X11_NO_MITSHM=1 in your remote/container environment:
export QT_X11_NO_MITSHM=1 - or add –ipc=host to your docker run command:
docker run --ipc=host ....
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:
- Get shared memory to work.
- docker run –ipc=host … gets shared memory working for docker. This is the more performant solution, as using shared memory is faster than not.
- I’m not sure why -v /dev/shm:/dev/shm doesn’t work for docker as /dev/shm is where shared memory resides according to the linked wikipedia article.
- Tell chromium not to use shared memory.
- The line with HasSwitch(switches::kNoXshm), checks if there was a command line argument to chromium switches::kNoXshm, which maps to no-xshm. We can pass the –no-xshm flag to electron. Electron seems to pass the flag on to chromium.
- We can also tell chromium not to use shared memory by setting the environment variable QT_X11_NO_MITSHM to anything, because as seen in the code above, it just checks if QT_X11_NO_MITSHM is defined. We can also set J2D_USE_MITSHM=0 or J2D_USE_MITSHM=false, instead. It is a little bit of a hack, and is not future proof, as the developers of chromium could change this logic at any time (like checking if you’re actually running qt), but at the moment, it should work for both docker containers and remote x11 forwarding.