Skip to content

server: limit the size for COM_STMT_SEND_LONG_DATA in each connection - #70350

Open
djshow832 wants to merge 4 commits into
pingcap:masterfrom
djshow832:long_data
Open

server: limit the size for COM_STMT_SEND_LONG_DATA in each connection#70350
djshow832 wants to merge 4 commits into
pingcap:masterfrom
djshow832:long_data

Conversation

@djshow832

@djshow832 djshow832 commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: close #70349

Problem Summary:
#69693 only limits the size of a single COM_STMT_SEND_LONG_DATA, but it does limit the size for each statement, connection, user, or instance.

What changed and how does it work?

  • Attach the param memory to the session memory tracker.
  • If the session memory exceeds the quota, do not append the param and report an error in the ComExecute.
  • Release the memory after the statement closes or resets.

Note that it only limits memory on the connection level, not the user or instance level, but it's enough for most cases.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

Summary by CodeRabbit

  • Bug Fixes
    • Prepared statements now correctly enforce query memory quotas for long-data parameters.
    • Additional data is rejected when memory or packet limits are reached.
    • Memory used by long-data parameters is released after execution failures and when statements are reset or closed.
    • Subsequent statements can reuse released memory without retaining stale parameter data.

@ti-chi-bot ti-chi-bot Bot added release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 5, 2026
@ti-chi-bot

ti-chi-bot Bot commented Aug 5, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign 3aceshowhand for approval. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 16ef83db-daa3-40a2-918e-038c08f23dbc

📥 Commits

Reviewing files that changed from the base of the PR and between 04c65cb and 1398da1.

📒 Files selected for processing (1)
  • pkg/server/driver_tidb.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/server/driver_tidb.go

📝 Walkthrough

Walkthrough

COM_STMT_SEND_LONG_DATA now enforces query memory quotas, tracks accepted bytes, reports quota errors, and releases tracked memory during statement reset or close. Tests cover quota refusal, recovery, and cleanup.

Changes

Long-data quota enforcement

Layer / File(s) Summary
Quota tracking and statement cleanup
pkg/server/driver_tidb.go
TiDBStatement tracks charged long-data bytes, enforces max_allowed_packet and query memory limits, reports quota errors, and releases state during reset and close.
Quota behavior validation
pkg/server/conn_stmt_test.go
Tests verify quota enforcement, rejected excess data, execution errors, later accepted data, and memory release on statement close.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested reviewers: bb7133, yangkeao, tiancaiamao

Poem

A rabbit tracks each long-data byte,
Then stops at the quota gate.
Cleanup frees the stored amount,
Reset clears the statement state.
Hop, hop—the next query starts.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the connection-level size limit added for COM_STMT_SEND_LONG_DATA.
Description check ✅ Passed The description includes the issue, problem, implementation details, unit test coverage, side effects, documentation checks, and release note.
Linked Issues check ✅ Passed The changes address issue #70349 by bounding connection-level long-data memory, reporting quota errors, and releasing memory on reset or close.
Out of Scope Changes check ✅ Passed The changes remain focused on COM_STMT_SEND_LONG_DATA limits, memory tracking, error handling, cleanup, and related unit tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@djshow832
djshow832 marked this pull request as draft August 5, 2026 08:52
@ti-chi-bot ti-chi-bot Bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 5, 2026
@djshow832
djshow832 marked this pull request as ready for review August 5, 2026 08:54
@ti-chi-bot ti-chi-bot Bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 5, 2026
@djshow832
djshow832 requested review from YangKeao and bb7133 August 5, 2026 08:59

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/server/driver_tidb.go`:
- Line 121: Update the empty-data branch in the relevant long-data handling
method before its return to release the replaced parameter’s boundLongDataBytes
charge and corresponding session MemTracker charge, preserving normal behavior
for non-empty data. Add a regression test covering an initial data chunk
followed by an empty chunk for the same parameter and verify the tracker charge
is released.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 5c91b6cb-dd77-45e6-bc71-32bffb410b90

📥 Commits

Reviewing files that changed from the base of the PR and between ea373a0 and b7810b1.

📒 Files selected for processing (2)
  • pkg/server/conn_stmt_test.go
  • pkg/server/driver_tidb.go

Comment thread pkg/server/driver_tidb.go
@codecov

codecov Bot commented Aug 5, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 42 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.4252%. Comparing base (8380b57) to head (1398da1).
⚠️ Report is 4 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #70350        +/-   ##
================================================
- Coverage   76.3271%   73.4252%   -2.9020%     
================================================
  Files          2041       2078        +37     
  Lines        559032     583952     +24920     
================================================
+ Hits         426693     428768      +2075     
- Misses       131439     154708     +23269     
+ Partials        900        476       -424     
Flag Coverage Δ
integration 40.7336% <0.0000%> (+1.0650%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 59.9240% <ø> (ø)
parser ∅ <ø> (∅)
br 46.6167% <ø> (-16.0923%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

djshow832 and others added 2 commits August 5, 2026 17:05
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@ti-chi-bot ti-chi-bot Bot added the needs-cherry-pick-release-8.5 Should cherry pick this PR to release-8.5 branch. label Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-cherry-pick-release-8.5 Should cherry pick this PR to release-8.5 branch. release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unbounded COM_STMT_SEND_LONG_DATA Accumulation Causes Connection-Scoped Memory DoS in TiDB

1 participant