pytest with moto fails with `botocore.exceptions.ClientError: An error occurred (InvalidClientTokenId) when calling the ... operation` (it is reaching real AWS).
IntermediateTroubleshootingConcurrency, async, tests, services & containers
Answer
Likely causes → The client was created outside the mock_aws context (at module import, in a session-scoped fixture created before the mock started), so it talks to real AWS with fake credentials.
Diagnose → Check where clients are created relative to mock_aws() / @mock_aws.
Fix → Create clients inside the mock (in fixtures that depend on the mock fixture), not at import time. Set fake credentials in an autouse fixture so a leak fails fast instead of using your real profile.
Prevent → No module-level clients; dependency-inject clients into functions.
More on Concurrency, async, tests, services & containers
`RuntimeError: asyncio.run() cannot be called from a running event loop` in a Jupyter notebook or inside an async framework.Intermediate`RuntimeWarning: coroutine 'fetch' was never awaited` and nothing happens.BeginnerAn asyncio health-checker is no faster than a sequential loop.Advanced`Task was destroyed but it is pending!` at shutdown.Advanced`OSError: [Errno 24] Too many open files` from an asyncio scanner with 20,000 targets.IntermediateA `ThreadPoolExecutor` job finishes "successfully" but half the work is missing, with no errors logged.Intermediate