Skip to content

Instantly share code, notes, and snippets.

@nightuser
Last active February 20, 2025 23:48
Show Gist options
  • Save nightuser/2ec1b91a66ec33ef0a0a67b6c570eb40 to your computer and use it in GitHub Desktop.
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 2024-03-27 16:03:20.518579015 +0000
+++ b/chrome-remote-desktop 2024-03-27 16:17:58.241912847 +0000
@@ -110,6 +110,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
@@ -738,16 +740,33 @@
return True
return False
+ def _use_existing_session(self):
+ with open(EXISTING_X_DISPLAY_FILE_PATH) as fh:
+ try:
+ display_raw = fh.readline().rstrip()
+ display = int(display_raw)
+ except ValueError:
+ logging.error("Display must be a number; got: '%s'" % display_raw)
+ 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
+
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
that time has passed."""
logging.info("Setting up and launching session")
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,
@JovannMC
Copy link

JovannMC commented Sep 27, 2024

Below is the one-liner from @mike-callahan post and individual commands, modified to create the Xsession file as well. I tested it on Arch Linux with chrome-remote-desktop version 128.0.6613.44 and it worked.

@jack9603301 Audio doesn't work for me either.

echo $DISPLAY | tr -d ':' >> ~/.config/chrome-remote-desktop/Xsession && cd /opt/google/chrome-remote-desktop && sudo cp chrome-remote-desktop chrome-remote-desktop_bak && sudo wget -qO- "https://gist.githubusercontent.com/nightuser/2ec1b91a66ec33ef0a0a67b6c570eb40/raw/b09250dcc9a28f09de93192a948dffb4963c2aca/use_existing_session.patch" | tail -n +5 > ~/use_existing_session.patch && sudo patch -Np1 -i ~/use_existing_session.patch && rm ~/use_existing_session.patch && systemctl --user restart chrome-remote-desktop.service && systemctl --user status chrome-remote-desktop.service
echo $DISPLAY | tr -d ':' >> ~/.config/chrome-remote-desktop/Xsession && cd /opt/google/chrome-remote-desktop
sudo cp chrome-remote-desktop chrome-remote-desktop_bak
sudo wget -qO- "https://gist.githubusercontent.com/nightuser/2ec1b91a66ec33ef0a0a67b6c570eb40/raw/b09250dcc9a28f09de93192a948dffb4963c2aca/use_existing_session.patch" | tail -n +5 > ~/use_existing_session.patch
sudo patch -Np1 -i ~/use_existing_session.patch
rm ~/use_existing_session.patch
systemctl --user restart chrome-remote-desktop.service
systemctl --user status chrome-remote-desktop.service

I already had the host configured before applying the patch and for some reason it no longer appeared in https://remotedesktop.google.com/access. I had to do again the entire process from https://remotedesktop.google.com/headless

This worked for me (Linux Mint 22, version 129.0.6668.14) with some tweaks.

I had issues with chrome-remote-desktop erroring out, which I solved by changing ~/.config/chrome-remote-desktop/Xsession's contents to be 0 instead of 0.0. I also had to run the systemctl commands without --user and appending @$USER to the commands, so the last two commands became the following:

systemctl restart chrome-remote-desktop@$USER.service
systemctl status chrome-remote-desktop@$USER.service

@Sectimus
Copy link

@JovannMC I hope you are aware of the rm ~/ bomb in that one liner of yours.

@Sectimus
Copy link

Sectimus commented Oct 17, 2024

I wonder if it is possible for a similar fix under wayland, given that without the patch you can use remote desktop without issue... albeit still under a different session. :p

@JovannMC
Copy link

@JovannMC I hope you are aware of the rm ~/ bomb in that one liner of yours.

that isn't me!! that was from the quote
but yeah, that almost got me, @SitramSoft might wanna edit both the one liner and individual commands πŸ˜…

@SitramSoft
Copy link

@JovannMC I hope you are aware of the rm ~/ bomb in that one liner of yours.

that isn't me!! that was from the quote but yeah, that almost got me, @SitramSoft might wanna edit both the one liner and individual commands πŸ˜…

😱 Thanks for pointing out that mistake. It should be fixed now in both places.

I hope nobody deleted their home directory with that command πŸ˜‚

@y8s
Copy link

y8s commented Oct 17, 2024

@JovannMC I hope you are aware of the rm ~/ bomb in that one liner of yours.

that isn't me!! that was from the quote but yeah, that almost got me, @SitramSoft might wanna edit both the one liner and individual commands πŸ˜…

it's still in your quote reply. the sloppy readers like me will totally copypasta themselves into doom.

@JovannMC
Copy link

@JovannMC I hope you are aware of the rm ~/ bomb in that one liner of yours.

that isn't me!! that was from the quote but yeah, that almost got me, @SitramSoft might wanna edit both the one liner and individual commands πŸ˜…

it's still in your quote reply. the sloppy readers like me will totally copypasta themselves into doom.

fixed also

@JovannMC I hope you are aware of the rm ~/ bomb in that one liner of yours.

that isn't me!! that was from the quote but yeah, that almost got me, @SitramSoft might wanna edit both the one liner and individual commands πŸ˜…

😱 Thanks for pointing out that mistake. It should be fixed now in both places.

I hope nobody deleted their home directory with that command πŸ˜‚

iirc for me it tried to but didn't really do anything (esp since it didnt have -rf thankfully)

@Lamphobic
Copy link

Lamphobic commented Feb 20, 2025

FYI @JovannMC the gist referenced in your expanded one liner is from 2022. It's not the current patch.

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