Skip to content

fix: handle os.getlogin() OSError in keychain.py#166

Open
Dabao21 wants to merge 1 commit intolsdefine:mainfrom
Dabao21:fix/keychain-getlogin-oserror
Open

fix: handle os.getlogin() OSError in keychain.py#166
Dabao21 wants to merge 1 commit intolsdefine:mainfrom
Dabao21:fix/keychain-getlogin-oserror

Conversation

@Dabao21
Copy link
Copy Markdown

@Dabao21 Dabao21 commented Apr 25, 2026

Problem

In memory/keychain.py, os.getlogin() is called at module import time to generate the encryption mask:

_MASK = hashlib.sha256(f"{os.getlogin()}@ga_keychain".encode()).digest()

os.getlogin() raises OSError when there is no controlling terminal. This happens in:

  • Docker containers
  • systemd services
  • CI/CD environments (GitHub Actions, etc.)
  • Some WSL configurations

When this fails, the entire keychain module cannot be imported, breaking any code that depends on it.

Fix

Catch OSError and fall back to environment variables:

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()

USER is the standard variable on Linux/macOS, USERNAME on Windows. The 'default' fallback ensures the module always loads.

Impact

  • keychain module now works in headless/CI environments
  • Encryption mask remains deterministic per-user (same user → same mask)
  • No behavior change for normal terminal sessions

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant