Skip to content

Instantly share code, notes, and snippets.

@nightuser
Last active January 8, 2024 15:31
  • Star 22 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nightuser/2ec1b91a66ec33ef0a0a67b6c570eb40 to your computer and use it in GitHub Desktop.
Use existing Xorg session for chrome-remote-desktop
Add an option to use the existing Xorg session with
chrome-remote-desktop.
The original idea of the patch: https://superuser.com/a/850359
--- a/chrome-remote-desktop 2022-04-23 17:34:24.118136150 +0300
+++ b/chrome-remote-desktop 2022-04-23 17:33:56.083833805 +0300
@@ -109,6 +109,8 @@
X_LOCK_FILE_TEMPLATE = "/tmp/.X%d-lock"
FIRST_X_DISPLAY_NUMBER = 20
+EXISTING_X_DISPLAY_FILE_PATH = os.path.join(CONFIG_DIR, "Xsession")
+X_SESSION_FILE_TEMPLATE = "/tmp/.X11-unix/X%d"
# Amount of time to wait between relaunching processes.
SHORT_BACKOFF_TIME = 5
@@ -514,6 +516,25 @@
return True
return False
+ def _use_existing_session(self):
+ with open(EXISTING_X_DISPLAY_FILE_PATH) as fh:
+ try:
+ display = int(fh.readline().rstrip())
+ except ValueError:
+ logging.error("Display is not a number")
+ sys.exit(1)
+ if not os.path.exists(X_SESSION_FILE_TEMPLATE % display):
+ logging.error("Xorg session file doesn't exist")
+ sys.exit(1)
+
+ logging.info("Using existing Xorg session: %d" % display)
+ self.child_env["DISPLAY"] = ":%d" % display
+ self.child_env["CHROME_REMOTE_DESKTOP_SESSION"] = "1"
+
+ # Set SSH_AUTH_SOCK to the file name to listen on.
+ if self.ssh_auth_sockname:
+ self.child_env["SSH_AUTH_SOCK"] = self.ssh_auth_sockname
+
def launch_session(self, server_args, backoff_time):
"""Launches process required for session and records the backoff time
for inhibitors so that process restarts are not attempted again until
@@ -522,10 +543,13 @@
self._init_child_env()
self.setup_audio()
self._setup_gnubby()
- self._launch_server(server_args)
- if not self._launch_pre_session():
- # If there was no pre-session script, launch the session immediately.
- self.launch_desktop_session()
+ if os.path.exists(EXISTING_X_DISPLAY_FILE_PATH):
+ self._use_existing_session()
+ else:
+ self._launch_server(server_args)
+ if not self._launch_pre_session():
+ # If there was no pre-session script, launch the session immediately.
+ self.launch_desktop_session()
self.server_inhibitor.record_started(MINIMUM_PROCESS_LIFETIME,
backoff_time)
self.session_inhibitor.record_started(MINIMUM_PROCESS_LIFETIME,
@VSMent
Copy link

VSMent commented Aug 26, 2020

Hello, tell me please how to use this?
Thanks in advance)

@nightuser
Copy link
Author

@VSMent: You should apply it to chrome-remote-desktop Python script located in the chrome remote desktop install directory (usually somewhere in /opt). Then create a file named Xsession in the configuration directory of CRD with the number of the X session (usually 0 or 1, see $DISPLAY env variable in your shell, place the number without : prefix).

I still didn't implement the PulseAudio support (it should be relatively easy -- just create a sink and give it to CRD). Don't know whether it's really needed or not.

@VSMent
Copy link

VSMent commented Aug 27, 2020

Thanks a lot, will try)

@fabh2o
Copy link

fabh2o commented Feb 25, 2021

Old patch
--- chrome-remote-desktop.bkp	2021-02-25 11:44:22.689318232 +0100
+++ chrome-remote-desktop	2021-02-25 21:05:16.438434584 +0100
@@ -107,6 +107,8 @@
 
 X_LOCK_FILE_TEMPLATE = "/tmp/.X%d-lock"
 FIRST_X_DISPLAY_NUMBER = 20
+EXISTING_X_DISPLAY_FILE_PATH = os.path.join(CONFIG_DIR, "Xsession")
+X_SESSION_FILE_TEMPLATE = "/tmp/.X11-unix/X%d"
 
 # Amount of time to wait between relaunching processes.
 SHORT_BACKOFF_TIME = 5
@@ -774,14 +776,29 @@
         "Session output: ", SESSION_OUTPUT_TIME_LIMIT_SECONDS)
     output_filter_thread.start()
 
+  def _use_existing_session(self):
+    with open(EXISTING_X_DISPLAY_FILE_PATH) as fh:
+      display = int(fh.readline().rstrip())
+    if not os.path.exists(X_SESSION_FILE_TEMPLATE % display):
+      logging.error("Xorg session file doesn't exist")
+      sys.exit(1)
+    logging.info("Using existing Xorg session: %d" % display)
+    self.child_env["DISPLAY"] = ":%d" % display
+    self.child_env["CHROME_REMOTE_DESKTOP_SESSION"] = "1"
+
   def launch_session(self, x_args):
     self._init_child_env()
     self._setup_pulseaudio()
     self._setup_gnubby()
-    self._launch_x_server(x_args)
-    if not self._launch_pre_session():
+#    self._launch_x_server(x_args)
+#    if not self._launch_pre_session():
       # If there was no pre-session script, launch the session immediately.
-      self.launch_x_session()
+#      self.launch_x_session()
+    if os.path.exists(EXISTING_X_DISPLAY_FILE_PATH):
+      self._use_existing_session()
+    else:
+      self._launch_x_server(x_args)
+      self._launch_x_session()
 
   def launch_host(self, host_config, extra_start_host_args):
     # Start remoting host
This patch works for version `89.0.4389.45` of ChromeRemoteDesktop

@tokland
Copy link

tokland commented Apr 5, 2021

@fabh2o Thank you! Working on Arch Linux, AUR chrome-remote-desktop 89.0.4389.45-1. @nightuser, maybe you can update the gist to this version? For the record, some example steps to apply it:

$ systemctl --user stop chrome-remote-desktop.service
$ cp /opt/google/chrome-remote-desktop/chrome-remote-desktop .
$ cp chrome-remote-desktop chrome-remote-desktop.backup
$ patch < file_containing_the_diff_from_the_previous_comment.patch
patching file chrome-remote-desktop
$ sudo cp chrome-remote-desktop /opt/google/chrome-remote-desktop/chrome-remote-desktop
$ systemctl --user start chrome-remote-desktop.service

@nightuser
Copy link
Author

Updated the gist.

@tokland: you can simplify things:

cd /opt/google/chrome-remote-desktop
sudo cp chrome-remote-desktop chrome-remote-desktop_bak # backup the original
sudo patch -Np1 -i /path/to/use_existing_session.patch
systemctl --user restart chrome-remote-desktop.service

You don't have to stop the service and you can patch the file inplace.

@dmmat
Copy link

dmmat commented Jul 13, 2021

@nightuser I try on last version (91.0.4472.10-1). and i have black screen (

@nightuser
Copy link
Author

@dmmat: What DE/WM are you using?

@dmmat
Copy link

dmmat commented Jul 13, 2021

@dmmat: What DE/WM are you using?

5.10.42-1-MANJARO - GNOME Shell 3.38.4 - Resolution: 1366x768

@nightuser
Copy link
Author

@dmmat: On Wayland? It's only supported on Xorg.

@dmmat
Copy link

dmmat commented Jul 13, 2021

@dmmat: On Wayland? It's only supported on Xorg.

Thanks, work with Xorg, but problem with startup using systemctl --user start chrome-remote-desktop.service

Job for chrome-remote-desktop.service failed because a timeout was exceeded.
See "systemctl --user status chrome-remote-desktop.service" and "journalctl --user -xeu chrome-remote-desktop.service" for details.

systemctl --user status chrome-remote-desktop.service

× chrome-remote-desktop.service - "Chrome Remote Desktop host daemon"
     Loaded: loaded (/usr/lib/systemd/user/chrome-remote-desktop.service; disabled; vendor preset: enabled)
     Active: failed (Result: timeout) since Tue 2021-07-13 15:48:16 EEST; 35s ago
    Process: 1762 ExecStart=/usr/bin/crd --start (code=killed, signal=TERM)
        CPU: 327ms

j 13 15:46:46 medics-terminal-2 systemd[362]: Starting "Chrome Remote Desktop host daemon"...
j 13 15:48:16 medics-terminal-2 systemd[362]: chrome-remote-desktop.service: start operation timed out. Terminating.
j 13 15:48:16 medics-terminal-2 systemd[362]: chrome-remote-desktop.service: Failed with result 'timeout'.
j 13 15:48:16 medics-terminal-2 systemd[362]: Failed to start "Chrome Remote Desktop host daemon".


but work with run crd --start and enter password

@nightuser
Copy link
Author

@dmmat: The password? Did you set up the headless mode? The basic instructions can be found on the AUR page or see this blog post.

@dmmat
Copy link

dmmat commented Jul 13, 2021

@dmmat: The password? Did you set up the headless mode? The basic instructions can be found on the AUR page or see this blog post.

i mean password for local user

@Prometheus720
Copy link

@VSMent: You should apply it to chrome-remote-desktop Python script located in the chrome remote desktop install directory (usually somewhere in /opt). Then create a file named Xsession in the configuration directory of CRD with the number of the X session (usually 0 or 1, see $DISPLAY env variable in your shell, place the number without : prefix).

When you say apply, do you just append it?

@nightuser
Copy link
Author

@Prometheus720: Patches are usually applied with the patch command. See this reply above for instructions.

@nightuser
Copy link
Author

@dmmat: Sorry for disappearing. Did you figure it out? I don't understand why it requires a password. Did you perhaps run it with sudo instead of as a normal user?

@dmmat
Copy link

dmmat commented Jul 29, 2021

@nightuser run as normal, but system ask the root password =(

@nightuser
Copy link
Author

@dmmat: I don't know why it happens. I found this, bit it looks bizarre. But you may try \systemctl instead of systemctl (the backslash runs the original command even if it's shadowed by an alias).

Also, when you restart the service with systemctl --user restart chrome-remote-desktop.service can you check which user runs CRD? You can check with ps aux (the first column contains a user and the last one a command itself).

If it fails, perhaps try asking at stackexchange.

@LaFabriqueVirtuelle
Copy link

@nightuser @dmmat I can confirm the patch worked for me yesterday with CRD v92.0.4515.41-1 on Manjaro KDE and chrome-remote-desktop show expected behavior

I fresh installed CRD, then configured it headless and ran it from my phone (showed new blank Xorg session) then applied patch with patch command (from /opt/google/chrome-remote-desktop
sudo patch -Np1 -i ~/use_existing_session.patch), then added a file Xsession with only a 0 in it to ~/.config/chrome-remote-desktop and finally reconnected trough my phone to see my current Xorg session!

Thanks a lot for this patch btw ;)

@noseratio
Copy link

@nightuser, thank you for this patch. I wonder what prevents almighty Google from implementing such a useful feature in the first place.

@LeaT113
Copy link

LeaT113 commented Apr 23, 2022

Hello, this has not worked for me. I tried applying the patch by manually replacing the lines as well and it seems the original file has changed. The function def launch_session is no longer around line 774, but around line 519. Even then I was not able to get this working. Could you update the patch please?

The console output was:

can't find file to patch at input line 4
Perhaps you used the wrong -p or --strip option?

and then

File to patch: /opt/google/chrome-remote-desktop/chrome-remote-desktop
patching file /opt/google/chrome-remote-desktop/chrome-remote-desktop
Hunk #1 succeeded at 109 (offset 2 lines).
Hunk #2 FAILED at 776.
1 out of 2 hunks FAILED -- saving rejects to file /opt/google/chrome-remote-desktop/chrome-remote-desktop.rej

@nightuser
Copy link
Author

@LeaT113 Hi. I've updated the patch and it works now. If it still didn't work, make sure that you're using X11 (Wayland is not supported) and then follow the steps from this post—or just do a normal install with crd --setup if you haven't used CRD previously.

@jjmonteiro
Copy link

jjmonteiro commented May 30, 2022

Hi guys, was attempting this patch but got the following output. Any ideas?

patching file chrome-remote-desktop
patch unexpectedly ends in middle of line
Hunk #3 succeeded at 543 with fuzz 1.

edit:
Although the patch command seems to fail, the code changes were applied successfully but it still doesn't work afterwards.
what fixed it for me was the following (Debian 11):

FIRST_X_DISPLAY_NUMBER = 0
echo "0" > ~/.config/chrome-remote-desktop/Xsession
systemctl restart chrome-remote-desktop@"your_user".service

However, my remote session seems to be missing some privileges and I can't run certain applications..

@ihmcjacky
Copy link

ihmcjacky commented Jun 22, 2022

@nightuser I would like to thank you for the patch, I am now able to resume the same session when using chrome remote desktop, but 1 question, since I have 2 monitors connected, but I see 2 different resolutions when remote, how can I configure to use both screens with resolution 1920x1080? thanks again

image

@ihmcjacky
Copy link

Hi guys, was attempting this patch but got the following output. Any ideas?

patching file chrome-remote-desktop patch unexpectedly ends in middle of line Hunk #3 succeeded at 543 with fuzz 1.

edit: Although the patch command seems to fail, the code changes were applied successfully but it still doesn't work afterwards. what fixed it for me was the following (Debian 11):

FIRST_X_DISPLAY_NUMBER = 0 echo "0" > ~/.config/chrome-remote-desktop/Xsession systemctl restart chrome-remote-desktop@"your_user".service

However, my remote session seems to be missing some privileges and I can't run certain applications..

I encountered the same error as yours, I ended up manually patching the file myself without running the command

@dji-transpire
Copy link

It seems this is needed on Manjaro when using xfce4 as window manager. Whenever I try to use a version that starts a new xserver, crd stops after a minute saying it cannot open or connect to Xorg.

Does Manjaro constrain Xwindows to open just one desktop?

@hexates
Copy link

hexates commented Dec 11, 2023

Updated the gist.

@tokland: you can simplify things:

cd /opt/google/chrome-remote-desktop
sudo cp chrome-remote-desktop chrome-remote-desktop_bak # backup the original
sudo patch -Np1 -i /path/to/use_existing_session.patch
systemctl --user restart chrome-remote-desktop.service

You don't have to stop the service and you can patch the file inplace.

Hello!
I am currently trying to get this to work on the steamdeck, but despite followig these instructions I am not getting it work unfortunately.
Is it possible to get it working currently or am I out of luck?

@y8s
Copy link

y8s commented Jan 8, 2024

this patch is my hero. CRD breaks all the time and just running

cd /opt/google/chrome-remote-desktop
sudo cp chrome-remote-desktop chrome-remote-desktop_bak # backup the original
sudo patch -Np1 -i /path/to/use_existing_session.patch
systemctl --user restart chrome-remote-desktop.service

fixes it like magic.

version: 121.0.6167.13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment