Fix server streaming handler not cancelled on client disconnect by stefanvanburen · Pull Request #175 · connectrpc/connect-python

@stefanvanburen mentioned this pull request

Mar 17, 2026

@stefanvanburen

When a client disconnects during a server streaming RPC, the async
generator continued yielding indefinitely because receive() was never
consulted after the initial request was consumed.

Fix this by spawning a background task in the `EndpointServerStream`
case that monitors receive() for http.disconnect. When detected, an
Event is set; the streaming loop checks it between yields and raises
ConnectError(CANCELED) if set. The response stream is also explicitly
`aclose()`'d in the finally block so generator finally-clauses run
promptly rather than being deferred to GC.

Fixes #174.

Signed-off-by: Stefan VanBuren <svanburen@buf.build>

@stefanvanburen

Signed-off-by: Stefan VanBuren <svanburen@buf.build>

anuraaga

@stefanvanburen

Signed-off-by: Stefan VanBuren <svanburen@buf.build>

anuraaga

@stefanvanburen

;)

Signed-off-by: Stefan VanBuren <svanburen@buf.build>