Skip to content

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

Done

Description

@datlechin

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

  • makeTunnel calls probeForwardDestination after authentication (apple/TablePro/Core/SSH/LibSSH2TunnelFactory.swift:97-104). The probe opens a channel, then calls libssh2_channel_close and libssh2_channel_free (:1101-1145). The driver's connection opens its own channel in openAndRelay (apple/TablePro/Core/SSH/LibSSH2Tunnel.swift:264-330), which already records a failed open in forwardFailure (:326) for consumeLastForwardFailure (:338-346).
  • SSHTunnelManager keys tunnels by connectionId (apple/TablePro/Core/SSH/SSHTunnelManager.swift:30-124, :115), and every createTunnel builds the full chain. SSHSessionPump already multiplexes many relayed channels on one session.
  • createTunnel runs in Task.detached (SSHTunnelManager.swift:81-89), on the global cooperative executor. Inside it, connectTCP blocks in poll() (apple/TablePro/Core/SSH/LibSSH2TunnelFactory+TCP.swift:58-74), createSession sets blocking mode and runs the handshake (LibSSH2TunnelFactory.swift:636-651), and authentication and the probe block too. Address resolution in the same file already moves getaddrinfo to a DispatchQueue (LibSSH2TunnelFactory+TCP.swift:116).
  • buildEffectiveConnection on the @MainActor DatabaseManager reads the SSH password, key passphrase and TOTP secret with synchronous SecItemCopyMatching, whatever the auth method (apple/TablePro/Core/Database/DatabaseManager+SSH.swift:87-94). The driver path already reads secrets off the main actor with DeferredSecretRead (apple/TablePro/Core/Database/DatabaseDriver.swift:1387-1394).
  • The SSH socket gets only keepalive options (LibSSH2TunnelFactory+TCP.swift:92, :190-194), no TCP_NODELAY. The core sets it (core/crates/net/src/socket_options.rs:8).
  • connectTCP tries 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 in core/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 pluginDispatchAsyncCancellable pattern (apple/Plugins/TableProPluginKit/PluginConcurrencySupport.swift:35). The existing socket shutdown in SSHConnectionAttempt stays the cancel path.
  • Read the SSH secrets with DeferredSecretRead off the main actor, and only the ones the auth method needs.
  • Set TCP_NODELAY on 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_OPEN per connect 2 today, 1 after.
  • 5 connections behind one bastion, Reopen Last Session at +50 ms: sshd Accepted publickey lines 5 today, 1 after; time to all sessions live, median of 5.
  • Instruments Swift Concurrency template while 10 SSH connections recover after pmset sleepnow and wake: no cooperative thread blocked in poll or recv.
  • Time Profiler main thread during Reopen Last Session: no SecItemCopyMatching under buildEffectiveConnection.
  • 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

  1. added
    enhancement
    platform-macos
    on Nov 25, 2026
  2. datlechin commented on Nov 25, 2026

    @datlechin
    MemberAuthor

    #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:

    • Sharing one session across destinations. It needs channel opens that do not block each other, which libssh2's API cannot do: either a change to libssh2's channel open state, or the opens moved to the core's SSH transport.
    • The in-app measures: Reopen Last Session timing at +50 ms, the Swift Concurrency template during recovery after wake, and the Time Profiler main thread. The harness numbers in perf(ssh): share one session per route, use the probe channel, keep libssh2 off the pool #4573 cover the same code paths: sign-ins 5 to 1, channel opens 2 to 1 per connect, cooperative threads blocked in poll 10 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementplatform-macos

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions