Hacktoberfest 2026: as issues que os mantenedores marcaram para outubro, abertas e boas para iniciantes. Ver issues do Hacktoberfest

Notifier spins without backoff when the Start context ends by deadline

Fechada Para iniciantes
#1,493 1 comentário 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Um pull request relacionado já foi integrado.

  • #1495 de @brandur — integrado

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
62/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
go, postgresql
Domínio
backend, databases

Direção de pesquisa

Comece em internal/notifier/notifier.go, no loop de reconexão que chama n.listenAndWait(ctx) e verifica errors.Is(err, context.Canceled). A correção é parar o loop quando ctx.Err() não for nil e verificar as outras saídas com context.Canceled no mesmo arquivo. Está concluído quando um context que atinge o seu deadline encerra Start sem logs repetidos de Error running listener, demonstrado por um teste que usa um context.WithTimeout curto.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

Summary

If the context passed to Client.Start ends by deadline rather than cancellation, the notifier's reconnect loop spins with no backoff and logs Error running listener (will attempt reconnect after backoff) hundreds of thousands of times per second, forever.

Where

internal/notifier/notifier.go (v0.47.0, and unchanged on master at 2f6b27e):

for attempt := 0; ; attempt++ {
    if err := n.listenAndWait(ctx); err != nil {
        if errors.Is(err, context.Canceled) {
            break
        }
        sleepDuration := serviceutil.ExponentialBackoff(attempt, serviceutil.MaxAttemptsBeforeResetDefault)
        n.Logger.ErrorContext(ctx, n.Name+": Error running listener (will attempt reconnect after backoff)", ...)
        ...
        serviceutil.CancellableSleep(ctx, sleepDuration)
    }
}

Once ctx is done with context.DeadlineExceeded, listenAndWait fails immediately, the context.Canceled check doesn't match, and CancellableSleep returns immediately because ctx is already done. The loop never exits and never sleeps.

Reproduction
  1. Create a client against a healthy Postgres.
  2. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second), then client.Start(ctx).
  3. Wait past the deadline.

We measured 379,487 Error running listener lines in the 2 seconds after the deadline, and 0 when the same context is cancelled instead. Left running, it wrote a ~476 GB log in about 6 hours before we found it.

Suggested fix

Exit the loop when the context is done for any reason, e.g. if ctx.Err() != nil { break } (or check context.DeadlineExceeded too). The other errors.Is(err, context.Canceled) exits in the notifier may deserve the same treatment.

Workaround

Start River on a context that can only end by cancellation, e.g. runCtx, cancel := context.WithCancel(context.WithoutCancel(ctx)); context.AfterFunc(ctx, cancel).

Linguagem predominante
Go
Estrelas
5.8k
Forks
188
Merge médio
22h 44min
PRs com merge (30d)
95

Preparar o ambiente

Este projeto não oferece contêiner de desenvolvimento, Dockerfile nem guia de contribuição, então a configuração fica por sua conta: comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de riverqueue/river

Todas as issues de riverqueue/river

Issues semelhantes

Mais issues de Go

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.