# Build Reliable Python Async Services

Async Python code can leak sessions, block responses, and deadlock under load. This skill gives FastAPI and SQLAlchemy patterns for safer async database work.

## Install

```bash
npx skillstore add cjharmath/py-async-patterns
```

## Metadata

- Status: approved
- Slug: cjharmath-py-async-patterns
- Skillstore revision: r1
- Version status: missing
- Tree hash: 7473c9172b714a8b8dc35d35163bbb2ce9282b885a95aef70525a502c46f707e
- Author: CJHarmath
- GitHub username: CJHarmath
- License: MIT
- Repository: https://github.com/CJHarmath/claude-agents-skills/tree/main/skills/py-async-patterns
- Ref: dd4a3ef9f20ddf38830950b4bb713df96b431fd6
- Supported tools: Claude, Codex, Claude Code
- Audit status: complete
- Agent install advisory: allowed
- Manual install advisory: allowed
- Artifact signature: available
- Audit attestation: unavailable
- Human verification: not\_verified
- Risk factors: external\_commands, env\_access
- Quality score: 78
- Quality tier: bronze
- Public page: https://skillstore.pages.dev/skills/cjharmath-py-async-patterns
- Manifest: https://skillstore.pages.dev/api/skills/cjharmath-py-async-patterns/manifest

## Capabilities

- Explains request-scoped AsyncSession lifecycle patterns.
- Shows when to use concurrent or sequential database queries.
- Describes transaction commit, rollback, and refresh patterns.
- Covers connection pool and session closure pitfalls.
- Provides background task and deadlock avoidance guidance.
- Lists local checks for session leaks and missing awaits.

## Use Cases

- Improve FastAPI Database Endpoints: Use request-scoped sessions, safe commits, and refresh patterns while building async API handlers.
- Review Async Query Performance: Decide which queries can run concurrently and which queries must stay sequential because of data dependencies.
- Investigate Async Production Issues: Trace session leaks, connection timeouts, stale data, deadlocks, and slow endpoints with focused async patterns.

## Prompt Templates

### Explain Async Session Scope

```
Use py-async-patterns to explain how this FastAPI endpoint should scope AsyncSession usage. Identify any session leak risks.
```

### Fix Transaction Boundaries

```
Use py-async-patterns to review this async database write flow. Recommend commit, rollback, refresh, and transaction boundary changes.
```

### Optimize Query Concurrency

```
Use py-async-patterns to classify these async queries as independent or dependent. Suggest where gather is safe and where await order matters.
```

### Audit Async Failure Modes

```
Use py-async-patterns to audit this service for session leaks, pool exhaustion, stale data, deadlocks, and missing post-condition validation.
```

## Limitations

- It provides guidance and examples, not a runnable library.
- Examples must be adapted to each project schema and session factory.
- Detection commands are heuristic and may miss project-specific issues.
- It focuses on FastAPI and SQLAlchemy, not every async framework.

## Best Practices

- Scope each AsyncSession to one request or unit of work.
- Use explicit transactions when several writes must succeed together.
- Validate async post-conditions before returning user-facing results.

## Anti Patterns

- Using a global shared AsyncSession across requests.
- Running dependent database queries concurrently before required data exists.
- Starting fire-and-forget tasks without considering error handling.

## Security Audit

- Audited at: 2026-07-06T06:34:53.868\+00:00
- Summary: Static analysis flagged Markdown backticks, a placeholder DATABASE\_URL, and database model examples as security issues. Manual review found these are documentation-only patterns with no prompt injection, exfiltration intent, or hidden execution behavior.

## Stats

- Views: 213
- Downloads: 10
- Favorites: 0
- Popularity score: 0
