[BUGFIX] Ingester: close TSDB on compaction failure to prevent resource leak#7560
Open
sandy2008 wants to merge 2 commits into
Open
[BUGFIX] Ingester: close TSDB on compaction failure to prevent resource leak#7560sandy2008 wants to merge 2 commits into
sandy2008 wants to merge 2 commits into
Conversation
…eak (cortexproject#7557) When `db.Compact()` fails after a successful `tsdb.Open()` in `createTSDB`, the opened TSDB was not closed, leaking head, WAL, and mmap file handles. Add `db.Close()` with warning-level logging on the compaction error path. Closes cortexproject#7557 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Signed-off-by: Sandy Chen <Yuxuan.Chen@morganstanley.com>
Signed-off-by: Friedrich Gonzalez <1517449+friedrichg@users.noreply.github.com>
SungJin1212
approved these changes
May 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does
createTSDBopens a TSDB viatsdb.Open()and then runsdb.Compact()to flush any head data into blocks after WAL replay. IfCompact()returned an error, the function returned immediately without callingdb.Close(), leaking the head, WAL, and mmap file handles held by the open database.Symptom: each failed compaction permanently leaked one set of TSDB file descriptors and memory-mapped regions. Under repeated startup compaction failures for different tenants, the ingester could exhaust file descriptors (
ulimit -n) and eventually crash or refuse to open new TSDBs.Fix: call
db.Close()before returning the compaction error. IfCloseitself fails, log a warning so the secondary failure is observable rather than silent. This follows the existing close-error logging pattern used incloseAllTSDB(line 3032).Which issue(s) this PR fixes
Fixes #7557
Checklist
CHANGELOG.mdupdated —[BUGFIX] Ingester:entry undermaster / unreleased.make docnot required).Test plan
go build -tags "netgo slicelabels" ./pkg/ingester/...— cleango vet -tags "netgo slicelabels" ./pkg/ingester/...— cleangoimports -l -local github.com/cortexproject/cortex ./pkg/ingester/ingester.go— cleango test -timeout 300s -tags "netgo slicelabels" -count=1 ./pkg/ingester/...— PASS (27s)go test -tags "netgo slicelabels" -count=1 -run "TestHeadCompactionOnStartup" ./pkg/ingester/...— PASS (exercises the success path of the same code)go test -tags "netgo slicelabels" -count=1 -run "TestIngester_OpenExistingTSDBOnStartup" ./pkg/ingester/...— PASS