Skip to content

fix(frontend): apply negotiated session collation - #30398

Merged
mergify[bot] merged 9 commits into
matrixorigin:mainfrom
daviszhen:codex/fix-30395-handshake-collation
Oct 17, 2026
Merged

mergify[bot] merged 9 commits into
matrixorigin:mainfrom
daviszhen:codex/fix-30395-handshake-collation

Conversation

@daviszhen

@daviszhen daviszhen commented Oct 16, 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:

Fixes #30395

What this PR does / why we need it:

The MySQL handshake negotiated a charset/collation in protocol state, but the newly authenticated session retained collation_connection = 'default' and the default connection charset variables. This was also lost after COM_RESET_CONNECTION rebuilt a session from account-global defaults.

This change applies the admitted protocol charset/collation to character_set_client, character_set_connection, character_set_results, and collation_connection for initial authentication, COM_CHANGE_USER, and COM_RESET_CONNECTION. Reset uses a candidate-session hook before the old generation is retired, so a failed application does not publish the candidate. The wire regressions cover a non-default utf8mb4_unicode_ci handshake, repeated reset, and change-user behavior.

Validation:

make cgo
git diff --check
go test ./pkg/frontend -run 'TestNegotiatedSessionCharsetInitializesSessionVariables|TestCharsetAdmissionHandshake|TestMySQLWireChangeUserAuthSwitchAndRepeatedBorrow|TestMySQLWireResetConnectionPreservesDatabase' -count=1

@daviszhen
daviszhen requested a review from XuPeng-SH as a code owner October 16, 2026 09:56
@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 →

@daviszhen

Copy link
Copy Markdown
Contributor Author

Reviewed latest head 95d72e0520d8c87963a782f1b847fee66e4c1fab (including the main merge).

No blocking findings in this change. The negotiated values are applied after account-global initialization during authentication; COM_CHANGE_USER installs its requested protocol state before authenticating the candidate. Reset applies the same values to the candidate before retiring the previous session, preserving the existing failure boundary. I also checked that proxy cache reuse identity includes the handshake collation ID.

Validation performed on this head in an isolated worktree:

  • Passed the focused charset-admission, negotiated-session, change-user, and session-reset tests.
  • A temporary review-only test used a real TCP MySQL client against an embedded CN, with authentication enabled. For utf8mb4_general_ci, utf8_unicode_ci, and utf8mb4_unicode_ci, all four session variables were correct immediately after connecting, before any SET NAMES. Explicit SET NAMES still worked, and a rejected cross-charset pair left the existing values unchanged.
  • A separate wire test changed users with collation IDs 192 and 224, verified the corresponding utf8/utf8mb4 session values, then deliberately changed the session variables to binary and confirmed COM_RESET_CONNECTION restored the last negotiated pair.
  • git diff --check passed.

Non-blocking suggestion: promote the authenticated TCP checks and the cross-charset change-user/dirty-reset scenario into permanent tests. The current change-user regression always uses the same default collation, so it would not detect accidentally retaining the previous negotiated pair.

Code-review recommendation: approve, subject to required CI passing. CI was still pending/running at review time; I did not run the entire frontend/integration suite. This is an ordinary review comment, not a formal approval.

@daviszhen

Copy link
Copy Markdown
Contributor Author

Updated PR #30398 to head 0f8239bbfc7e50160044cc1c9b75a1cd7979ee31.

  • Merged the latest upstream/main (0723ae37b1) without conflicts.
  • Fixed the SCA failures by removing the unused Session.reset wrapper and unused wireHandshakeResponseWithDatabase test helper.
  • Updated system_variables.result: the handshake now correctly publishes the client-negotiated utf8mb4 charset, so the old utf8 golden value was stale.
  • Local focused frontend handshake/change-user/reset tests pass; git diff --check passes.
  • The new Required CI run is in progress.

@daviszhen

Copy link
Copy Markdown
Contributor Author

再次更新到 ae4c92965d。

  • 合并最新 upstream/main aa4b6eddfdad54d11be6fd75a5675eb9801f6690。
  • 上一轮 UT 的 TestIssue29400CopyAlterRetainedGatePromotionFastFails/same_table_dml 在本地针对性重跑通过;该失败是并发/集群时序下的旧测试失败,不是握手 collation 改动引入。
  • 覆盖率日志显示 changed-code coverage 为 15/20 = 75.00%,而门禁要求严格大于 75%;新增 TestNegotiatedSessionCharsetRejectsIncompleteState 覆盖空状态、非法 charset/collation 和 nil session 分支。
  • frontend handshake/RESET/CHANGE USER 与 charset admission 针对性测试均通过。

