Skip to content

retry: delegate killed checks to signal handler - #2042

Merged
ti-chi-bot[bot] merged 6 commits into
tikv:masterfrom
YangKeao:fix-68682-explicit-txn-disconnect
Aug 13, 2026
Merged

ti-chi-bot[bot] merged 6 commits into
tikv:masterfrom
YangKeao:fix-68682-explicit-txn-disconnect

Conversation

@YangKeao

@YangKeao YangKeao commented Aug 4, 2026 •

Copy link
Copy Markdown
Member

Problem

TiDB needs to run richer kill-signal handling at interruptible TiKV retry checkpoints so it can cooperatively detect a disconnected client while a statement is blocked in TiKV. The existing client-go code only reads the legacy Killed flag, and the 2PC path duplicates that check.

This is needed by pingcap/tidb#68682.

Downstream integration: pingcap/tidb#70343.

What changed

  • Add the optional kv.KillSignalHandler interface to kv.Variables.
  • Let Backoffer.CheckKilled delegate to the handler when configured, while preserving the existing Killed fallback for compatibility.
  • Reuse Backoffer.CheckKilled for interruptible 2PC actions instead of duplicating the atomic kill-flag check.
  • Add unit coverage for handler precedence, legacy fallback, and nil variables.

Compatibility

This is an additive API change. Existing callers that only populate Variables.Killed keep the previous behavior. TiDB can opt in by installing its SQL kill-signal handler.

Tests

  • go test ./config/retry ./txnkv/transaction

Summary by CodeRabbit

  • New Features

    • Added configurable handling for kill signals during interruptible operations.
    • Kill-signal handlers can now provide custom interruption errors.
    • Existing kill-state behavior remains supported as a fallback.
  • Bug Fixes

    • Improved cancellation checks before processing transaction batches.
    • Correctly reports query interruptions caused by kill signals.
    • Avoids unnecessary checks when kill-related settings are unavailable.
    • Improved reliability when kill-signal handling changes during concurrent operations.

@ti-chi-bot ti-chi-bot Bot added dco-signoff: yes Indicates the PR's author has signed the dco. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Aug 4, 2026
@coderabbitai

coderabbitai Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 43f1f05b-bde5-416f-8584-fed5339b158f

📥 Commits

Reviewing files that changed from the base of the PR and between 1a233db and 764e536.

📒 Files selected for processing (2)
  • kv/variables.go
  • txnkv/transaction/2pc.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • txnkv/transaction/2pc.go
  • kv/variables.go

📝 Walkthrough

Walkthrough

Variables now supports configurable kill-signal handlers. Backoffer.CheckKilled uses the handler when configured and otherwise checks the legacy kill flag. Interruptible transaction batch actions now use this check.

Changes

Kill signal handling

Layer / File(s) Summary
Handler contract and backoff integration
kv/variables.go, config/retry/backoff.go
Variables stores and loads a kill-signal handler atomically. Backoffer.CheckKilled invokes the handler when configured and otherwise checks Killed.
Transaction checkpoint and validation
txnkv/transaction/2pc.go, config/retry/backoff_test.go
Interruptible batch actions return the result of bo.CheckKilled() before processing batches. Tests cover handler errors, signal interruption, no-kill behavior, and concurrent handler replacement.

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

Sequence Diagram(s)

sequenceDiagram
  participant TransactionAction
  participant Backoffer
  participant Variables
  participant KillSignalHandler
  TransactionAction->>Backoffer: CheckKilled()
  Backoffer->>Variables: LoadKillSignalHandler()
  Variables-->>Backoffer: configured handler or nil
  Backoffer->>KillSignalHandler: HandleSignal()
  KillSignalHandler-->>Backoffer: error or nil
  Backoffer-->>TransactionAction: interruption result
Loading

Suggested reviewers: lcwangchao, wfxr, zyguan, ekexium

🚥 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 and concisely describes the main change: delegating retry kill checks to a signal handler.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ 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.

