Retry HTTP requests on dead connections#76
Merged
Conversation
The previous fix (commit 30f8968) introduced a proactive session refresh based on a 5-minute age threshold (CONNECTION_MAX_AGE). While this helps in some cases, it does not prevent RemoteDisconnected errors because the server (or an intermediate load balancer) can close the TCP connection at any time — well before the 5-minute window expires. When that happens, the next request on the recycled connection hits a dead socket and raises: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')) This commit introduces a centralized _request() method that all HTTP methods (get, post, put, patch, delete, post_multipart) now delegate to. The method applies a reactive retry strategy on top of the existing proactive refresh: 1. _refresh_if_stale() still runs first as a best-effort preventive measure. 2. If the actual request raises requests.ConnectionError or http.client.RemoteDisconnected, the broken session is closed, a fresh session is created, and the request is retried exactly once. 3. If the retry also fails, the exception propagates normally — no infinite retry loops. This also consolidates duplicated request logic: each HTTP method no longer independently calls _refresh_if_stale() and session.<method>(), reducing the surface for future inconsistencies. Tests updated to mock session.request (the unified dispatch point) and four new test cases added covering: ConnectionError retry, RemoteDisconnected retry, non-retryable errors (e.g. Timeout) pass through, and retry failure propagation. Signed-off-by: Guilherme Monteiro de Oliveira <guilherme@monteiroliveira.com>
Collaborator
Author
|
Error: Action failed: The process '/opt/hostedtoolcache/sonar-scanner-cli/7.2.0.5079/linux-x64/bin/sonar-scanner' failed with exit code 3 Sonar quebrou.. |
Collaborator
Author
|
@lmeilibr tem que dar bump na versão do pyproject |
Extract _prepare_and_send and _request_raw so that get_raw benefits from the same dead-connection retry logic as all other HTTP methods. Signed-off-by: Guilherme Monteiro de Oliveira <guilherme@monteiroliveira.com>
Cover get_raw happy path, error mapping, ConnectionError retry, RemoteDisconnected retry, and retry failure propagation. Also asserts that the prepared URL preserves literal brackets. Signed-off-by: Guilherme Monteiro de Oliveira <guilherme@monteiroliveira.com>
lmeilibr
approved these changes
Apr 17, 2026
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.
Retry Request em conexões mortas
Prefácio
O fix anterior (commit 30f8968) introduziu um refresh proativo de sessão baseado em um threshold de 5 minutos (
CONNECTION_MAX_AGE). Apesar de ajudar em alguns casos, não previne erros deRemoteDisconnected— o servidor ou um load balancer intermediário pode fechar a conexão TCP a qualquer momento antes da janela de 5 minutos expirar, deixando o próximo request em um socket morto.Este PR introduz um método centralizado
_request()com uma estratégia de retry reativo por cima do refresh proativo existente, além de preservar o suporte aparamsnoget()e o métodoget_raw()para URLs pré-construídas.CHANGELOG
15b053bfdb88: Retry HTTP requests on dead connections instead of only refreshingc18d5f061449: Delegate get_raw to _request_raw with connection retry21dc0c40f861: Add tests for get_raw and _request_raw connection retrya9492e784aab: Bump version to 0.9.1REQUISITOS PARA O PR
*.log,print,código comentadooudados sensíveis no diff(Não deixe código comentado e verifique seus commits antes de darpush, é exatamento para isso que o git serve).