Repository navigation
fix: retry events when reading the HTTP response fails - #82
Open
RishavTiwari25 wants to merge 1 commit into
Open
RishavTiwari25 wants to merge 1 commit into
RishavTiwari25 wants to merge 1 commit into
Conversation
urlopen() wraps only errors raised while sending the request in URLError. Errors raised while reading the response escaped HttpClient.post(): a connection closed or reset by the server or a proxy (RemoteDisconnected, ConnectionResetError), a cut-short body (IncompleteRead), or a body that is not the expected JSON. The exception ended Workers.send() inside the thread pool, where nothing observed it, so the batch was dropped without a retry, a callback or a log line. These are now reported as HttpStatus.UNKNOWN, which the response processor retries (events carry an insert_id, so a retry of a request the server did ingest is deduplicated).
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.
Summary
Events are dropped silently when the HTTP response cannot be read.
urllib.request.urlopen()wraps only the errors raised while sending the request inURLError;h.getresponse()andres.read()are outside that wrapper. SoHttpClient.post()raised for:http.client.RemoteDisconnected,ConnectionResetError). This is common with idle keep-alive connections and in serverless runtimes;http.client.IncompleteRead);JSONDecodeError) or JSON withoutcode(KeyError).The exception came out of
Workers.send()inside theThreadPoolExecutor. Nothing reads that future on the normalbuffer_consumerpath, so the batch was gone: no retry, nocallback, no log line.Fix:
HttpClient.post()catches these (OSError,http.client.HTTPException,ValueError,KeyError) after the existingtimeout/HTTPError/URLErrorhandlers and returns aResponsewithHttpStatus.UNKNOWNand the error message.ResponseProcessoralready retriesUNKNOWN(#70), with the usual backoff and max-retry handling. Events get aninsert_idfromContextPlugin, so if the server had already ingested a request whose response was lost, the retry is deduplicated.Tests: new
src/test/test_http_client.pypatchesurlopen: a JSON success still parses;RemoteDisconnected,ConnectionResetErrorandIncompleteRead, and non-JSON / missing-codebodies, returnUNKNOWNwith an error message. These 5 cases raise onmain. Full suite (python -m unittest discover -s ./src -p 'test_*.py'): 121 tests OK.The test module keeps its own reference to the real
HttpClient.post, because some existing test modules assign aMagicMocktoHttpClient.postand do not restore it.Checklist
Note
Medium Risk
Changes error handling on the event upload path; misclassification could affect retry vs. drop behavior, but behavior aligns with existing UNKNOWN retry logic.
Overview
Fixes silent event loss when the Amplitude HTTP client fails while reading the response (after the request was already sent).
HttpClient.post()now catchesOSError,HTTPException,ValueError, andKeyError—covering dropped connections (RemoteDisconnected,ConnectionResetError), truncated bodies (IncompleteRead), and unexpected payloads (HTML proxies, JSON withoutcode)—and returns aResponsewithHttpStatus.UNKNOWNand an error body instead of raising into the worker thread. That status is already retried by the response processor, so batches are no longer dropped without callback or logging.Adds
test_http_client.pywith mockedurlopencases for success, connection/read failures, and malformed bodies.Reviewed by Cursor Bugbot for commit 3fa011d. Bugbot is set up for automated code reviews on this repo. Configure here.