@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: 2

🤖 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 `@kv/variables.go`:
- Around line 39-40: Document the exported HandleSignal method in the
KillSignalHandler interface with a clear Go doc comment describing when it
returns an error, following standard Go documentation conventions.

In `@txnkv/transaction/2pc.go`:
- Around line 1103-1105: Extend transaction-level tests in 2pc_test.go beyond
TestMinCommitTsManager to cover doActionOnBatches with an interruptible batch
and a backoffer configured with a KillSignalHandler that returns
ErrQueryInterrupted. Assert that doActionOnBatches returns that interruption
error before invoking batch handling.
🪄 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: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 285cb786-9601-4402-bc04-5b5820e4703a

📥 Commits

Reviewing files that changed from the base of the PR and between 341d469 and da5feb9.

📒 Files selected for processing (4)
  • config/retry/backoff.go
  • config/retry/backoff_test.go
  • kv/variables.go
  • txnkv/transaction/2pc.go

Comment thread kv/variables.go
Comment thread txnkv/transaction/2pc.go
@YangKeao
YangKeao force-pushed the fix-68682-explicit-txn-disconnect branch from da5feb9 to 416c4d8 Compare August 4, 2026 18:28

@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 `@kv/variables.go`:
- Around line 58-66: Synchronize access to Variables.KillSignalHandler between
SetKillSignalHandler and Backoffer.CheckKilled to eliminate races when shared
Variables are reused. Treat handler configuration as lifecycle state and either
protect both reads and writes with the existing synchronization mechanism or
store and retrieve the handler through a concurrency-safe copy/source, while
preserving the current handler precedence behavior.
🪄 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: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 52f9cd7b-82ca-4a37-afcd-fb1192fa5e49

📥 Commits

Reviewing files that changed from the base of the PR and between da5feb9 and 416c4d8.

📒 Files selected for processing (4)
  • config/retry/backoff.go
  • config/retry/backoff_test.go
  • kv/variables.go
  • txnkv/transaction/2pc.go
🚧 Files skipped from review as they are similar to previous changes (3)
  • config/retry/backoff.go
  • txnkv/transaction/2pc.go
  • config/retry/backoff_test.go

Comment thread kv/variables.go Outdated
@YangKeao

YangKeao commented Aug 4, 2026

Copy link
Copy Markdown
Member Author

/test pull-integration-test-nextgen

@YangKeao
YangKeao force-pushed the fix-68682-explicit-txn-disconnect branch 2 times, most recently from 0c306f8 to bd0b65f Compare August 6, 2026 04:15
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@ti-chi-bot ti-chi-bot Bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Aug 6, 2026
@YangKeao
YangKeao force-pushed the fix-68682-explicit-txn-disconnect branch from bd0b65f to 1a233db Compare August 6, 2026 04:22
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@YangKeao

YangKeao commented Aug 6, 2026

Copy link
Copy Markdown
Member Author

/test pull-integration-test-nextgen

Signed-off-by: Yang Keao <yangkeao@chunibyo.icu>
@YangKeao
YangKeao force-pushed the fix-68682-explicit-txn-disconnect branch from 1a233db to 764e536 Compare August 12, 2026 07:21

@ekexium ekexium 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.

The rest LGTM

