Skip to content

fix(streaming): fail fast on HTTP handshake rejections with legacy websockets - #370

Open
chinesepowered wants to merge 1 commit into
AssemblyAI:masterfrom
chinesepowered:fix/async-legacy-websockets-401-347
Open

chinesepowered wants to merge 1 commit into
AssemblyAI:masterfrom
chinesepowered:fix/async-legacy-websockets-401-347

Conversation

@chinesepowered

Copy link
Copy Markdown

What changes

On websockets < 13 (allowed by setup.py), AsyncRealTimeTranscriber falls back to the legacy client, which raises InvalidStatusCode for an HTTP handshake rejection rather than InvalidStatus. That case is now treated like InvalidStatus: not retried, and reported with the HTTP status, as RealTimeTranscriberOptions documents ("HTTP-level rejections (auth, quota, bad request) are never retried"). The status is read from exc.status_code (legacy) and otherwise from exc.response.status_code. InvalidStatusCode is referenced only inside the < 13 import fallback, so newer websockets never touch the deprecated name.

Fixes #347

Before and after

websockets 11.0.3, server answers the handshake with HTTP 401:

before: 3 handshakes, ~1 s, on_error (None, "Connection failed: server rejected WebSocket connection: HTTP 401")
after:  1 handshake, on_error (401, "WebSocket handshake rejected (HTTP 401)"), same as the sync client

Tests

Added test_an_http_rejection_from_the_installed_websockets_client_is_not_retried, which drives a real handshake against a local server returning 401, so it exercises whichever websockets client is installed. On websockets 11.0.3 it fails on master (assert 3 == 1) and passes with the fix; on websockets 17 it passes either way, so the py311-websockets11.0 tox env is where it guards against regression. Full unit suite passes on websockets 17.4 and 11.0.3, apart from two existing Windows-only failures that also fail on master; ruff 0.3.5 and mypy 1.5.1 on Python 3.9 are clean on the changed source file.

…bsockets

On websockets < 13 the async client falls back to the legacy websockets.client.connect, which reports an HTTP rejection as InvalidStatusCode rather than InvalidStatus. connect() only special-cased InvalidStatus, so a 401 or 403 was retried max_connection_retries times and reported with code=None. The fallback now treats InvalidStatusCode as a rejection too and reads its status_code, so both websockets generations fail fast with the HTTP status, as the sync client does.

Fixes AssemblyAI#347

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.

On websockets < 13 (allowed by setup.py), AsyncRealTimeTranscriber retries HTTP 401/403 handshake rejections and reports code=None

1 participant