Skip to content

Fix TestSelectScatterPartialOLAP data race#19390

Open
Devanshusharma2005 wants to merge 10 commits intovitessio:mainfrom
Devanshusharma2005:fix/vtgate-scatter-session-race
Open

Fix TestSelectScatterPartialOLAP data race#19390
Devanshusharma2005 wants to merge 10 commits intovitessio:mainfrom
Devanshusharma2005:fix/vtgate-scatter-session-race

Conversation

@Devanshusharma2005
Copy link

@Devanshusharma2005 Devanshusharma2005 commented Feb 15, 2026

Description

This PR fixes a potential race condition in vtgate where multiple goroutines may access or mutate PreSessions, ShardSessions, and PostSessions concurrently during Rollback, Release, or ReleaseAll.

Previously, these flows directly iterated over session slice fields without taking a stable snapshot. If another goroutine modified those slices at the same time, it could lead to a data race.

This change introduces safe snapshot helpers on SafeSession and updates the relevant call sites to use them, ensuring that Rollback, Release, and ReleaseAll work on a stable copy of the session slices.

The race was detected in the unit test TestSelectScatterPartialOLAP, which would occasionally fail with -race enabled.

Related Issue(s)

Fixes #18996

BEFORE

image

AFTER

Screenshot 2026-02-15 at 4 00 11 PM

To Reproduce

Reproduction Steps

To reproduce the race condition in TestSelectScatterPartialOLAP, run the test multiple times with the race detector enabled:

source dev.env
go clean -testcache
cnt=0
while [[ $cnt -lt 200 ]]; do
  echo "Run $cnt"
  go test -count=1 -race -timeout 30s \
    -run ^TestSelectScatterPartialOLAP$ \
    vitess.io/vitess/go/vt/vtgate || break
  cnt=$((cnt+1))
done

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

AI Disclosure

Used grep command to trace the function calls and map out the flow, then manually patched the logic (kept it simple since it's a small change.)

Signed-off-by: Matt Lord <mattalord@gmail.com>
Signed-off-by: Devanshu Sharma <devanshusharma658@gmail.com>
@github-actions github-actions bot added this to the v24.0.0 milestone Feb 15, 2026
@vitess-bot vitess-bot bot added NeedsWebsiteDocsUpdate What it says NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Feb 15, 2026
@vitess-bot
Copy link
Contributor

vitess-bot bot commented Feb 15, 2026

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@Devanshusharma2005 Devanshusharma2005 changed the title CI: Fix workflows that install xtrabackup (#19383) Bug Report: Fix TestSelectScatterPartialOLAP data race Feb 15, 2026
@Devanshusharma2005 Devanshusharma2005 changed the title Bug Report: Fix TestSelectScatterPartialOLAP data race Fix TestSelectScatterPartialOLAP data race Feb 15, 2026
@mattlord mattlord added Component: Build/CI Type: Testing and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Feb 15, 2026
Signed-off-by: Devanshu Sharma <devanshusharma658@gmail.com>
Devanshusharma2005 and others added 4 commits February 17, 2026 00:17
Co-authored-by: Arthur Schreiber <schreiber.arthur@googlemail.com>
Signed-off-by: Devanshu Sharma <devanshusharma658@gmail.com>
Co-authored-by: Arthur Schreiber <schreiber.arthur@googlemail.com>
Signed-off-by: Devanshu Sharma <devanshusharma658@gmail.com>
Signed-off-by: Devanshu Sharma <devanshusharma658@gmail.com>
Signed-off-by: Devanshu Sharma <devanshusharma658@gmail.com>
@arthurschreiber
Copy link
Member

Tho concat does slightly more allocations (but ig this is the tradeoff we can do here against the messy code of append).

@Devanshusharma2005 I don't think that's right. You can see how concat is implemented here: https://cs.opensource.google/go/go/+/refs/tags/go1.26.0:src/slices/slices.go;l=489-505

Essentially, it gets the full length required for the final slice, then allocates that slice and appends into it. Basically exactly what you did before. So it doesn't perform any additional allocations.

@arthurschreiber
Copy link
Member

@Devanshusharma2005 👋 Hey, I left a few more comments. I'm not sure the code here warrants being so performance focused, but I think it's a good learning opportunity to better understand how to make changes with a performance focus in mind.

@Devanshusharma2005
Copy link
Author

@Devanshusharma2005 👋 Hey, I left a few more comments. I'm not sure the code here warrants being so performance focused, but I think it's a good learning opportunity to better understand how to make changes with a performance focus in mind.

Hi arthur. Thank you so much for these reviews.
They are helping me to focus more on such details.
I'll commit the fix soon.

Devanshusharma2005 and others added 3 commits February 17, 2026 15:39
Co-authored-by: Arthur Schreiber <schreiber.arthur@googlemail.com>
Signed-off-by: Devanshu Sharma <devanshusharma658@gmail.com>
Signed-off-by: Devanshu Sharma <devanshusharma658@gmail.com>
Signed-off-by: Devanshu Sharma <devanshusharma658@gmail.com>
@Devanshusharma2005
Copy link
Author

@arthurschreiber I’ve implemented your first slices.Concat recommendation for GetShardSessionsForReleaseAll. This helped me better understand how slices.Concat guarantees a single allocation and avoids the potential reallocation that could happen when appending after an exact capacity slice.
If there are any further improvements or refinements you’d suggest, I’d be happy to incorporate them.

Copy link
Collaborator

@mhamza15 mhamza15 left a comment

Choose a reason for hiding this comment

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

LGTM! Just some very minor comments.

Signed-off-by: Devanshu Sharma <devanshusharma658@gmail.com>
@codecov
Copy link

codecov bot commented Feb 19, 2026

Codecov Report

❌ Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 69.66%. Comparing base (ed22ed7) to head (7acdf60).
⚠️ Report is 38 commits behind head on main.

Files with missing lines Patch % Lines
go/vt/vtgate/executorcontext/safe_session.go 90.90% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #19390      +/-   ##
==========================================
- Coverage   69.95%   69.66%   -0.29%     
==========================================
  Files        1610     1613       +3     
  Lines      216083   216465     +382     
==========================================
- Hits       151159   150805     -354     
- Misses      64924    65660     +736     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 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.

@Devanshusharma2005
Copy link
Author

@arthurschreiber lets get this across the finish line ^^

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.

Bug Report: TestSelectScatterPartialOLAP data race

4 participants