Skip to content

Prevent getNodeByQuery from leaking DB changes into client#2206

Merged
zuiderkwast merged 2 commits into
valkey-io:unstablefrom
gmbnomis:fix_getNodeByQuery_db_change_leak
Jun 16, 2025
Merged

Prevent getNodeByQuery from leaking DB changes into client#2206
zuiderkwast merged 2 commits into
valkey-io:unstablefrom
gmbnomis:fix_getNodeByQuery_db_change_leak

Conversation

@gmbnomis

Copy link
Copy Markdown
Contributor

To support multiple databases in cluster mode (see #1671), getNodeByQuery temporarily switches databases when tracking SELECT statements during slot migration/import. The intended logic is to revert any database change after the operation. However, this approach is flawed: in some transactions the database change is not properly reverted, causing the client to remain on the wrong database.

For example, if a transaction includes SELECT statements, the current database may be changed even if the transaction is never executed (see added test).

Fix the issue by saving the original database once and restoring to it after a switch.

To support multiple databases in cluster mode (see valkey-io#1671),
`getNodeByQuery` temporarily switches databases when tracking `SELECT`
statements during slot migration/import. The intended logic is to
revert any database change after the operation. However, this approach
is flawed: in some transactions the database change is not properly
reverted, causing the client to remain on the wrong database.

For example, if a transaction includes `SELECT` statements, the current
database may be changed even if the transaction is never executed (see
added test).

Fix the issue by saving the original database once and
restoring to it after a switch.

Signed-off-by: Simon Baatz <gmbnomis@gmail.com>
@codecov

codecov Bot commented Jun 11, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 71.56%. Comparing base (92c1549) to head (9e2052d).

Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #2206      +/-   ##
============================================
+ Coverage     71.50%   71.56%   +0.05%     
============================================
  Files           122      122              
  Lines         66493    66493              
============================================
+ Hits          47547    47585      +38     
+ Misses        18946    18908      -38     
Files with missing lines Coverage Δ
src/cluster.c 90.38% <100.00%> (ø)

... and 15 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.

@zuiderkwast zuiderkwast left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great, looks good!

Comment thread tests/unit/cluster/slot-migration.tcl Outdated
Co-authored-by: Viktor Söderqvist <viktor.soderqvist@est.tech>
Signed-off-by: Simon Baatz <gmbnomis@users.noreply.github.com>
@zuiderkwast zuiderkwast removed this from Valkey 8.1 Jun 16, 2025
@zuiderkwast zuiderkwast merged commit 81e0051 into valkey-io:unstable Jun 16, 2025
50 checks passed
ranshid pushed a commit to ranshid/valkey that referenced this pull request Jun 18, 2025
…#2206)

To support multiple databases in cluster mode (see valkey-io#1671),
`getNodeByQuery` temporarily switches databases when tracking `SELECT`
statements during slot migration/import. The intended logic is to revert
any database change after the operation. However, this approach is
flawed: in some transactions the database change is not properly
reverted, causing the client to remain on the wrong database.

For example, if a transaction includes `SELECT` statements, the current
database may be changed even if the transaction is never executed (see
added test).

Fix the issue by saving the original database once and restoring to it
after a switch.

---------

Signed-off-by: Simon Baatz <gmbnomis@gmail.com>
Signed-off-by: Simon Baatz <gmbnomis@users.noreply.github.com>
Co-authored-by: Viktor Söderqvist <viktor.soderqvist@est.tech>
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.

2 participants