-
Notifications
You must be signed in to change notification settings - Fork 2
[bug] Adding explicit CancelledError handlers during async waiting loops #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
blast-hardcheese
merged 14 commits into
main
from
dstewart/bug/respect-task-cancellation
May 7, 2025
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
32b169a
Adding explicit CancelledError handlers during async waiting loops
blast-hardcheese b2ad51b
re-raise should not be "raise e"
blast-hardcheese 46f65ee
Clear out the "connection" local
blast-hardcheese 250c478
Making room
blast-hardcheese 455ba7f
Renaming
blast-hardcheese b7074c6
Adding a test for cancelling upload
blast-hardcheese 6b2b84b
Moving fixtures out
blast-hardcheese b518386
Moving test_upload_cancel out
blast-hardcheese a93b0e9
Consolidating
blast-hardcheese d6cda64
Adding an RPC cancellation test
blast-hardcheese b5971b6
Add a subscription test
blast-hardcheese 31084f4
Propagating stream_id into logs
blast-hardcheese 3ce93d6
Adding stream cancel test
blast-hardcheese e9d44cd
method names
blast-hardcheese File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import asyncio | ||
| from typing import ( | ||
| AsyncIterator, | ||
| Awaitable, | ||
| Callable, | ||
| Literal, | ||
| TypeAlias, | ||
| TypedDict, | ||
| ) | ||
|
|
||
| import pytest | ||
| from websockets import ConnectionClosed, ConnectionClosedOK, Data | ||
| from websockets.asyncio.server import ServerConnection, serve | ||
|
|
||
| from replit_river.transport_options import UriAndMetadata | ||
|
|
||
| WsServerFixture: TypeAlias = tuple[ | ||
| Callable[[], Awaitable[UriAndMetadata[None]]], | ||
| asyncio.Queue[bytes], | ||
| Callable[[], ServerConnection | None], | ||
| ] | ||
|
|
||
|
|
||
| class OuterPayload[A](TypedDict): | ||
| ok: Literal[True] | ||
| payload: A | ||
|
|
||
|
|
||
| class _WsServerState(TypedDict): | ||
| ipv4_laddr: tuple[str, int] | None | ||
|
|
||
|
|
||
| async def _ws_server_internal( | ||
| recv: asyncio.Queue[bytes], | ||
| set_conn: Callable[[ServerConnection], None], | ||
| state: _WsServerState, | ||
| ) -> AsyncIterator[None]: | ||
| async def handle(websocket: ServerConnection) -> None: | ||
| set_conn(websocket) | ||
| datagram: Data | ||
| try: | ||
| while datagram := await websocket.recv(decode=False): | ||
| if isinstance(datagram, str): | ||
| continue | ||
| await recv.put(datagram) | ||
| except ConnectionClosedOK: | ||
| pass | ||
| except ConnectionClosed: | ||
| pass | ||
|
|
||
| port: int | None = None | ||
| if state["ipv4_laddr"]: | ||
| port = state["ipv4_laddr"][1] | ||
| async with serve(handle, "localhost", port=port) as server: | ||
| for sock in server.sockets: | ||
| if (pair := sock.getsockname())[0] == "127.0.0.1": | ||
| if state["ipv4_laddr"] is None: | ||
| state["ipv4_laddr"] = pair | ||
| serve_forever = asyncio.create_task(server.serve_forever()) | ||
| yield None | ||
| server.close() | ||
| await server.wait_closed() | ||
| # "serve_forever" should always be done after wait_closed finishes | ||
| assert serve_forever.done() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| async def ws_server() -> AsyncIterator[WsServerFixture]: | ||
| recv: asyncio.Queue[bytes] = asyncio.Queue(maxsize=1) | ||
| connection: ServerConnection | None = None | ||
| state: _WsServerState = {"ipv4_laddr": None} | ||
|
|
||
| def set_conn(new_conn: ServerConnection) -> None: | ||
| nonlocal connection | ||
| connection = new_conn | ||
|
|
||
| server_generator = _ws_server_internal(recv, set_conn, state) | ||
| await anext(server_generator) | ||
|
|
||
| async def urimeta() -> UriAndMetadata[None]: | ||
| ipv4_laddr = state["ipv4_laddr"] | ||
| assert ipv4_laddr | ||
| return UriAndMetadata(uri="ws://%s:%d" % ipv4_laddr, metadata=None) | ||
|
|
||
| yield (urimeta, recv, lambda: connection) | ||
|
|
||
| connection = None | ||
|
|
||
| try: | ||
| await anext(server_generator) | ||
| except StopAsyncIteration: | ||
| pass |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.