Skip to content

fix(storage): close the object iterator in isNonEmptyBucket (MOCA-413)#298

Merged
puneet2019 merged 1 commit into
mainfrom
fix/storage-isnonemptybucket-iterator-leak
Jul 1, 2026
Merged

fix(storage): close the object iterator in isNonEmptyBucket (MOCA-413)#298
puneet2019 merged 1 commit into
mainfrom
fix/storage-isnonemptybucket-iterator-leak

Conversation

@puneet2019

Copy link
Copy Markdown
Contributor

MOCA-413 — resource leak in x/storage/keeper isNonEmptyBucket

Leak

isNonEmptyBucket (called from DeleteBucket) created an object-store iterator and returned iter.Valid() without closing it — leaking a store iterator on every call:

iter := objectStore.Iterator(nil, nil)
return iter.Valid()

Fix

Add defer iter.Close(). defer runs after iter.Valid() is evaluated and the function isn't in a loop, so the iterator is always released and behavior is unchanged.

Test

New whitebox behavior test TestIsNonEmptyBucket (package keeper, since the function is unexported): empty bucket → false, bucket with an object → true, per-bucket isolation. A leak-catching test isn't practical — moca's KVStore doesn't panic on write-while-iterator-open, so the dangling iterator isn't observable from a unit test; the test guards behavior alongside the fix.

Verification

go build ./x/storage/..., go vet ./x/storage/keeper/, gofumpt, and go test ./x/storage/keeper/... all pass.

@puneet2019 puneet2019 requested a review from a team June 30, 2026 14:16
@github-actions github-actions Bot added the tests label Jun 30, 2026
isNonEmptyBucket created an object-store iterator and returned iter.Valid()
without ever calling iter.Close(), leaking a store iterator on every call (it
runs via DeleteBucket). Add `defer iter.Close()` so the iterator is always
released; defer runs after iter.Valid() is evaluated and the function is not in
a loop, so behavior is unchanged.

Add a whitebox behavior regression test (TestIsNonEmptyBucket): empty bucket ->
false, a bucket with an object -> true, plus per-bucket isolation. A
leak-catching test is impractical because moca's KVStore does not panic on
write-while-iterator-open, so the dangling iterator is not observable from a
unit test.

MOCA-413

Signed-off-by: puneetmahajan <59960662+puneet2019@users.noreply.github.com>
@puneet2019 puneet2019 force-pushed the fix/storage-isnonemptybucket-iterator-leak branch from ec0d084 to d4a37e1 Compare June 30, 2026 17:52
@puneet2019 puneet2019 requested a review from phenix3443 July 1, 2026 03:45
@puneet2019 puneet2019 merged commit 19d3c9e into main Jul 1, 2026
27 checks passed
@puneet2019 puneet2019 deleted the fix/storage-isnonemptybucket-iterator-leak branch July 1, 2026 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants