Skip to content

Make fastapi-sqla session factory setup idempotent#1238

Merged
anfimovdm merged 1 commit into
masterfrom
idempotent-sqla-setup
Jun 28, 2026
Merged

Make fastapi-sqla session factory setup idempotent#1238
anfimovdm merged 1 commit into
masterfrom
idempotent-sqla-setup

Conversation

@anfimovdm

Copy link
Copy Markdown
Contributor

Skip re-registering session factories that already exist instead of disposing and recreating their engines on every setup_all() call. Since setup_all() runs on every dramatiq task invocation, the previous behaviour needlessly tore down and rebuilt connection pools each time.

Skip re-registering session factories that already exist instead of
disposing and recreating their engines on every setup_all() call.
Every service runs on a single, long-lived event loop (the dramatiq
workers share alws.dramatiq.event_loop, the web app has one loop per
process), so the engines stay valid for the process lifetime and
disposing/recreating them on every task only churns the asyncpg pool.

The test suite runs each module on its own event loop, so reuse a
cached factory across modules would raise 'got Future attached to a
different loop'. Reset the factories per module in the create_tables
fixture so setup_all() rebuilds the engines on the current loop.
@anfimovdm anfimovdm force-pushed the idempotent-sqla-setup branch from bdb26dd to 50b8258 Compare June 28, 2026 14:10
@anfimovdm anfimovdm merged commit b1bd9c3 into master Jun 28, 2026
3 checks passed
@anfimovdm anfimovdm deleted the idempotent-sqla-setup branch June 28, 2026 14:13
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