Comment thread kv/variables.go Outdated
// SetKillSignalHandler atomically sets the handler used at interruptible KV request
// checkpoints. Passing nil clears the handler.
func (v *Variables) SetKillSignalHandler(handler interface{ HandleSignal() error }) {
if handler == nil {

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.

Nit: we can also check if it's a typed nil

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

After discuss with @lcwangchao, as we don't pass any typed nil here yet, it's too redudant to handle the case. I'll remove the typed nil check based on reflect.

Comment thread kv/variables.go Outdated
Signed-off-by: Yang Keao <yangkeao@chunibyo.icu>
@ti-chi-bot ti-chi-bot Bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Aug 13, 2026
Comment thread kv/variables.go Outdated
return v.killSignalHandler
}

func isNilKillSignalHandler(handler KillSignalHandler) bool {

@lcwangchao lcwangchao Aug 13, 2026 •

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.

Why does it need to introduce isNilKillSignalHandler instead of handler == nil ? Does this have any special case to make us to have to do this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No. For the current use case == nil is enough. But I received a previews comment:
image

And I think it would be better than not 🤦. As this branch will not be called very frequently, it'll not introduce high performance influence.

Signed-off-by: Yang Keao <yangkeao@chunibyo.icu>
@ti-chi-bot ti-chi-bot Bot removed the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 13, 2026
@ti-chi-bot ti-chi-bot Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Aug 13, 2026
Signed-off-by: Yang Keao <yangkeao@chunibyo.icu>
Comment thread config/retry/backoff.go Outdated
if b.vars == nil {
return nil
}
if handler := b.vars.LoadKillSignalHandler(); handler != nil {

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.

Do we need to aslo check b.vars.Killed here ? Is there any case we need to ignore b.vars.Killed ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No. I will move it to the end! 👍

Signed-off-by: Yang Keao <yangkeao@chunibyo.icu>
@YangKeao
YangKeao force-pushed the fix-68682-explicit-txn-disconnect branch from 92986cf to caafcfe Compare August 13, 2026 09:43
Signed-off-by: Yang Keao <yangkeao@chunibyo.icu>
@ti-chi-bot ti-chi-bot Bot added the lgtm label Aug 13, 2026
@ti-chi-bot

ti-chi-bot Bot commented Aug 13, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

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

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 removed the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Aug 13, 2026
@ti-chi-bot

ti-chi-bot Bot commented Aug 13, 2026

Copy link
Copy Markdown

[LGTM Timeline notifier]

Timeline:

  • 2026-08-13 08:14:49.334821842 +0000 UTC m=+3293475.370916898: ☑️ agreed by ekexium.
  • 2026-08-13 10:43:32.396051255 +0000 UTC m=+3302398.432146310: ☑️ agreed by lcwangchao.

@ti-chi-bot
ti-chi-bot Bot merged commit 52c1e76 into tikv:master Aug 13, 2026
14 checks passed
YangKeao added a commit to YangKeao/client-go that referenced this pull request Sep 7, 2026
 \n\nSigned-off-by: Yang Keao <yangkeao@chunibyo.icu>

Signed-off-by: Yang Keao <yangkeao@chunibyo.icu>
YangKeao added a commit to YangKeao/client-go that referenced this pull request Sep 7, 2026
Signed-off-by: Yang Keao <yangkeao@chunibyo.icu>
YangKeao added a commit to YangKeao/client-go that referenced this pull request Sep 7, 2026
 \n\nSigned-off-by: Yang Keao <yangkeao@chunibyo.icu>

(cherry picked from commit 52c1e76)
Signed-off-by: Yang Keao <yangkeao@chunibyo.icu>
ti-chi-bot Bot pushed a commit that referenced this pull request Sep 8, 2026
 \n\nSigned-off-by: Yang Keao <yangkeao@chunibyo.icu>
ti-chi-bot Bot pushed a commit that referenced this pull request Sep 8, 2026
 \n\nSigned-off-by: Yang Keao <yangkeao@chunibyo.icu>
AilinKid pushed a commit to AilinKid/client-go that referenced this pull request Sep 11, 2026
 \n\nSigned-off-by: Yang Keao <yangkeao@chunibyo.icu>

(cherry picked from commit 0f91f12)
ti-chi-bot Bot pushed a commit that referenced this pull request Oct 12, 2026
 \n\nSigned-off-by: Yang Keao <yangkeao@chunibyo.icu>\n\nCo-authored-by: YangKeao <yangkeao@chunibyo.icu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved dco-signoff: yes Indicates the PR's author has signed the dco. lgtm size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants