From 7b604384f58cc9ad08e6379b258793f92a33aad5 Mon Sep 17 00:00:00 2001 From: Felix Moessbauer Date: Mon, 1 Jul 2024 21:11:11 +0200 Subject: [PATCH] fix: always terminate host component when browser is terminated On chrome and chromium, the native part is not reliably terminated when the browser is terminated. As we also cannot reliably send a message to the host part on termination of the browser, there is no direct way to let the host script known. In theory, the script should terminate once stdio is closed, but in practice this is not always the case, leading to orphaned processes. To fix this, we use prctl to register a PDEATHSIG handler. Signed-off-by: Felix Moessbauer --- linux-entra-sso.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/linux-entra-sso.py b/linux-entra-sso.py index a5ff579..2421516 100755 --- a/linux-entra-sso.py +++ b/linux-entra-sso.py @@ -12,6 +12,8 @@ import json import struct import uuid +import ctypes +from signal import SIGINT from threading import Thread, Lock from gi.repository import GLib from pydbus import SessionBus @@ -25,6 +27,8 @@ # dbus start service reply codes START_REPLY_SUCCESS = 1 START_REPLY_ALREADY_RUNNING = 2 +# prctl constants +PR_SET_PDEATHSIG = 1 class NativeMessaging: @@ -186,8 +190,17 @@ def run_dbus_monitor(): loop = GLib.MainLoop() loop.run() + def register_terminate_with_parent(): + libc = ctypes.CDLL("libc.so.6") + libc.prctl(PR_SET_PDEATHSIG, SIGINT, 0, 0, 0) + print("Running as browser plugin.", file=sys.stderr) print("For interactive mode, start with --interactive", file=sys.stderr) + + # on chrome and chromium, the parent process does not reliably + # terminate the plugin process when the parent process is killed. + register_terminate_with_parent() + ssomib = SsoMib(daemon=True) ssomib.on_broker_state_changed(notify_state_change) monitor = Thread(target=run_dbus_monitor)