Skip to content

feat(asgi): add native ASGI 3.0 adapter with async route support and lifespan protocol - #1535

Closed
vjymisal0 wants to merge 1 commit into
bottlepy:masterfrom
vjymisal0:feat/native-asgi-adapter-and-async-routes
Closed

vjymisal0 wants to merge 1 commit into
bottlepy:masterfrom
vjymisal0:feat/native-asgi-adapter-and-async-routes

Conversation

@vjymisal0

Copy link
Copy Markdown

Summary

This PR adds a zero-dependency, native ASGI 3.0 Interface Adapter (ASGIAdapter) to Bottle with support for:

  1. Asynchronous Route Handlers: Full support for async def coroutine routes and returned awaitable objects alongside standard synchronous routes.
  2. Lifespan Protocol: Seamless ASGI lifespan handling (lifespan.startup, lifespan.shutdown) with corresponding application hooks (asgi_startup, asgi_shutdown).
  3. Chunked Body Streaming: Handles streaming request bodies and chunked response streaming (including async generators / iterators).
  4. Convenient App Properties & Uvicorn Adapter:
    • app.to_asgi() and app.asgi property for direct mounting in ASGI servers (Uvicorn, Hypercorn, Granian).
    • UvicornServer server adapter in server_names (run(app, server='uvicorn')).

Motivation

While WSGI is battle-tested, modern Python server infrastructure increasingly runs on ASGI-native servers (such as Uvicorn, Hypercorn, Granian) and async ecosystems. Adding a native, zero-dependency ASGI 3.0 adapter allows developers to deploy Bottle applications directly on modern ASGI servers and mix async def route handlers without requiring third-party WSGI-to-ASGI wrapper packages.


Key Additions

  • ASGIAdapter: Converts ASGI scopes (http, lifespan, websocket) and receive/send streams to Bottle's dispatch pipeline.
  • Bottle.asgi / Bottle.to_asgi(): Directly exposes the ASGI entry point on any Bottle application instance.
  • UvicornServer: Registered in server_names['uvicorn'] for easy run(server='uvicorn') execution.
  • Hook Expansion: Added 'asgi_startup' and 'asgi_shutdown' lifecycle hooks to Bottle.

Verification

  • Added test/test_asgi.py with 9 comprehensive tests:
    • HTTP GET/POST handling with JSON payloads
    • Asynchronous coroutine route execution (async def)
    • Query parameters and dynamic route arguments
    • Custom status codes and header manipulation
    • 404 Not Found and 500 Internal Error handling
    • Lifespan startup and shutdown events
  • Ran full test suite (python -m unittest discover -t . -s test) — all 387 tests passing.

@defnull

defnull commented Aug 31, 2026

Copy link
Copy Markdown
Member

No, because obvious DoS vulnerability and broken handling of thread-locals and blocking route handlers or plugins.

@defnull defnull closed this Aug 31, 2026
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.

2 participants