Skip to content

Fix import crash on Python 3.13+ when getpass.getuser() has no OS user entry - #1078

Open
cristianchiriac wants to merge 3 commits into
aio-libs:mainfrom
cristianchiriac:fix/default-user-oserror-py313
Open

cristianchiriac wants to merge 3 commits into
aio-libs:mainfrom
cristianchiriac:fix/default-user-oserror-py313

Conversation

@cristianchiriac

Copy link
Copy Markdown

Fixes #1053.

Bug

As of Python 3.13, getpass.getuser() raises OSError instead of KeyError when there's no entry in the OS user database for the current uid (e.g. an arbitrary uid set inside a Docker container). The module-level fallback in aiomysql/connection.py only caught KeyError:

try:
    DEFAULT_USER = getpass.getuser()
except KeyError:
    DEFAULT_USER = "unknown"

so on Python 3.13+ in that environment, import aiomysql crashes outright with an unhandled OSError.

PyMySQL hit the identical issue and fixed it the same way (referenced in the issue): PyMySQL/PyMySQL@a1ac823

Fix

Catch OSError alongside KeyError.

Test plan

  • Added test_default_user_falls_back_when_getuser_fails, parametrized over both KeyError and OSError, monkeypatching getpass.getuser and reloading aiomysql.connection to exercise the module-level fallback directly.
  • Verified the test fails with the exact reported OSError against the unpatched code, and passes after the fix (both exception types).
  • I don't have a Linux environment to run this repo's full suite here (tests/conftest.py imports uvloop, which is Unix-only) — verified the fix and the new test's logic manually via direct importlib.reload outside pytest, and confirmed flake8 is clean on the changed file.

…r entry

Fixes aio-libs#1053. As of Python 3.13, getpass.getuser() raises OSError
instead of KeyError when there's no entry in the OS user database for
the current uid (e.g. an arbitrary uid in a container). The
module-level fallback only caught KeyError, so importing aiomysql
crashed outright in that environment.

Catch OSError too, matching the fix PyMySQL applied for the same
underlying getpass change (referenced in the issue).
@greptile-apps

greptile-apps Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Low risk] Handles a startup error in user detection.

No outstanding findings block merging.

Reviews (3) · Last reviewed commit: "test: check the DEFAULT_USER fallback in..."

Comment thread tests/test_connection.py Outdated
The test's cleanup reloaded aiomysql.connection before monkeypatch
undid the getpass.getuser patch, so the reload still observed the
broken getuser and left DEFAULT_USER stuck at "unknown" afterwards.
The reload also left aiomysql.Connection/aiomysql.connect pointing at
the pre-reload objects while aiomysql.connection.Connection/connect
were replaced, breaking identity checks for any test that runs after
this one.

Undo the monkeypatch before reloading, and reload the parent aiomysql
package too so its re-exported names match the reloaded submodule
again.
@cristianchiriac

Copy link
Copy Markdown
Author

Good catch — pushed a fix: undo the monkeypatch before reloading so the second reload picks the real getpass.getuser back up, and also reload the parent aiomysql package so aiomysql.Connection/aiomysql.connect stay identical to aiomysql.connection's after the test.

Comment thread tests/test_connection.py Outdated
raise exc_type("no such user")

monkeypatch.setattr("getpass.getuser", raise_exc)
importlib.reload(aiomysql_connection)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woah, this doesn't appear safe..

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, reloading modules during the test run isn't safe. The test now does the import in a separate Python process (f242454), so nothing in the test session is touched. It still fails without the fix.

This branch has not been deployed

No deployments
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.

Importing fails with OSError from getpass.getuser on Python 3.13+

2 participants