Repository navigation
Done
Labels
Description
Problem
An SSH connect costs more round trips and more blocked threads than it needs:
- Every tunnel build opens a direct-tcpip channel to the database, closes it, and then opens a second one for the driver. That is 1 extra SSH RTT plus the bastion's own TCP connect to the database, on every connect, recovery and rebuild after wake (about 50-60 ms at +50 ms, estimated).
- Ten connections behind one bastion pay ten SSH handshakes and ten authentications (about 5-8 RTT plus key-exchange and signature CPU each, estimated), including any agent or TOTP prompts.
- The blocking handshake runs on Swift's cooperative pool. When as many SSH dials run at once as there are cores (Reopen Last Session, recovery after wake), other async work in the app stalls for the length of the negotiation, up to the connect timeout on a dead host. Not measured.
- Three Keychain reads per SSH connect run on the main thread.
- A bastion name whose first address is a black-holed IPv6 address fails at the full connect timeout and never tries the IPv4 address.
Cause
makeTunnelcallsprobeForwardDestinationafter authentication (apple/TablePro/Core/SSH/LibSSH2TunnelFactory.swift:97-104). The probe opens a channel, then callslibssh2_channel_closeandlibssh2_channel_free(:1101-1145). The driver's connection opens its own channel inopenAndRelay(apple/TablePro/Core/SSH/LibSSH2Tunnel.swift:264-330), which already records a failed open inforwardFailure(:326) forconsumeLastForwardFailure(:338-346).SSHTunnelManagerkeys tunnels byconnectionId(apple/TablePro/Core/SSH/SSHTunnelManager.swift:30-124,:115), and everycreateTunnelbuilds the full chain.SSHSessionPumpalready multiplexes many relayed channels on one session.createTunnelruns inTask.detached(SSHTunnelManager.swift:81-89), on the global cooperative executor. Inside it,connectTCPblocks inpoll()(apple/TablePro/Core/SSH/LibSSH2TunnelFactory+TCP.swift:58-74),createSessionsets blocking mode and runs the handshake (LibSSH2TunnelFactory.swift:636-651), and authentication and the probe block too. Address resolution in the same file already movesgetaddrinfoto aDispatchQueue(LibSSH2TunnelFactory+TCP.swift:116).buildEffectiveConnectionon the@MainActorDatabaseManagerreads the SSH password, key passphrase and TOTP secret with synchronousSecItemCopyMatching, whatever the auth method (apple/TablePro/Core/Database/DatabaseManager+SSH.swift:87-94). The driver path already reads secrets off the main actor withDeferredSecretRead(apple/TablePro/Core/Database/DatabaseDriver.swift:1387-1394).- The SSH socket gets only keepalive options (
LibSSH2TunnelFactory+TCP.swift:92,:190-194), noTCP_NODELAY. The core sets it (core/crates/net/src/socket_options.rs:8). connectTCPtries the addresses one at a time, and a poll timeout throws instead of moving on (LibSSH2TunnelFactory+TCP.swift:68-74). The core implements RFC 8305 incore/crates/net/src/dial.rs.
Overlaps #4459: C5e already covers using the probe channel for the first connection (handshake_rtts 9 to 8, channel opens 4 to 3). This issue keeps that and adds the rest.
Change
- Keep the probed channel and hand it to the first client the listener accepts, so the early diagnosis costs nothing extra (C5e). Later clients open their own channel as today.
- An SSH session registry keyed by the resolved route (each hop's host, port, user, host key and auth identity). Each database connection takes a reference and gets its own listener whose clients open channels on the shared session, the OpenSSH ControlMaster model. Death of the shared transport starts every holder's recovery. Check port-forward channel limits against common sshd settings.
- Run the blocking negotiation (TCP connect, handshake, auth, probe) on a dedicated queue per attempt and bridge with a checked continuation, the
pluginDispatchAsyncCancellablepattern (apple/Plugins/TableProPluginKit/PluginConcurrencySupport.swift:35). The existing socket shutdown inSSHConnectionAttemptstays the cancel path. - Read the SSH secrets with
DeferredSecretReadoff the main actor, and only the ones the auth method needs. - Set
TCP_NODELAYon the SSH transport socket and on each jump-hop socket. Treat it as hygiene: a capture should confirm it, no connect-time win is expected. - Race addresses with RFC 8305 on the BSD-socket path: alternate families, 250 ms stagger, take the first that connects and close the rest, each sharing the deadline, following
core/crates/net/src/dial.rs.
Measure
- tcpdump on the SSH port:
SSH_MSG_CHANNEL_OPENper connect 2 today, 1 after. - 5 connections behind one bastion, Reopen Last Session at +50 ms: sshd
Accepted publickeylines 5 today, 1 after; time to all sessions live, median of 5. - Instruments Swift Concurrency template while 10 SSH connections recover after
pmset sleepnowand wake: no cooperative thread blocked inpollorrecv. - Time Profiler main thread during Reopen Last Session: no
SecItemCopyMatchingunderbuildEffectiveConnection. - A bastion name with an unroutable AAAA and a working A record: connect in about 250 ms plus one connect instead of failing at the timeout.
Part of the performance plan.
Activity
datlechin commentedon Nov 25, 2026
#4573 ships every item in Change, with one narrower scope: the session registry keys by destination as well as by route, so connections share a session only when they reach the same database through the same bastion.
Why: libssh2 opens one channel at a time per session (
session->open_state). A call made while another open is pending carries that open on and takes its channel. A destination behind a firewall that drops packets holds its open until the server's own connect gives up (127 s on Linux), so on a session shared across destinations every other database's new connections would wait behind it. #4573 queues opens per session and settles an abandoned one before the next starts.Left:
poll10 of 10 to 0 during a 10-tunnel connect, and a black-holed first address connecting after 253 ms instead of failing at the timeout.