Skip to content

hashtable iterator safety: invalidate on exhaustion#3551

Merged
JimB123 merged 2 commits into
valkey-io:unstablefrom
rainsupreme:hashtable-iter-safety
Apr 24, 2026
Merged

hashtable iterator safety: invalidate on exhaustion#3551
JimB123 merged 2 commits into
valkey-io:unstablefrom
rainsupreme:hashtable-iter-safety

Conversation

@rainsupreme

Copy link
Copy Markdown
Contributor

PR #2611 is abandoned, so I'm raising this PR to guard against the same potential issue.

If anyone called hashtableNext after it has returned false, it may re-enter the loop with stale table/index values. If the hashtable has been resized/rehashed since the first exhaustion so this may result in out of bounds access.

It also somewhat guards against the caller forgetting to clean up the iterator and leaving rehash disabled indefinitely resulting in accumulating performance degradation, but of course relying on this to automatically clean up should be avoided.


/* Cleanup is a no-op but must not crash. */
hashtableCleanupIterator(&iter);
hashtableRelease(ht);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't have this covered somewhere else, it might be interesting for one of these tests to call hashtableRelease before calling hashtableCleanupIterator

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have one like this that tests safe iterator tracking, and I added one that specifically exhausts the iterator first.

Signed-off-by: Rain Valentine <rsg000@gmail.com>
@rainsupreme rainsupreme force-pushed the hashtable-iter-safety branch from 8be8843 to f9df959 Compare April 23, 2026 21:37
Signed-off-by: Rain Valentine <rsg000@gmail.com>
@codecov

codecov Bot commented Apr 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.22%. Comparing base (5abf79e) to head (d36de2a).
⚠️ Report is 5 commits behind head on unstable.

Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #3551      +/-   ##
============================================
- Coverage     76.34%   76.22%   -0.12%     
============================================
  Files           159      159              
  Lines         80045    80091      +46     
============================================
- Hits          61107    61052      -55     
- Misses        18938    19039     +101     
Files with missing lines Coverage Δ
src/hashtable.c 97.85% <100.00%> (+<0.01%) ⬆️
src/unit/test_hashtable.cpp 95.46% <100.00%> (+0.27%) ⬆️

... and 26 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@JimB123 JimB123 merged commit a7d4953 into valkey-io:unstable Apr 24, 2026
58 of 60 checks passed
@enjoy-binbin

Copy link
Copy Markdown
Member

@JimB123 we normally use squash merge

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.

3 participants