From 362dbf038b672ee23246f342911a526cafd71045 Mon Sep 17 00:00:00 2001 From: Salman Date: Tue, 24 Jan 2023 21:09:55 +0000 Subject: [PATCH] linux_me2me_host.py: Quit gnome-session properly When the gnome-session process is killed, it can cause the service restart to halt at times. This change tries to stop the gnome-session using gnome-session-quit instead. Bug: chromium:1291247 Change-Id: Iee4763ab86fd7bf73518dfeae2a8ace8950e47cc Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4192107 Reviewed-by: Lambros Lambrou Auto-Submit: Salman Malik Commit-Queue: Salman Malik Cr-Commit-Position: refs/heads/main@{#1096363} --- remoting/host/linux/linux_me2me_host.py | 38 ++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/remoting/host/linux/linux_me2me_host.py b/remoting/host/linux/linux_me2me_host.py index 8442489fbee74e..c47dda69fd3236 100755 --- a/remoting/host/linux/linux_me2me_host.py +++ b/remoting/host/linux/linux_me2me_host.py @@ -159,9 +159,12 @@ # wayland compositor/server for clients to connect to. RUNTIME_DIR_TEMPLATE = "/run/user/%s" -# Binary name for the gnome-session. +# Binary name for `gnome-session`. GNOME_SESSION = "gnome-session" +# Binary name for `gnome-session-quit`. +GNOME_SESSION_QUIT = "gnome-session-quit" + # Globals needed by the atexit cleanup() handler. g_desktop = None g_host_hash = hashlib.md5(socket.gethostname().encode()).hexdigest() @@ -933,6 +936,39 @@ def _wait_for_setup_before_host_launch(self): return self._wait_for_wayland_compositor_running() def cleanup(self): + if self.host_proc is not None: + logging.info("Sending SIGTERM to host proc (pid=%s)", self.host_proc.pid) + try: + psutil_proc = psutil.Process(self.host_proc.pid) + psutil_proc.terminate() + + # Use a short timeout, to avoid delaying service shutdown if the + # process refuses to die for some reason. + psutil_proc.wait(timeout=10) + except psutil.TimeoutExpired: + logging.error("Timed out - sending SIGKILL") + psutil_proc.kill() + except psutil.Error: + logging.error("Error terminating process") + self.host_proc = None + + # We currently only support gnome-session (which is currently managed) + # by CRD itself. + logging.info("Executing %s" % GNOME_SESSION_QUIT) + if shutil.which(GNOME_SESSION_QUIT): + cleanup_proc = subprocess.Popen( + [GNOME_SESSION_QUIT, "--force", "--no-prompt"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + env=self.child_env) + stdout, stderr = cleanup_proc.communicate() + if stderr: + logging.error("Failed to execute %s:\n%s" % + (GNOME_SESSION_QUIT, stderr)) + self.session_proc = None + else: + logging.warning("No %s found on the system" % GNOME_SESSION_QUIT) + super(WaylandDesktop, self).cleanup() if self._wayland_socket: full_socket_path = os.path.join(self.runtime_dir, self._wayland_socket)