sysid / sse-starlette

BSD 3-Clause "New" or "Revised" License
560 stars 37 forks source link

Is GeneratorExit one of expected exceptions? #33

Closed jacek-jablonski closed 2 years ago

jacek-jablonski commented 2 years ago

Hi, I have some simple endpoint using sse:

async def subscribe(request: Request):
    topics = request.path_params["topics"]

    async def event_publisher(topics: List[str]):
        receiver = Receiver(topics)
        try:
            while True:
                try:
                    async with timeout(WAIT_FOR_NEW_MESSAGE_TIMEOUT):
                        message = await receiver.get_message()
                except TimeoutError:
                    continue

                yield message
        except CancelledError as e:
            logger.info(f"Disconnected from client (via refresh/close) {request.client}")
            raise e

    return EventSourceResponse(event_publisher(topics.split(",")), ping=config["SSE_KEEPALIVE"])

Every once in a while I notice that GeneratorExit shows up at this endpoint. Is this an expected behavior? I thought client hang-up is handled by CanceledError. So where does this GeneratorExit come from?

sysid commented 2 years ago

Hello @jacek-jablonski, my understanding of client hang-up concurs with yours. I am afraid I cannot answer your question.