Skip to content

Fix parallel test failures caused by lru_cache on get_aws_config() #50

@laurencegoolsby

Description

@laurencegoolsby

Context

The get_aws_config() function in env.py uses @lru_cache to avoid re-reading environment variables on every call. This works with make test but causes test failures when running in parallel (pytest -n auto).

In short:

  • pytest-xdist runs tests across multiple workers
  • lru_cache can cache config from one test's fixture setup before another test's monkeypatch.setenv has executed
  • The autouse clear_config_cache fixture in conftest.py clears the cache between tests
  • Fixture ordering across parallel workers is not guaranteed

Tests pass reliably when run sequentially (pytest -x -v).

Implementation

Options to investigate:

  1. Use pytest-xdist worker-scoped fixtures - ensure each worker gets its own config initialization with correct env vars before any tests run
  2. Lazy singleton pattern - replace lru_cache with a simple module-level _config: AWSEnvConfig | None = None that gets set on first access and cleared explicitly in tests
  3. Accept sequential test execution — if parallel test speed is not a priority, document that tests must run sequentially and update CI accordingly

Recommended: Option 2 - simplest change, no new dependencies, explicit control over cache lifecycle in tests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions