Repository navigation
fix(streaming): fail fast on HTTP handshake rejections with legacy websockets - #370
Open
chinesepowered wants to merge 1 commit into
Open
chinesepowered wants to merge 1 commit into
chinesepowered wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes
On websockets < 13 (allowed by
setup.py),AsyncRealTimeTranscriberfalls back to the legacy client, which raisesInvalidStatusCodefor an HTTP handshake rejection rather thanInvalidStatus. That case is now treated likeInvalidStatus: not retried, and reported with the HTTP status, asRealTimeTranscriberOptionsdocuments ("HTTP-level rejections (auth, quota, bad request) are never retried"). The status is read fromexc.status_code(legacy) and otherwise fromexc.response.status_code.InvalidStatusCodeis referenced only inside the< 13import 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:
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 onmaster(assert 3 == 1) and passes with the fix; on websockets 17 it passes either way, so thepy311-websockets11.0tox 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 onmaster; ruff 0.3.5 and mypy 1.5.1 on Python 3.9 are clean on the changed source file.