Skip to content

Implement Connect-over-WebSockets - #466

Open
anuraaga wants to merge 23 commits into
connectrpc:mainfrom
anuraaga:websockets-asgi
Open

anuraaga wants to merge 23 commits into
connectrpc:mainfrom
anuraaga:websockets-asgi

Conversation

@anuraaga

Copy link
Copy Markdown
Collaborator

This implements the pending spec for websockets support in the ASGI server. It takes an approach of wrapping the websockets stream into the normal connect one, while we can consider an abstraction to use a more websockets-native control flow, I feel the wrapping approach works pretty well already.

connectrpc/connectrpc.com#452

@anuraaga
anuraaga marked this pull request as draft November 12, 2026 14:29
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
@anuraaga
anuraaga marked this pull request as ready for review November 12, 2026 14:37
@anuraaga
anuraaga requested a review from a team November 12, 2026 14:37
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>

@stefanvanburen stefanvanburen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Took a pass against the RFC in connectrpc/connectrpc.com#452 — the websocket conformance suites at 63de2c4 pass for me locally on uvicorn, pyvoy, and pyvoy-trio (442/442 each), but a few things slip past them, inline. None of them are blocking IMO (the origin ones all fail closed), so approving; fine to fix here or follow up, whichever's easier.

Couple of RFC SHOULDs that I don't think need to block this, but ought to at least get a docs note: without connect-timeout-ms the wait for the client's M is unbounded (§11.2 wants a server deadline), and no_context_takeover (§10) is entirely up to the ASGI server's config, so I think users need to be told to set it (like server.py does for pyvoy / uvicorn).

Comment thread src/connectrpc/_server_async.py Outdated
self._compressions = resolve_compressions(compressions)
codecs = codecs if codecs is not None else get_default_codecs()
self._codecs = {codec.name(): codec for codec in codecs}
for pattern in websockets_allowed_origins:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

websockets_allowed_origins is iterated twice here, so a generator validates fine and then ends up as ():

app = HaberdasherASGIApplication(svc, websockets_allowed_origins=(o for o in ["app.example.org"]))
assert app._websockets_allowed_origins == ()  # every cross-origin handshake then 403s

Probably just want to tuple(...) it once up front.

Comment thread src/connectrpc/_server_async.py Outdated
self._compressions = resolve_compressions(compressions)
codecs = codecs if codecs is not None else get_default_codecs()
self._codecs = {codec.name(): codec for codec in codecs}
for pattern in websockets_allowed_origins:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also, I imagine folks will copy their CORS origins over here — "https://app.example.org" passes validation, but _origin_allowed only compares the authority so it can never match, and every handshake from that origin gets a 403. Maybe reject patterns containing :// (or /) with the same ValueError?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

When looking at starlette's docs, I didn't think people generally include scheme. A validation error for it seems to make sense anyways though

Comment thread src/connectrpc/_server_websocket.py Outdated
value = value_bytes.decode()
if name in ("content-type", CONNECT_STREAMING_HEADER_COMPRESSION):
continue
if name.startswith("trailer-"):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This stashes trailer-* headers on every response, but they only get sent back out in _send_error_end, so on a 200 they're just dropped. e.g. a handler setting ctx.response_headers["trailer-id"] = "x" on MakeHat gives ['M{}', 'B{"size":10}', 'S{}']. Admittedly an edge case (and trailer- is already ambiguous for Connect unary), but I think it should only strip them when _error_status is set?

# RPCs also use the streaming message flow.
if (
isinstance(endpoint, EndpointUnary)
and not is_websocket

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

On trio, routing unary through _call_stream means the disconnect watch only sets the flag (the asyncio watch always cancels), so a unary handler blocked on something slow runs to completion after the client goes away — in a quick test with a 2s sleep and a 1001 disconnect, the handler ran the full 2s on trio, cancelled immediately on asyncio. Same as gRPC unary on trio today I think, so not new, but §8 does say the server MUST treat it as cancelled. Maybe pass cancel=True to the watch when the endpoint is unary?

def _stream_error(
state: _StreamState, close_code: int, code: Code, message: str
) -> ConnectError:
state.close_code = close_code

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Setting close_code here, when the error is created rather than when it ends the RPC, means a client-stream / bidi handler that catches the request error and returns normally still gets a 1009 (or 1002 / 1003) close after a successful S{}. Repro: MakeFlexibleHat that does try: async for _ in request: ... except ConnectError: pass, read_max_bytes=8, and an oversized B → S{} then close 1009. §13 only has those codes after an S carrying the error, so I think the send adapter ought to only use the override close code when the S actually has an error.

Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
@anuraaga

Copy link
Copy Markdown
Collaborator Author

Couple of RFC SHOULDs that I don't think need to block this, but ought to at least get a docs note:

Yup I think we'll want to add these to Deployment in the connectrpc.com docs

Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
…nto websockets-asgi

# Conflicts:
#	conformance/test/gen/connectrpc/conformance/v1/service_connect.py
#	connectrpc-grpchealth/connectrpc_grpchealth/_gen/grpc/health/v1/health_connect.py
#	connectrpc-grpcreflect/connectrpc_grpcreflect/_gen/grpc/reflection/v1/reflection_connect.py
#	connectrpc-grpcreflect/connectrpc_grpcreflect/_gen/grpc/reflection/v1alpha/reflection_connect.py
#	connectrpc-grpcreflect/test/connectrpc/example/deprecated_connect.py
#	connectrpc-grpcreflect/test/connectrpc/example/empty_connect.py
#	connectrpc-grpcreflect/test/connectrpc/example/haberdasher_connect.py
#	example/example/gen/connectrpc/eliza/v1/eliza_connect.py
#	protoc-gen-connectrpc/protoc_gen_connectrpc/__init__.py
#	src/connectrpc/_protocol_connect.py
#	src/connectrpc/_protocol_grpc.py
#	src/connectrpc/_server_async.py
#	test/connectrpc/example/deprecated_connect.py
#	test/connectrpc/example/empty_connect.py
#	test/connectrpc/example/haberdasher_connect.py
#	test/google_compat/connectrpc/example/deprecated_connect.py
#	test/google_compat/connectrpc/example/empty_connect.py
#	test/google_compat/connectrpc/example/haberdasher_connect.py
#	test/test_server_protocols.py
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
@anuraaga

Copy link
Copy Markdown
Collaborator Author

Pending conformance bump

Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>

This branch has not been deployed

No deployments
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