新的 Required CI 已重新触发,请基于该 head 重新 review。

@XuPeng-SH XuPeng-SH 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.

Reviewed ae4c92965d08efddd5d5db19571754851c980692 against aa4b6eddfdad54d11be6fd75a5675eb9801f6690 (all six changed files, implementation +50/-4, tests/golden +87/-6, no documentation delta).

No blocking finding. The fix belongs at the existing authentication/reset publication boundary: initialize from account globals, then apply the admitted connection identity. COM_CHANGE_USER shares canonical authentication; reset applies to the unpublished candidate before retiring the old generation. Existing candidate rejection cleanup restores the old protocol/session; the outer wire handler intentionally disconnects after failed COM_CHANGE_USER. No duplicate registry, state machine or per-query work; the superseded reset wrapper was removed.

Supplemental reviewer validation on this exact head: actual authenticated TCP SQL connections for utf8mb4_general_ci, utf8_unicode_ci and utf8mb4_unicode_ci, asserting all four variables immediately after login and unchanged after an invalid SET NAMES pair. A separate real wire-loop probe exercises cross-charset COM_CHANGE_USER (192/224), dirty session collation, COM_RESET_CONNECTION, physical-connection reuse twice, followed by an unsupported collation ERR and disconnect. Both final probes passed: authenticated SQL root 4.53s (package 6.195s), wire root <0.01s (package 1.143s), Go 1.27.1/darwin-arm64. The wire fixture skips credential checking, so authenticated initialization is covered separately by the TCP probe.

Reused exact-head successful CI run 38994096430 for UT, SCA, coverage, build and BVT, plus the green Connector/J reset E2E. All prior conversation notes were checked; there are no unresolved review threads. Recommend retaining the cross-charset wire regression in the repository as follow-up test coverage.

Performance boundary: four setters/validation at login/change/reset; no changed SQL execution hot path. No claim of zero extra reset CPU or a measured TP benchmark. Typed migration preserves these negotiated values; pre-v22 targets remain subject to existing fail-closed replayability checks.

Validation notes: the initial raw go-test command failed at dynamic loading before any test; the repository CGo wrapper resolved that. The first wire probe incorrectly expected reuse after rejected COM_CHANGE_USER, contradicted by the unchanged disconnect contract; the corrected probe checks ERR/disconnect last and passes. Authenticated SQL passed in the earlier combined run; that combined command exited 1 solely for the incorrect wire oracle. No product source was changed.

@mergify mergify Bot added the queued label Oct 17, 2026
@mergify

mergify Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-17 05:05 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-10-17 07:48 UTC · at db1bcc19f95078ce9d960e13b35c2877b9351f28 · squash

This pull request spent 2 hours 43 minutes 38 seconds in the queue, including 51 minutes 57 seconds running CI.

Required conditions to merge

Copy link
Copy Markdown
Contributor

本轮独立功能验证(2026-10-17,冻结 main d9cf2dd4e2b92e2193bb390115be624b76099764,Linux/amd64、Go 1.27.1、匹配 CGo):

  1. 通过真实 go-sql-driver/mysql TCP 连接重跑本 issue 原始复现:握手 45 / 192 / 224 后、执行 SET NAMES 之前,初始 @@collation_connection 分别为 utf8mb4_general_ci / utf8_unicode_ci / utf8mb4_unicode_ci,三个场景全部通过;显式 SET NAMES 对照也通过。
  2. 额外使用 mysql2 3.24.5 连接真实嵌入式数据库。三种 collation 各检查初始四个会话变量、先 SET NAMES 改变会话后连续两次 COM_RESET_CONNECTION、切换 charset 的 COM_CHANGE_USER、change-user 后再次 reset,合计 15 个阶段通过;每阶段检查全部三个 charset 变量和 collation,reset/change-user 后数据库与 connection_id 保持正确,prepared/binary 查询也通过。
  3. 完整 pkg/frontend 普通及 -race -count=1 均通过;charset/routine 相关定向普通/race 均通过。整包各有 1 个现有 rule-cache double-check mocking 测试主动跳过,未计为验证通过。

结论:#30398 修复的 main 会话初始化/重置范围已验证,按约定转给 @Ariznawlll。该结论针对本 issue 的会话状态;#25096/#28164 的 general_ci 比较/UNIQUE,以及 #30339/#30340 的诊断/级联问题在本轮 main 仍复现,分别继续由原 issue 跟踪。

This branch was previously deployed

1 inactive deployment
ci — 8b5e001e Deployed Oct 17, 2026 by mergify[bot]
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 size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: successful collation handshake leaves collation_connection as default

3 participants