Repository navigation
Add the ResetSession packet to the native protocol - #123914
alex-clickhouse wants to merge 9 commits into
Conversation
The `ResetSession` client packet (type 15) returns the session of a native connection to its state right after authentication, without a new connection. `Session::resetSessionContext` makes a new session context with the same code as login, and `TCPHandler` sets the database from the handshake on it before it replaces the old context. If anything fails, the old context stays and the server replies with `Exception`; on success it replies with `EndOfStream`. The packet has the protocol version 54493. A client checks it against the server's version from the Hello packet, and the server accepts the packet from a client with any version, so a client does not need support for all earlier protocol versions to use it. Related: ClickHouse#105962 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Workflow [PR], commit [f92df54] Summary: ✅
AI ReviewSummaryAdds the native TCP Current-head fast-test results pass four reset regressions; the transaction test is skipped there. The transaction and other reset regressions passed in the earlier PR commit's targeted stateless run, and authentication, session-limit, and framing cases passed in its integration run. Those full jobs are still pending on the reviewed head. Final VerdictStatus: ✅ Approve. No code changes requested; pending CI should complete before merge. |
While the server runs a query, it reads client packets in `receivePacketsExpectCancel`. There, packet 15 went to the default branch, `UNKNOWN_PACKET_FROM_CLIENT`, and the server closed the connection without an `Exception`. The packet has no body, so the input is in sync: raise `UNEXPECTED_PACKET_FROM_CLIENT`, as during the read of the query data, so the client gets the error before the server closes the connection. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units328 translation units recompiled, 3165 s compile time in total, 326 of them have a recent master baseline. |
A reset is a new login of the authenticated user without its credentials. After `EXECUTE AS`, a reset gave back the rights that `EXECUTE AS` dropped, so `EXECUTE AS` was no longer one-way for the client of the connection. The server now rejects the reset with `ACCESS_DENIED` in this case. The session does not change and the connection stays open; the client must open a new connection. After `EXECUTE AS` back to the authenticated user, the reset works again. Settings that the session cannot change back itself (`readonly`, `allow_ddl`) get the values from the profile, as at a new login. SQL cannot send the packet, so they stay one-way for the SQL that runs in the session. The spec and `05326_reset_session_packet_user` describe and test both. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI enables `table_engines_require_grant`, so `CREATE TEMPORARY TABLE` alone does not permit the restricted users to create `Memory` tables. Add `TABLE ENGINE ON Memory` to the atomicity and HTTP authentication profile tests so they reach their session-preservation assertions. Reproduced both failures locally with CI access-control improvements enabled, then verified the stateless test through `clickhouse-test` and the HTTP profile test body through the disposable-server adapter. The local binary is the earlier PR build; full integration CI remains needed. CI: https://github.com/ClickHouse/ClickHouse/actions/runs/38709305532/job/116337142579#step:4:34008
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
Related: #105962
This adds the
ResetSessionpacket (type15) to the native TCP protocol. The packet returns the session of a connection to its state right after authentication, without a new connection. A client connection pool can send it when a connection comes back to the pool, so that the next user does not get the settings, temporary tables or current database of the previous user.This PR replaces the SQL statement
RESET SESSIONfrom #105962. In review, tavplubix asked for a packet of the native protocol instead of a SQL query, so thatTCPHandlermakes a new context and the other components do not change.How it works
Session::resetSessionContextmakes a new session context with the same code that login uses (createSessionContextForUserandapplySettingsFromAuthServer, now shared by both). The new context gets the authenticated user, the external roles, the grants and expiry of the authentication method, and the settings from an external authentication server. The client info of the connection is kept.SET, the current roles and database, temporary tables, query parameters, scalars and in-memory backups.TCPHandlersets the database from the handshake on the new context before it replaces the old one. If the handshake had no database, the session uses the default database of the user, read again from access control.Exception. The connection stays open, except forUSER_EXPIRED. If the database from the handshake was dropped, the reset fails withUNKNOWN_DATABASE, the same as a new connection with that handshake.INVALID_TRANSACTIONinside a transaction. No query runs, so the transaction is not touched, and the client can stillCOMMITorROLLBACK.ACCESS_DENIEDwhile the session runs as another user afterEXECUTE AS. SeeEXECUTE ASand other one-way changes.idle_connection_timeoutand the others) from the new context and replies withEndOfStream.UNEXPECTED_PACKET_FROM_CLIENTand closes the connection.EXECUTE ASand other one-way changesA session can give up rights in a way that its SQL cannot undo:
EXECUTE AS target(the form without a subquery), the session runs astarget. To go back,targetneedsIMPERSONATEon the original user. Without it, the server returnsACCESS_DENIED.SET readonly = 1(or2),SET readonly = 0fails withREADONLY.SET allow_ddl = 0,SET allow_ddl = 1fails.A proxy can use this to run less trusted SQL on its own connection. In the review of #105962, ringerc found that the SQL statement
RESET SESSIONundidEXECUTE AS(comment). The less trusted SQL could sendRESET SESSIONitself, soEXECUTE ASwas no longer one-way. The same applied toreadonlyandallow_ddl.This PR closes this gap with two rules:
ResetSession. The SQL that runs in the session cannot undoEXECUTE AS,readonlyorallow_ddlwith it.EXECUTE AS, the server rejects the reset withACCESS_DENIEDand the session does not change. The client must open a new connection, and for that it needs the credentials. AfterEXECUTE ASback to the authenticated user, the reset works again. SQL Server has the same rule: its connection pool reset fails with error 18059 when the session runs as an impersonated principal.readonlyandallow_ddlget the values from the profile of the user, the same as all other settings, because a new login of the same user also gets these values. The server does not keep a list of one-way settings: each new setting would have to be added to such a list, and it is easy to forget. The user check is one condition, and it does not depend on what the session changed.A proxy that forwards the packets of a less trusted client on its own connection must not forward
ResetSession. This is a check of one packet type, not a match on SQL text.05326_reset_session_packet_usertests these rules: the reset is rejected afterEXECUTE ASand the session stays usable, the reset works again afterEXECUTE ASback to the authenticated user, andreadonlygets the value from the profile.Version gate
The packet has the protocol version
54495(DBMS_MIN_PROTOCOL_VERSION_WITH_RESET_SESSION). It is gated on the server's version from the server Hello, not on the client's version. The server accepts the packet from a client with any version, because neither the request nor the reply has a field that depends on the version.A gate on the client's version would force a client to declare
54495. With that declaration, the server switches on every feature up to54495for the connection, which includes sparse columns (54465), replicated columns (54482) and the newStringlayout (54492). For example, the native client in clickhouse-cs declares54460, and it would have to implement all of these before it could clean its pooled connections. The clientTablesStatusRequestalready uses the same pattern:ConnectionEstablishersends it only ifserver_revisionis high enough.An older server closes the connection when it receives an unknown packet, so a client must check the server's version first. The spec in
docs/resources/develop-contribute/native-protocol/protocol.mdxdescribes this.Benchmark
On review, tavplubix asked how much this saves. We measured the clickhouse-cs native client against ClickHouse 26.7 in Docker, with network delay added by
tc netem. The reset was simulated withSELECT 1on an open connection. The other option was to close the connection and open a new one (TCP, TLS,Hello). Median time for one cleanup:SELECT 1, TLS)The purpose is to avoid additional connection-establishment round trips and reduce socket churn for applications that frequently reuse pooled connections.
ResetSessiontakes one round trip on the existing connection. In this benchmark setup, a plain reconnect takes two round trips and a TLS reconnect takes three. Keeping the socket open also avoids repeatedly creating and closing sockets and reduces pressure on ephemeral ports andTIME_WAITunder heavy connection turnover.The measurements above use
SELECT 1as a latency proxy; they are not measurements of the actual reset implementation.Regression coverage
The executable stateless tests cover session cleanup, transaction rejection without aborting the transaction, current access-control defaults, one-way
EXECUTE AS, authentication-scoped grants, restrictive settings, quota consumption, and state preservation when database validation fails. Integration tests cover authentication expiry, session limits, all four chunked/unframed combinations with an older client revision, and external-authentication profile replay and failure handling.Limits
EndOfStreambefore treating a connection as clean. OnException, the previous session state can remain; discard the connection if cleanup cannot be completed.apply_settings_from_server) keeps the values from login.session_id.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The native protocol has a new client packet
ResetSession. It returns the session of a connection to its state right after authentication: settings, roles, current database, temporary tables and query parameters. Client connection pools can use it to clean a connection without a new connection. A session that runs as another user afterEXECUTE AScannot be reset. A client can use the packet when the server's protocol version is54495or more, whatever its own version is.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/123914]