Skip to content

core: fix goroutine leak on repeated Raft snapshot restores - #32214

Open
danieldethloff1993 wants to merge 2 commits into
hashicorp:mainfrom
danieldethloff1993:fix/raft-restore-goroutine-leak-32213
Open

danieldethloff1993 wants to merge 2 commits into
hashicorp:mainfrom
danieldethloff1993:fix/raft-restore-goroutine-leak-32213

Conversation

@danieldethloff1993

@danieldethloff1993 danieldethloff1993 commented Nov 25, 2026 •

Copy link
Copy Markdown

Description

What does this PR do?

Fixes #32213. When integrated Raft storage is used and snapshots are restored periodically (e.g. hourly), the number of goroutines on the active node grows without bound (>4000 after a month).

Root cause: a snapshot restore calls preSeal followed by postUnseal while c.activeContext stays alive. Four background workers were tied only to that context (or had no stop mechanism), so every restore started a new instance while the previous one kept running:

  • MFA cached-response cleanup worker (setupCachedMFAResponseAuth)
  • consumption billing metrics worker
  • Raft undo log monitor (monitorUndoLogs)
  • Raft WAL verifier (StartRaftWalVerifier)

Fix: each worker now runs on its own cancellable child context. It is cancelled in preSeal / teardownLoginMFA / stopRaftActiveNode, and any previous instance is cancelled (and, for MFA, awaited) before a new one starts. The MFA and billing workers also stop their tickers correctly.

Tests added:

  • TestCachedMFAResponseAuthLifecycle (vault/login_mfa_test.go, uses testing/synctest)
  • TestRaft_SnapshotAPI_RepeatedRestore (vault/external_tests/raft/raft_test.go). It restores a snapshot repeatedly on a 3-node cluster (force=false/true) and asserts the goroutine count stays stable. With the undo-log fix reverted, this test fails.

Validation:

  • go test -tags=testonly ./vault/external_tests/raft -run '^(TestRaft_SnapshotAPI|TestRaft_SnapshotAPI_RepeatedRestore)$' -count=1: passed
  • go test -tags=testonly ./vault -run '^(TestCachedMFAResponseAuthLifecycle|TestConsumptionBillingMetricsWorker.*)$' -count=1: passed
  • The same two commands with -race (Linux, golang:1.27.4 container): passed
  • gofmt -l is clean, and go vet reports no new findings

A changelog entry is included (changelog/32214.txt).

Jira: N/A (external contribution; tracked in GitHub issue #32213).

A snapshot restore runs preSeal and postUnseal without cancelling the
active context, so workers bound only to that context were started again
on every restore without stopping the previous instance. With hourly
restores this accumulated thousands of goroutines.

Give the MFA cleanup worker, consumption billing worker, raft undo log
monitor and raft WAL verifier their own cancellable lifecycle that is
stopped on seal/teardown and when they are restarted.

Fixes hashicorp#32213

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Goroutine leak on repeated Raft snapshot restores

1 participant