Skip to content

internal/client: close stream on first recv error - #2023

Merged
ti-chi-bot[bot] merged 1 commit into
tikv:masterfrom
windtalker:fix_stream_leak
Jul 23, 2026
Merged

internal/client: close stream on first recv error#2023
ti-chi-bot[bot] merged 1 commit into
tikv:masterfrom
windtalker:fix_stream_leak

Conversation

@windtalker

@windtalker windtalker commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Issue Number: close #2018

Problem Summary:

Stream RPC leases can be retained indefinitely in tikvrpc.CheckStreamTimeoutLoop if a stream is registered into connArray.streamTimeout and the setup path returns after the first Recv reports a non-EOF error without closing the stream. Recv resets the lease deadline to 0 before returning, and the timeout loop treats deadline == 0 as active/idle, so the stale lease can remain tracked indefinitely.

What is changed:

  • Close CopStream when the first Recv returns a non-EOF error.
  • Close BatchCopStream when the first Recv returns a non-EOF error.
  • Close MPPStream when the first Recv returns a non-EOF error.
  • Add TestStreamFirstRecvErrorClosesLease to cover all three stream setup paths and verify the stream context is canceled.

Tests:

  • go test ./internal/client -run '^TestStreamFirstRecvErrorClosesLease$'

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability when streaming requests encounter an error immediately.
    • Streaming connections are now properly closed in these failure cases, preventing resource leaks and helping ensure requests terminate cleanly.
    • Added coverage to verify that failed streams are canceled and return the expected error.

@ti-chi-bot ti-chi-bot Bot added the contribution This PR is from a community contributor. label Jul 17, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jul 17, 2026

Copy link
Copy Markdown

Hi @windtalker. Thanks for your PR.

I'm waiting for a tikv member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@ti-chi-bot ti-chi-bot Bot added needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. dco-signoff: no Indicates the PR's author has not signed dco. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 17, 2026
@coderabbitai

coderabbitai Bot commented Jul 17, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9bb83760-8a9c-489c-9cbb-4f388578541b

📥 Commits

Reviewing files that changed from the base of the PR and between a5548a3 and 2a79cc3.

📒 Files selected for processing (2)
  • internal/client/client.go
  • internal/client/client_test.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • internal/client/client.go
  • internal/client/client_test.go

📝 Walkthrough

Walkthrough

First-receive non-EOF errors now close coprocessor, batch coprocessor, and MPP streams. Tests simulate these failures and verify that the associated stream contexts are canceled.

Changes

Stream lease cleanup

Layer / File(s) Summary
Close streams on first-receive errors
internal/client/client.go
The coprocessor, batch coprocessor, and MPP response handlers close registered streams before returning non-EOF first-receive errors.
Verify lease cancellation
internal/client/client_test.go
Test wrappers inject first-receive failures, and subtests verify nil responses, propagated errors, and canceled stream contexts.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: closing streams on first non-EOF recv errors.
Linked Issues check ✅ Passed The PR implements the linked issue's primary fix for CopStream, BatchCopStream, and MPPStream first-Recv error cleanup and adds coverage.
Out of Scope Changes check ✅ Passed The changes stay focused on stream-close handling and the corresponding test, with no obvious unrelated additions.
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.

Signed-off-by: xufei <xufeixw@mail.ustc.edu.cn>
@ti-chi-bot ti-chi-bot Bot added dco-signoff: yes Indicates the PR's author has signed the dco. and removed dco-signoff: no Indicates the PR's author has not signed dco. labels Jul 17, 2026
@ti-chi-bot ti-chi-bot Bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Jul 17, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ekexium, zyguan

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

The pull request process is described 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

@ti-chi-bot ti-chi-bot Bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jul 23, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown

[LGTM Timeline notifier]

Timeline:

  • 2026-07-17 08:19:25.733982205 +0000 UTC m=+960951.770077261: ☑️ agreed by zyguan.
  • 2026-07-23 07:49:44.295767051 +0000 UTC m=+1477570.331862097: ☑️ agreed by ekexium.

@ti-chi-bot
ti-chi-bot Bot merged commit 816292f into tikv:master Jul 23, 2026
12 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved contribution This PR is from a community contributor. dco-signoff: yes Indicates the PR's author has signed the dco. lgtm needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. 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.

tikvrpc: potential memory leak in stream RPC lease tracking

3 participants