Skip to content

fix(frontend): cancel requests on terminal TCP errors - #32364

Merged
XuPeng-SH merged 4 commits into
matrixorigin:4.2-devfrom
XuPeng-SH:codex/32362-4.2-disconnect-cancellation
Nov 8, 2026
Merged

XuPeng-SH merged 4 commits into
matrixorigin:4.2-devfrom
XuPeng-SH:codex/32362-4.2-disconnect-cancellation

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Part of #32362. Keep the issue open: ordinary FIN disconnects retain the existing policy.

What this PR does / why we need it:

On 4.2-dev, the existing five-second frontend liveness monitor only probes requests at least 30 seconds old. A client reset can therefore leave a younger running statement and its transaction locks alive until that request finishes or becomes eligible for the legacy check.

On Linux, reuse the existing worker and timer. Each pass takes one snapshot of active requests, checks eligible TCP/TLS sockets for a terminal error (POLLERR and TCP_CLOSE) without reading protocol bytes or clearing the socket error, collects those at least 30 seconds old during the same pass, and then performs their original FIN check. Collection happens before any skipped terminal probe so those candidates are not lost. Retirements reuse the existing request cancellation, admission fencing and connection/session cleanup owners. For terminal retirement, busy lifecycle operations retain ownership and are retried on a later pass; the legacy close keeps its existing behavior. Cached probes are capped at 131,072 and evicted after an inactive pass. The snapshot grows with active requests during a pass; after the pass it is reused only up to its retention cap. If a legacy close blocks, the current snapshot allocation can remain until that close returns.

The nominal terminal sampling interval is five seconds; it is not a cancellation or rollback deadline. The terminal phase finishes before legacy closes, but a legacy close can block later passes on a lifecycle mutex. The terminal phase can also delay FIN sampling and lengthen the existing legacy stale-request-snapshot window. The legacy FIN rule remains unchanged: it can cancel a half-closed client once its request is old enough. Unsupported transports, non-Linux platforms, consumed errors and inconclusive probes retain the existing behavior. A reset is not necessarily visible across a proxy hop, and cancellation cannot undo a completed commit.

Validation

  • Real authenticated MySQL TCP, normal and race: an RST during SLEEP following an uncommitted UPDATE caused rollback and released a blocked SELECT FOR UPDATE; a write-half-closed client received the complete result. The reset/rollback subtest completed in 4.92 s normal and 4.81 s under race including setup, not as a timing guarantee.
  • On the previous revision e8d6fabaab, a separate ordinary full-FIN SQL control remained running at 7 seconds, consistent with the retained legacy policy.
  • Full frontend package normal and under race; focused races cover age boundaries, unread input, TLS, healthy socket error queues, request/lifecycle admission, one-worker ordering, blocked legacy close, full-cache socket replacement, cache eviction and cleared snapshot slots. The changed blocked-close test ran 100 times with -race; the other focused concurrency stress runs are from the previous revision.
  • Full proxy consumer test on previous revision e8d6fabaab; current incremental gofmt, build, vet, repository-pinned molint and golangci-lint include the proxy closure. Molint reports only the same Routine.handleRequest recover warning observed at the exact base.
  • On the previous revision, the unchanged !linux monitor was tested through a Linux build-tag overlay; Darwin build-tag selection was inspected, but native Darwin execution was not performed.

Performance

The final one-pass traversal removes one in-memory pass but does not reduce per-socket poll calls. A same-process comparison against e8d6fabaab alternated the two implementations over the same 10,000 distinct TCP sockets, with 80 pairs in each of two process runs. Median scan times were:

Active requests Previous → one-pass, run 1 Previous → one-pass, run 2
Recent 7.079 → 7.036 ms 8.328 → 8.343 ms
Half old 12.206 → 12.407 ms 14.355 → 14.461 ms
Old 18.392 → 18.430 ms 20.596 → 20.649 ms

The paired median relative changes ranged from -0.28% to +1.48%; there is no clear measurable speedup in these tests. An initial separate-process ABBA comparison varied substantially with host load and was inconclusive. Both comparisons are synthetic, not service throughput measurements.

The following end-to-end and resource measurements were taken on the previous revision e8d6fabaab, before this one-pass change. A local, CPU-affinity-pinned single-CN SQL workload used 64 idle and eight active connections, 240,000 SELECT 1 calls per sample, and the order base / original PR / candidate / candidate / original PR / base. These are two samples per variant on a shared host:

Variant QPS samples Median QPS Median p99
4.2-dev base 32,665 / 31,101 31,883 0.664 ms
Original one-second PR 32,596 / 31,916 32,256 0.653 ms
Reused five-second worker 32,632 / 32,376 32,504 0.660 ms

No >10% median throughput regression was observed in this workload. Other services were active and sample counts are small, so this does not establish a general production performance bound.

A follow-up ran 1,120,000 queries per variant for 42.63–45.69 seconds each. One sample per variant yielded 26,274 QPS / 0.810 ms p99 for base, 24,511 / 0.867 ms for the original PR, and 25,364 / 0.832 ms for the candidate. The candidate was 3.46% below base and 3.48% above the original PR in this run. Other test/service processes were active, so these single-sample differences cannot be attributed solely to the code.

A synthetic cold-reactivation cycle with 1,000 active routines sharing one TCP socket measured a median 0.795 ms / 181 KB for the original PR and 0.724 ms / 165 KB for the new implementation; the base measured 0.039 ms / 0 B because it does not check these young requests. The test used five idle one-second passes for the original PR versus one idle five-second pass for the new worker. In a separate intentionally saturated 10,000-entry snapshot/manager-lock microbenchmark, writer p99 samples were 139/146 µs for base, 148/144 µs for the original PR and 144/145 µs for the candidate. These focused tests are not end-to-end production measurements.

A second synthetic benchmark used 10,000 distinct recent active TCP sockets. Median warm per-pass time was 0.897 ms for the base, 8.127 ms for one original-PR fast plus legacy pass, and 7.888 ms for the new combined pass; all had zero warm allocations. The base does no young-request socket probes, and the original PR additionally runs its fast pass every second. These measurements show the cost of the extra checks, not an end-to-end SQL regression or a CPU percentage.

An above-cap synthetic test with 135,000 active sessions sharing one TCP socket measured 6.8–10.6 ms of manager writer-lock wait during the first allocating snapshot across three runs. That snapshot cumulatively allocated about 16.4 MB and retained about 3.43 MB before processing. After an active pass, the capped probe cache retained about 16.26 MB of additional live heap; after one idle pass both cache and oversized scratch became eligible for collection and returned near the pre-scan heap after forced GC. These numbers exclude the existing sessions and socket; they do not imply immediate RSS reduction or provide a worst-case latency bound.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH

XuPeng-SH commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: 96834e0ca07b419d8fe91d1b2e5539a29b187ecf

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

mo-bot review · mo-bot-local · review

Commit: 96834e0ca07b419d8fe91d1b2e5539a29b187ecf

Assessment: approve

The bounded Linux monitor reuses existing cancellation and cleanup owners while preserving FIN and KILL behavior. Request fencing, lifecycle contention and resource bounds are coherent; no actionable findings remain.

Model: gpt-6.1-sol / max

Session: 01a1a004-90c3-73b1-acc9-3d4636452003 · slot 0 · generation 0

Validation:

  • Reviewed all nine changed paths, both discussion entries and linked issue #32362.

  • Isolation preflight and source, patch, formatting and provenance audits passed. Static receipt: checks/1794141351015496998-3.json.

  • Exact extracted TCP/TLS probe tests each passed 100 race repetitions without skips. Receipts: checks/1794141169913286978-3.json and checks/1794141170364754703-3.json.

  • Assessed author-reported MySQL cleanup, TLS, frontend/proxy, static-analysis and performance results. Receipts and retained logs are reconciled in validation-ledger.json.

Limitations:

  • Local socket tests exclude the monitor, request lifecycle and transaction cleanup. Full application tests were not independently run; native prerequisites and raw author logs were unavailable.

  • Five frozen CI checks remain in progress and were not counted as passes.

  • FIN and non-Linux behavior retain the existing policy. Performance samples provide no production guarantee.

@XuPeng-SH

XuPeng-SH commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: b39f2f06670ca2ba673f54de212a9752b2cc7903

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

mo-bot review · mo-bot-local · review

Commit: b39f2f06670ca2ba673f54de212a9752b2cc7903

Assessment: approve

Empty-cache reclamation safely reuses the existing worker and insertion path. The new regression supports keeping terminal cancellation independent of blocking legacy close. No actionable findings remain.

Model: gpt-6.1-sol / max

Session: 01a1a004-90c3-73b1-acc9-3d4636452003 · slot 0 · generation 0

Validation:

  • Covered all nine paths using qualified prior coverage and continuation review; assessed all four discussion entries and linked issue #32362.

  • Preflight and source, patch, formatting, reuse-identity and discussion audits passed. Receipts: checks/1794143144446361075-3.json and checks/1794143199024345634-3.json; matching logs retained.

  • Reused matching prior TCP/TLS probe evidence and assessed current author-reported race, MySQL, static-analysis and performance results.

Limitations:

  • No local application or new regression execution; native prerequisites and raw author logs were unavailable.

  • Four frozen CI checks remain in progress.

  • End-to-end performance remains inconclusive. Reclamation adds allocations when an expired cache is rebuilt.

  • FIN and non-Linux policies remain unchanged; cancellation has no hard one-second guarantee.

@matrix-meow matrix-meow added size/XL Denotes a PR that changes [1000, 1999] lines and removed size/L Denotes a PR that changes [500,999] lines labels Nov 8, 2026
@XuPeng-SH

XuPeng-SH commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: e8d6fabaab9c5a3006e355efafb25e9673cd5c60

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

mo-bot review · mo-bot-local · review

Commit: e8d6fabaab9c5a3006e355efafb25e9673cd5c60

Assessment: approve

The Linux terminal-error monitor reuses existing sampling, admission and cleanup owners while preserving the scoped FIN and KILL policies. Probe caching and snapshot reclamation have explicit bounds and costs; no actionable findings remain.

Model: gpt-6.1-sol / max

Session: 01a1a004-90c3-73b1-acc9-3d4636452003 · slot 0 · generation 0

Validation:

  • Reviewed all nine changed paths, six discussion entries, linked issue32362, lifecycle/rollback consumers and platform fallback.

  • Isolation preflight and frozen-source, complete patch, formatting, discussion and probe-provenance audit passed; receipts and logs are reconciled in validation-ledger.json.

  • Assessed current author-reported frontend normal/race,100x focused races, real MySQL rollback/lock release and half-close, proxy, static and performance results.

Limitations:

  • Local extracted socket tests timed out downloading dependencies before execution; no local runtime or race pass. Full application tests were not independently run; native prerequisites and raw author logs were unavailable.

  • Five frozen CI checks remain in progress.

  • FIN/non-Linux behavior remains legacy. Lifecycle contention and blocked legacy closes can delay later sampling; performance measurements provide no production bound.

@XuPeng-SH

XuPeng-SH commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor Author

mo-bot review · mo-bot-local · done

Commit: 57a13a31b76d0f7f79570d66a930cadb3663bb52

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

mo-bot review · mo-bot-local · review

Commit: 57a13a31b76d0f7f79570d66a930cadb3663bb52

Assessment: approve

The terminal-error path reuses the existing worker and cleanup owners. Its capped probe cache avoids rebuilding probe state each pass; admission fencing and rollback are validated. No actionable findings remain.

Model: gpt-6.1-sol / max

Session: 01a1a004-9884-72c0-ad39-f096c4209c4a · slot 1 · generation 0

Validation:

  • 22 focused tests passed normal/race execution and 100 race repetitions each, without skips.

  • Full frontend normal/race and full proxy normal passed.

  • Authenticated MySQL regression passed normal/race, verifying reset rollback, lock release and complete half-close results.

  • Formatting, whitespace, vet and Darwin file-selection checks passed. Source, inputs, discussion and 36 check receipts were reconciled in validation-ledger.json.

Limitations:

  • Full frontend retains the pre-existing unrelated TestRuleCacheDoubleCheckLocking skip.

  • FIN/non-Linux policies remain legacy; lifecycle contention or blocked legacy close can delay subsequent sampling.

  • Darwin had file-selection checks only. Supplied performance measurements establish no production bound; four relevant CI checks were pending in the frozen snapshot.

@XuPeng-SH
XuPeng-SH merged commit 1155fcd into matrixorigin:4.2-dev Nov 8, 2026
@XuPeng-SH
XuPeng-SH deleted the codex/32362-4.2-disconnect-cancellation branch November 8, 2026 18:10
XuPeng-SH added a commit that referenced this pull request Nov 9, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Part of #32362; keep the issue open. The 4.2-dev change is #32364; main
already has a one-second disconnect probe without the 30-second grace
described in that issue.

## What this PR does / why we need it:

On main, an idle connection's `KILL CONNECTION` can hold the routine
request mutex while closing its protocol. A disconnect scan that has
already snapshotted that routine can then wait behind a slow close
before it reaches another disconnected request. This change removes that
lock-held close when the routine and its lifecycle controller are
uncontended and idle. It seals the existing lifecycle controller before
releasing the locks, so a reset or migration cannot acquire the session
while the protocol is closing. Busy or contended paths retain their
previous behavior; this does not guarantee that every disconnect scan is
nonblocking.

A request finishing concurrently with KILL now hands connection closure
to the existing connection owner after attempting its response. This
closes an existing window where KILL and request completion could each
leave closure to the other. No additional monitor, per-connection cache,
or persistent close state is introduced.

The existing main-branch FIN/RST sampling policy is unchanged. In
particular, main currently cancels a request after a client
write-half-close; this PR does not change that limitation. During an
account KILL of an idle connection, a concurrent subsequent KILL QUERY
can now fail immediately because the connection is already sealed; the
base implementation could instead wait for the close and report a no-op
success. KILL QUERY against a still-running request remains supported.

## Validation

- Full `./pkg/frontend` normal and race tests; `./pkg/proxy` normal
tests. Seventeen individually measured KILL, disconnect, reset, response
and cleanup cases each passed 100 repetitions with `-race`.
- Disposable authenticated single-CN SQL tests exercised RST and FIN
after an uncommitted update, verified rollback and lock release, checked
remote/self KILL, and recorded main's existing write-half-close
behavior.
- `go vet`, the repository-pinned `molint`, and incremental
`golangci-lint` passed for the affected frontend/proxy/issues closure
and final test delta. Native Darwin execution was not available.
- In a local alternating base/candidate healthy-SQL comparison (8 active
and 64 idle connections, 240,000 `SELECT 1` calls per sample), base was
17.0k–18.7k QPS and this change 18.0k QPS; p95 was 0.819–0.899 ms and
0.828–0.854 ms respectively. Base itself varied ~10%, so these samples
show no obvious large regression in this workload; they do not establish
a general speedup or a production latency bound.

This branch was previously deployed

1 inactive deployment
ci — 57a13a31 Deployed Nov 8, 2026 by XuPeng-SH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working kind/enhancement size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants