From 55fc247d90e269b2d57d9d98f9edb9b448d2f196 Mon Sep 17 00:00:00 2001 From: Dabao21 <642011598@qq.com> Date: Sat, 25 Apr 2026 07:42:46 +0800 Subject: [PATCH] fix: handle os.getlogin() OSError in keychain.py os.getlogin() raises OSError when there is no controlling terminal (e.g. systemd services, Docker containers, CI environments). This caused the entire keychain module to fail on import. Now falls back to USER/USERNAME environment variables with a default. --- memory/keychain.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/memory/keychain.py b/memory/keychain.py index cb580dc6..ae96815f 100644 --- a/memory/keychain.py +++ b/memory/keychain.py @@ -2,7 +2,11 @@ import json, os, hashlib, pathlib _PATH = pathlib.Path.home() / "ga_keychain.enc" -_MASK = hashlib.sha256(f"{os.getlogin()}@ga_keychain".encode()).digest() +try: + _user = os.getlogin() +except OSError: + _user = os.environ.get('USER', os.environ.get('USERNAME', 'default')) +_MASK = hashlib.sha256(f"{_user}@ga_keychain".encode()).digest() def _xor(data: bytes) -> bytes: return bytes(b ^ _MASK[i % len(_MASK)] for i, b in enumerate(data))