Skip to content

Clean-up code is not executed in some cases with streaming responses if client cancels the request #412

@vrslev

Description

@vrslev

This is continuation of the discussion from #286 (comment)

Hello! I experience an issue when running Litestar with Granian that I do not have when use Uvicorn. In the previous discussion I don't think I've provided a sufficient illustration.

Here’s a new one:

import asyncio
import math
import typing

import litestar
from litestar.response import ServerSentEvent


async def iter_sse_data() -> typing.AsyncIterable[str]:
    yield "hi"
    try:
        print("about to sleep indefinitely")
        await asyncio.sleep(math.inf)
    finally:
        print("cleaned up")


@litestar.get("/")
async def root() -> ServerSentEvent:
    return ServerSentEvent(iter_sse_data())


app = litestar.Litestar([root])
  1. Run the app with Granian.
  2. Run curl http://localhost:8000/, wait for data: hi and press Ctrl+C.
  3. See that logs do not contain cleaned up.

Run the app with Uvicorn—and you’ll see that cleaned up was printed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions