Skip to content

perf(ssh): share one session per route, use the probe channel, keep libssh2 off the pool - #4573

Merged
datlechin merged 4 commits into
mainfrom
perf/ssh-session-reuse
Nov 26, 2026
Merged

datlechin merged 4 commits into
mainfrom
perf/ssh-session-reuse

Conversation

@datlechin

Copy link
Copy Markdown
Member

Summary

  • One SSH session per route. Connections that reach the same database through the same bastion, user, sign-in and secrets share one authenticated session, as with OpenSSH's ControlMaster (SSHSessionRegistry, SSHSessionRoute, LibSSH2SharedSession). Connections that start together wait on the one sign-in already running, so host key, passphrase and code prompts come once. A waiter that is cancelled or reaches its own deadline leaves without failing the others. When the shared transport dies, every connection on it starts its own recovery. Each connection keeps its own listener, clients and forward diagnosis (LibSSH2Tunnel).
  • The route includes the destination. libssh2 opens one channel at a time per session (session->open_state). An unreachable database behind a firewall that drops packets holds that open until the server's own connect gives up, often minutes later. Sharing across destinations would make every other database wait behind it, so it is not done here.
  • The probe channel is used for the first client (C5e). The destination check opens a channel and keeps it for the first client that connects within 1 s, so a connect opens one channel instead of two. After 1 s it is freed, since a server with a short handshake timeout may already have closed it.
  • Channel opens on a session queue. A call made while another open is pending carries on that open and takes its channel. An open a caller gives up on is finished in the background before the next one starts. One that never finishes ends the session.
  • Closing one connection ends only its relays. This uses SSHSessionPump.endRelay. A relay waiting for the channel's send window never reads its local socket's end of stream.
  • Blocking work runs off the cooperative pool. The TCP connect, handshake, sign-in (and its passphrase or code prompt), jump channel opens and the SFTP subsystem start run on a queue per attempt (SSHConnectionAttempt.runBlocking). The destination check runs on the tunnel's relay queue. Cancelling still works through the attempt's existing socket shutdown.
  • SSH secrets are read off the main actor, only when needed. buildEffectiveConnection and sshCredentials (remote file, remote SQLite) read through DeferredSecretRead, and only the secrets the sign-in method uses (SSHStoredSecretNeeds).
  • RFC 8305 dial. The SSH socket is dialed by SSHTransportDial, which follows core/crates/net/src/dial.rs: address families alternate, a new attempt starts every 250 ms or as soon as one fails, all attempts share one deadline, and the first to connect wins. The iOS app dials the same way. It used to give every address its own 10 s timeout.
  • TCP_NODELAY on the SSH socket, on macOS and iOS (SSHTransportSocketOptions, renamed from SSHTransportKeepAlive). A jump hop's next session runs over a socketpair, so the first hop's TCP socket is the only one that needs it.
  • Listen backlog raised to SOMAXCONN. On a busy Mac the 30-clients-one-tunnel matrix cells failed with "Connection reset by peer" about one run in three, on main too. They passed 12 of 12 runs after the change.

Test plan

Measured with the matrix harness (scripts/probes/ssh-tunnel-matrix), using the app's own tunnel code against the v10 OpenSSH server behind a toxiproxy that adds 25 ms each way. Channel opens are counted from the ctype direct-tcpip lines in sshd's DEBUG1 log, sign-ins from its Accepted lines. Before is origin/main f5e5ea6.

before after
1 connect: channel opens 2 1
1 connect: wall time, median of 3 551 ms 449 ms
5 tunnels to one database at once: sign-ins 5 1
5 tunnels at once: channel opens 10 5
5 tunnels at once: wall time, median of 5, interleaved 1198 ms 1180 ms
A connection joining a live session: time to forwarding full connect, about 400 ms 1 channel open, about 55 ms
10 tunnels at once at 800 ms RTT (sample): cooperative threads blocked in poll 10 of 10 0
First address black-holed, second live (unit test) fails at the connect timeout connects after 253 ms
  • 5 connections started together finish in about the same time as before. Their 5 sign-ins used to run in parallel, and 5 channel opens on one session now run one after another. The gain is 1 sign-in instead of 5 (and 1 prompt), and about 55 ms instead of a full connect for every later connection on the route.
  • TCP_NODELAY is set as hygiene. No connect-time gain was expected, and none was measured.
  • New tests: SSHTransportDialTests, SSHTransportSocketOptionsTests (renamed, NODELAY added), SSHChannelHandOffTests, two SSHSessionPumpTests cases for endRelay, SSHSessionRegistryTests, SSHSessionRouteTests, SSHStoredSecretNeedsTests.
  • scripts/probes/check-ssh-tunnel-matrix.sh --app macos: 86 of 87 cells as expected. The one miss was the backlog reset described above, which the backlog change fixes.
  • Not measured here: Reopen Last Session timing and the main-thread profile in the running app (no UI timing harness), and a real AAAA/A name.

Closes #4545

…ibssh2 off the pool

Connections to the same database through the same bastion, user, sign-in
and secrets share one authenticated session (SSHSessionRegistry,
SSHSessionRoute, LibSSH2SharedSession); each keeps its own listener in
LibSSH2Tunnel. Concurrent connects wait on the one sign-in under way, each
until its own deadline. The destination check keeps its channel for the
first client, so a connect opens one channel instead of two. Channel opens
queue per session and an abandoned open is settled before the next, since
libssh2 keeps one pending open per session. Closing one tunnel ends only
its relays (SSHSessionPump.endRelay).

The TCP connect, handshake, sign-in, jump opens and SFTP start run on a
queue per attempt instead of the cooperative pool. SSH secrets are read
through DeferredSecretRead, only those the sign-in method uses. The SSH
socket is dialed RFC 8305 style (SSHTransportDial), on macOS and iOS, with
TCP_NODELAY (SSHTransportSocketOptions, renamed from SSHTransportKeepAlive),
and the tunnel listen backlog is SOMAXCONN.
@datlechin
datlechin enabled auto-merge (squash) November 26, 2026 01:50
@datlechin
datlechin merged commit 7b91fec into main Nov 26, 2026
11 checks passed
@datlechin
datlechin deleted the perf/ssh-session-reuse branch November 27, 2026 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(ssh): drop the probe channel, share a session per bastion, keep libssh2 off the pool

1 participant