Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

eventhubs: RetryOperation::CalculateExponentialDelay invokes UB and over-sleeps on final attempt

Aperta
#7,132 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

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

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

needs-triage

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 condition retryCount < 3 becomes 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:

Lingua principale
C++
Stelle
207
Fork
173
Merge medio
1g 10h
PR unite (30g)
30

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Azure/azure-sdk-for-cpp

Tutte le issue di Azure/azure-sdk-for-cpp

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.