Skip to content
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

restore AsyncExitStack, and use it correctly #96

Merged
merged 3 commits into from
Jan 4, 2025

Conversation

graingert
Copy link
Contributor

this exit stack was recently removed, however it's absolutely critical it gets used - and correctly.

Comment on lines -60 to -61
await self.aclose()
await self._task_group.__aexit__(exc_type, exc_val, exc_tb)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before it was possible for this aclose to raise eg CancelledError, but then it would not be raised into the taskgroup so the exception would not be processed

await self.aclose()
await self._task_group.__aexit__(exc_type, exc_val, exc_tb)
async def __aexit__(self, exc_type, exc_val, exc_tb) -> bool | None:
return await self._exit_stack.__aexit__(exc_type, exc_val, exc_tb)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before the return code was not passed from TaskGroup.__aexit__ so if it suppressed/propagated cancellation it would not then be suppressed/propagated from ASGIWebSocketAsyncNetworkStream

self._task_group = await stack.enter_async_context(anyio.create_task_group())
self._task_group.start_soon(self._run)

await self.send({"type": "websocket.connect"})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if send was cancelled it would not propagate into the TaskGroup async context manager!

This was fixed by using two AsyncExitStacks and pop_all(), so all code is covered by a context manager and therefore the TaskGroup.

Comment on lines 62 to 63
if message["type"] == "websocket.close":
raise WebSocketDisconnect(message["code"], message.get("reason"))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aclose was run here and then later in the __aexit__, I now run it once using a callback in the AsyncExitStack

@frankie567
Copy link
Owner

Well, I suspected removing the exit stack would be a problem; I should I've trusted my gut feelings 😄 Thank you for this, and for the detailed rationale behind it.

May I ask you to run the linters (hatch run lint)? Should be good to go then :)

Copy link

codecov bot commented Jan 4, 2025

Codecov Report

Attention: Patch coverage is 95.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 98.56%. Comparing base (fedb69d) to head (5ca211a).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
httpx_ws/transport.py 95.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #96      +/-   ##
==========================================
- Coverage   98.72%   98.56%   -0.17%     
==========================================
  Files           5        5              
  Lines         548      556       +8     
==========================================
+ Hits          541      548       +7     
- Misses          7        8       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@frankie567 frankie567 merged commit f594786 into frankie567:main Jan 4, 2025
6 of 8 checks passed
@graingert graingert deleted the patch-1 branch January 4, 2025 13:33
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