Skip to content

Conversation

@cofin
Copy link
Member

@cofin cofin commented Nov 16, 2025

Summary

Ensure the Litestar SQLSpecPlugin connection provider unwraps sync context managers before injecting connections.

Closes #238

The Problem

  • SqliteConfig (and other sync adapters) return sync context managers from provide_connection()
  • connection_provider_maker() treated any non-async context managers as ready-to-use connections, so Litestar stored _GeneratorContextManager objects.
  • Requests failed with AttributeError: '_GeneratorContextManager' object has no attribute 'cursor' when the driver tried to call .cursor().

The Solution

  • Wrap sync context managers with with_ensure_async_() so the provider always awaits the context manager path and stores the actual connection.
  • Preserve the existing awaitable/object path for adapters that already return concrete connections.
  • Add a regression test that exercises SqliteConfig to ensure sync context managers yield proper driver connections and clean up scope state.

Key Features

  • Handles both async and sync connection context managers in connection_provider_maker().
  • Reuses the shared sync-to-async helper instead of duplicating wrapper logic in the plugin.
  • Adds a Litestar handler test confirming SqliteConfig requests receive real driver connections and scope cleanup occurs.

@cofin cofin changed the title Fix Litestar sync connection provider fix(litestar): correctly handle sync context manager Nov 16, 2025
@cofin cofin merged commit 111d619 into main Nov 16, 2025
10 of 11 checks passed
@cofin cofin deleted the fix-litestar-sync-connection branch November 16, 2025 00:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants