Skip to content

Commit

Permalink
Fixed InvalidStateError on shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
qstokkink committed Sep 16, 2024
1 parent 79a1509 commit 26e1ce4
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/tribler/core/restapi/events_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import json
import time
from asyncio import CancelledError, Event, Queue
from asyncio import CancelledError, Event, Future, Queue
from contextlib import suppress
from traceback import format_exception
from typing import TYPE_CHECKING, TypedDict
Expand Down Expand Up @@ -251,6 +251,12 @@ async def get_events(self, request: Request) -> web.StreamResponse:
else:
self._logger.info("Event stream was closed due to shutdown")

# A ``shutdown()`` on our parent may have cancelled ``_handler_waiter`` before this method returns.
# If we leave this be, an error will be raised if the ``Future`` result is set after this method returns.
# See: https://github.com/Tribler/tribler/issues/8156
if request.protocol._handler_waiter and request.protocol._handler_waiter.cancelled(): # noqa: SLF001
request.protocol._handler_waiter = Future() # noqa: SLF001

# See: https://github.com/Tribler/tribler/pull/7906
with suppress(ValueError):
self.events_responses.remove(response)
Expand Down

0 comments on commit 26e1ce4

Please sign in to comment.