tikv / tikv/pd

Canceled contexts can still consume work in select-based paths

Open
#10,929 0 comments 0 reactions 0 assignees View on GitHub
type/bug
Dominant language
Go
Stars
1.2k
Forks
783
Avg merge
5d 21h
Merged PRs (30d)
36

Description

## Bug Report

### What did you do?

While reviewing internal select-based cancellation paths, I found several places where `ctx.Done()` is selected together with another ready channel that consumes work or acquires resources.

Affected examples include:

- `OrderedSingleFlight.Do`
- client batch request collection
- concurrency limiter token acquisition
- deadline watcher registration
- service discovery singleflight calls
- router/resource-manager dispatcher cancellation paths

### What did you expect to see?

When the context is already canceled, these paths should return the context error before consuming requests, acquiring tokens, registering deadlines, or starting shared work. If cancellation happens after resources are acquired, the resources or collected requests should be cleaned up before returning.

### What did you see instead?

Some paths may still consume a request, acquire a token, register a deadline, or start shared work when cancellation races with another ready select case.

In particular:

- an already canceled `OrderedSingleFlight.Do` call may start an execution
- `ConcurrencyLimiter.AcquireToken` may return a token for a canceled context
- `ConcurrentRunner.run` may return before releasing a token
- batch collection may collect requests after cancellation
- router dispatcher may exit after collecting requests without finishing them

### What version of PD are you using (`pd-server -V`)?

Current master.

Contributor guide

Open the contributing guide

Research direction

Start by tracing the listed entry points: OrderedSingleFlight.Do, ConcurrencyLimiter.AcquireToken, ConcurrentRunner.run, client batch collection, deadline watcher registration, service discovery singleflight, and the router/resource-manager dispatcher. Identify the relevant cancellation tests or package tests, then verify that canceled calls do not consume work or resources and that acquired resources and collected requests are cleaned up before returning.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend, distributed-systems
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.