eventhubs: RetryOperation::CalculateExponentialDelay invokes UB and over-sleeps on final attempt
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 72/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Tranquilla
- Stack tecnologico
- azure, cpp
- Ambito
- distributed-systems
Direzione di ricerca
Inizia da sdk/eventhubs/azure-messaging-eventhubs/src/retry_operation.cpp e analizza Execute, ShouldRetry, CalculateExponentialDelay e WasLastAttempt. Leggi i casi esistenti RetryOperationTest.ShouldRetryFalse1 e ShouldRetryFalse2, quindi aggiorna la copertura per il primo tentativo di retry e per il comportamento dell’ultimo tentativo. Il lavoro è completato quando non si verifica alcuno shift negativo né alcuna attesa dopo l’ultimo tentativo e i test di retry superano l’esecuzione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Summary
Azure::Messaging::EventHubs::_detail::RetryOperation in sdk/eventhubs/azure-messaging-eventhubs/src/retry_operation.cpp has two bugs in its delay math. Both are separate from the scope of #7131, which is narrowly about rethrowing the captured exception when retries are exhausted.
Bug 1 (undefined behavior on first retry): RetryOperation::Execute initializes retryCount = 0 and passes it as attempt into ShouldRetry. If the first attempt fails (returns false or throws a retriable exception), ShouldRetry calls CalculateExponentialDelay(attempt=0, jitterFactor). With attempt = 0, the shift expression below evaluates 1 << -1, which is undefined behavior per [expr.shift]. In practice on x86 it tends to mask the shift amount, but the result is implementation-defined at best and can change with optimizer settings or platforms.
Bug 2 (extra backoff sleep after the final attempt): WasLastAttempt(attempt) is attempt >= MaxRetries. The loop checks ShouldRetry(..., retryCount, ...) against the pre-increment retryCount, then increments and sleep_fors. With MaxRetries = 3:
- attempt 1 fails:
WasLastAttempt(0)is false, so the loop sleeps - attempt 2 fails:
WasLastAttempt(1)is false, so the loop sleeps - attempt 3 fails:
WasLastAttempt(2)is false, so the loop sleeps, then the loop conditionretryCount < 3becomes false and the loop exits
The loop always sleeps one extra time before reporting failure, which slows down failure surfacing for callers.
Motivation
Inside CalculateExponentialDelay, the backoff is computed from the raw attempt index without normalization:
auto exponentialRetryAfter = m_retryOptions.RetryDelay
* (((attempt - 1) <= beforeLastBit) ? (1 << (attempt - 1))
: (std::numeric_limits<int32_t>::max)());
When the first retry passes attempt = 0, attempt - 1 is -1, so 1 << (attempt - 1) is the negative shift that triggers the UB in Bug 1. Separately, WasLastAttempt is evaluated against the pre-increment retryCount rather than the attempt that is about to run, so the loop performs a sleep even after the last attempt has failed, which is the over-sleep in Bug 2.
Proposal
Normalize the attempt index inside the delay calculation (treat the first retry as attempt=1 for the shift, and check WasLastAttempt against retryCount + 1), or restructure the loop so the sleep only happens before a subsequent attempt and never after the last one.
This is a behavior change for every existing consumer of RetryOperation (producer_client, consumer_client, partition_client, processor, processor_partition_client), so it should be applied as its own PR and review.
Context:
- Original Copilot comment: https://github.com/Azure/azure-sdk-for-cpp/pull/7131#discussion_r3276773679
- Existing
RetryOperationTest.ShouldRetryFalse1/ShouldRetryFalse2already exercise the shift-by-negative path.
- Lingua principale
- C++
- Stelle
- 207
- Fork
- 173
- Merge medio
- 1g 10h
- PR unite (30g)
- 30
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di Azure/azure-sdk-for-cpp
-
DataLakeFileSystemClient::ListPaths() throws JSON exception due to accessing undefined fieldsApertacustomer-reported needs-triage question
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
Azure/azure-sdk-for-cpp#7435 ·
I maintainer di solito rispondono entro 1 giorno
-
bug EngSys
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
Azure/azure-sdk-for-cpp#7362 ·
I maintainer di solito rispondono entro 1 giorno
-
Client needs-team-attention Service Attention Storage
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
Azure/azure-sdk-for-cpp#7347 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Client Event Hubs needs-team-attention Service Attention
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
Azure/azure-sdk-for-cpp#7333 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
DataLakeDirectoryClient::ListPaths() throws exception due to incorrect format used for date-time fieldsForse già presa @seanmcc-msft l’ha presa oggi. Apertacustomer-reported needs-triage question
Azure/azure-sdk-for-cpp#7436 · 1 commento · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di Azure/azure-sdk-for-cpp
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
mpfaffenberger/privateer_reimagined#658 ·
I maintainer di solito rispondono entro 1 giorno
-
Broken links in the docsAperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 85/100
microsoft/onnxruntime#33018 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
AXERA-TECH/ax-llm#81 ·
-
enhancement
Difficoltà 2/5 Mezza giornata Idoneità per principianti 78/100
ros-industrial/ros2_canopen#448 ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno