Skip to content

leakybucket: unstrand bucket goroutines parked on their final send after reload - #4755

Open
choksi2212 wants to merge 1 commit into
crowdsecurity:masterfrom
choksi2212:fix/leaky-reload-bucket-stranding-4748
Open

choksi2212 wants to merge 1 commit into
crowdsecurity:masterfrom
choksi2212:fix/leaky-reload-bucket-stranding-4748

Conversation

@choksi2212

Copy link
Copy Markdown

What and why

Every bucket live at a SIGHUP reload is stranded forever, as reported in #4748: LeakRoutine's final event send blocks on the shared buffer-1 AllOut channel whose only readers (the output loops) were already stopped by ShutdownCrowdsecRoutines. One orphaned cleanup event fills the buffer and wedges every other bucket.

All four AllOut send sites now select on a per-generation retDone channel that shutdown closes once the readers are provably dead (after the 3s outputsTomb.Wait, before cancel()). A parked send then drops its event and the goroutine exits. While the readers are alive, retDone is not closed and the send keeps the exact plain-send semantics, so backpressure, the overflow path and the GC cleanup markers are unchanged. Buckets built without a factory (tests) get a nil channel, which is the plain send.

One concern only: this does not touch the pour busy-wait of #4734, nor the errgroup membership of bucket goroutines.

Fixes #4748

How it was tested

TestLeakRoutineExitsWhenOutputReadersAreGone runs two live buckets sharing the buffer-1 channel, stops the readers, closes retDone, then cancels the context. On master the second bucket parks forever (the test times out with "LeakRoutine is still parked on its final send after shutdown", which is the issue at unit scale); with this change both goroutines exit. TestLeakRoutineStillDeliversWhileReadersLive pins the other side: with a live reader, a canceled bucket still delivers its cleanup event, so GC'd buckets can't silently vanish.

$ go test ./pkg/leakybucket/ ./cmd/crowdsec/ -count=1
ok  github.com/crowdsecurity/crowdsec/pkg/leakybucket   15.2s
ok  github.com/crowdsecurity/crowdsec/cmd/crowdsec       0.5s

$ golangci-lint run ./pkg/leakybucket/... ./cmd/crowdsec/...    # v2.13.0
0 issues.

go test -race ./pkg/leakybucket/ fails on my Windows box, with the same three race families (exprhelpers debugger, namegenerator math/rand, uniq cache init) on master and on this branch; the uniq one reproduces on master with -run TestBucket/simple-leaky-uniq -count=5. None of the stacks touch the changed lines.

Checklist

  • One concern only.
  • No break to LAPI/CAPI payloads, database schema, config keys, or cscli -o json|raw output.
  • A human has reviewed this diff line by line.
  • A human has tested this change, not only an agent.

AI assistance used: mostly

…ter reload

Every bucket live at a SIGHUP reload is stranded forever: LeakRoutine's
final event send blocks on the shared buffer-1 overflow channel whose
only readers (the output loops) were already stopped by
ShutdownCrowdsecRoutines. One orphaned cleanup event fills the buffer and
wedges every other bucket. Shutdown now closes a per-generation channel
that the four send sites select on, so the parked sends drop their event
and the goroutines exit. While readers are alive, sends keep the exact
plain-send semantics, so backpressure and the GC cleanup markers are
unchanged.

Fixes crowdsecurity#4748

Signed-off-by: choksi2212 <choksi2212@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown

@choksi2212: There are no 'kind' label on this PR. You need a 'kind' label to generate the release automatically.

  • /kind feature
  • /kind enhancement
  • /kind refactoring
  • /kind fix
  • /kind chore
  • /kind dependencies
Details

I am a bot created to help the crowdsecurity developers manage community feedback and contributions. You can check out my manifest file to understand my behavior and what I can do. If you want to use this for your project, you can check out the BirthdayResearch/oss-governance-bot repository.

@github-actions

Copy link
Copy Markdown

@choksi2212: There are no area labels on this PR. You can add as many areas as you see fit.

  • /area agent
  • /area local-api
  • /area cscli
  • /area appsec
  • /area security
  • /area configuration
Details

I am a bot created to help the crowdsecurity developers manage community feedback and contributions. You can check out my manifest file to understand my behavior and what I can do. If you want to use this for your project, you can check out the BirthdayResearch/oss-governance-bot repository.

@choksi2212

Copy link
Copy Markdown
Author

/kind fix

@choksi2212

Copy link
Copy Markdown
Author

/area agent

@choksi2212

Copy link
Copy Markdown
Author

Still green after two weeks — happy to rework anything if the approach needs it, or rebase if that makes review easier. The bucket-strand case it fixes is easy to reproduce from the issue, and the regression tests pin both the stranded-send and the reload ordering.

@onovaes

onovaes commented Oct 30, 2026

Copy link
Copy Markdown

I can confirm that this issue is affecting a production environment running CrowdSec v1.7.7 on Ubuntu 24.04.

We captured the goroutine profile directly from the production agent:

goroutine profile: total 47153

40256 goroutines at:
pkg/leakybucket/bucket.go:264

6741 goroutines at:
pkg/leakybucket/bucket.go:159

The stack traces point directly to Leaky.LeakRoutine, matching the problem addressed by this PR.

The issue was observed after repeated SIGHUP reloads, including reloads triggered automatically by crowdsec-hubupdate.service.

This PR addresses a real production problem. We are looking forward to testing the fix once it becomes available in an official release.

Thank you for working on this!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reload (SIGHUP) strands every live bucket: LeakRoutine blocks forever on its final send after outputs are stopped

2 participants