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

Handle ping/pong events in ASGIWebSocketAsyncNetworkStream #59

Closed
wants to merge 1 commit into from

Conversation

dmontagu
Copy link

We've been occasionally hitting errors in CI apparently due to our ASGI app receiving Ping events in our tests, and hitting the error just below the lines added in this PR.

I don't know if there's a reason not to make this change but figured I would propose it. If there's a better solution than what is proposed here, any insight would be appreciated.

@frankie567
Copy link
Owner

frankie567 commented Feb 22, 2024

Hey @dmontagu 👋

Good catch indeed. I think the reason why I didn't implement Ping/Pong handling in the transport is because it's not part of the ASGI specification.

I even have a unit test that was relying on Ping message to check the UnhandledWebSocketEvent exception 😅

async def test_write_unhandled_event(self, scope: Scope):
async def app(scope, receive, send):
await send({"type": "websocket.accept"})
await receive()
connection = wsproto.connection.Connection(wsproto.connection.CLIENT)
async with ASGIWebSocketAsyncNetworkStream(app, scope) as (stream, _):
with pytest.raises(UnhandledWebSocketEvent):
ping_event = wsproto.events.Ping(b"PING")
await stream.write(connection.send(ping_event))


The easiest solution right now is to set the keepalive_ping_interval_seconds parameter to None on the client to disable the keepalive ping, which should be okay in a CI context.

That being said, as it's enabled by default, it's true that it provokes surprising errors with the ASGI transport. I think it would probably better to automatically disable it if we detect that we work with the ASGI transport rather than trying to handle the ping. What do you think?

Out of curiosity, in which project are you using it? 🙂

@Kludex
Copy link

Kludex commented Feb 22, 2024

That being said, as it's enabled by default, it's true that it provokes surprising errors with the ASGI transport. I think it would probably better to automatically disable it if we detect that we work with the ASGI transport rather than trying to handle the ping. What do you think?

Yes.

Out of curiosity, in which project are you using it? 🙂

😉

@frankie567
Copy link
Owner

I made a PR with my proposed solution: #63

If that looks good for you, then I'll merge it, and we can close the current one.

@frankie567
Copy link
Owner

So, I close this in favor of #63. Will make a release in a moment.

@all-contributors add @dmontagu for bug

Copy link
Contributor

@frankie567

I've put up a pull request to add @dmontagu! 🎉

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.

3 participants