Making SSH channel opens overlap without making the session unsafe

Four focused libssh2 patches removed serial waits from Waycar's connection path, with a delayed OpenSSH fixture checking both the saved time and the failure cases.

5 min read

On this page
  1. I wanted fewer waits, not more SSH connections
  2. What I measured
  3. Signing the first request
  4. Asking for the PTY and process together
  5. The small packet bug underneath concurrency
  6. Moving an open’s continuation onto the channel
  7. What I’d tell you if you build on libssh2

I wanted fewer waits, not more SSH connections

Waycar is my iPhone and iPad console for coding agents over SSH. One connection carries API requests to a remote Unix socket as well as terminal work. Opening independent channels on that connection should not make each request wait for the previous channel’s network round trip.

My timeline said otherwise. Channel opens finished a round trip apart. Wrapping the work in concurrent Swift tasks did not help, because the unfinished native operation belonged to the session, not the task.

I use libssh2 through the repository’s own WaycarSSH package. It does the SSH work the app depends on, and I did not need a different SSH library. I needed to understand where its non-blocking retry state lived, then change a few specific contracts.

The result was a patch set against libssh2 snapshot c7557852, dated 2026-10-03. These are local patches. Upstream libssh2 has not adopted the interfaces below.

What I measured

On 2026-10-03, make test-libssh2-patches ran against a throwaway OpenSSH 10.3p1 server behind a proxy that adds 80 ms of round-trip delay, with a fixed delay in each direction. The timing helper keeps the best of three runs of each comparison, because local scheduling and login setup add noise.

The best-of-three results:

Operation Serial/default path Patched path
Publickey authentication 218-234 ms 117-132 ms
PTY request plus exec 192 ms 93 ms
Four direct-streamlocal opens 366 ms 95 ms

These time single operations, not app launch. I would not turn them into a promise about every phone or server. The fixture makes wasted round trips visible, then checks that removing them did not skip required behavior.

Signing the first request

Publickey login normally sends an unsigned request first, asking whether the server would accept the key. After SSH_MSG_USERAUTH_PK_OK, it signs and sends the real request.

Patch 0001 adds an opt-in session flag, LIBSSH2_FLAG_PUBKEY_SKIP_QUERY. With it set, the first request is signed. The protocol allows that, and the default stays the same.

There is a cost. A key the server will reject now costs a signature before the rejection, and the patch documents a different error for that case. If signing needs a scarce or interactive resource, an app may not want to skip the query.

The tests check the server log for unsigned queries and signed requests, not just the time. They cover accepted and unauthorized keys. So the speedup has a protocol reason: the query is gone, and login does the same work.

I think this one could go upstream as an opt-in feature. The behavior and the error need to be part of the public contract.

Asking for the PTY and process together

The next wait sat between requesting a PTY and starting its command. Patch 0002 adds libssh2_channel_request_pty_process_startup(). It sends both requests back to back, then reads their replies in order.

Non-blocking retries still resume the same request buffers. A combined function does not make a half-sent packet go away.

One failure case matters most: the server can refuse the PTY and still run the command. The API reports which requests were denied, and its contract tells callers to close the channel on failure. A PTY refusal does not prove that no command ran.

The fixture uses a key denied PTY access and an unknown subsystem to test the refusals. This patch could also go upstream as a feature, if the combined API and its partial-success rules get review. It does not drop in for every caller’s existing sequence.

The small packet bug underneath concurrency

Patch 0003 is the clearest upstream bug fix.

ssh2_packet_requirev() waits for chosen packet types, optionally for one channel only. At the pinned snapshot, if it read a wanted packet type for a different channel, the filtered wait failed with -1. The packet belonged to another waiter. That was no reason for this waiter to fail.

The fix keeps reading when the type matches but the channel does not. The other channel’s packet stays queued for its owner. Callers without a channel filter still accept any packet of the wanted type.

You only see this once several requests wait on the session. Then the shared socket can deliver somebody else’s answer first. With this patch removed, the regression tests reproduce the failure.

I would offer this fix upstream on its own, apart from the new APIs. Matching a reply to its channel is useful whether or not the maintainers want my pipelining interface.

Moving an open’s continuation onto the channel

Patch 0004 adds start and finish calls for session, TCP-forward and stream-local channel opens. The start call creates a channel and builds its request without network I/O. The finish call sends it and waits for the reply addressed to that channel’s local id.

prepare channel A
prepare channel B
drive A until it waits for its reply
drive B until it waits for its reply
resume each through its own finish operation
free every prepared channel, including refusals

This sketch does not mean you can call libssh2 from several threads at once. Waycar’s driver still runs native calls one at a time under a mutex. It can release the mutex between turns because each open’s retry state now lives on its channel.

The outgoing side is still shared. If it holds a half-sent packet, the operation that owns that packet must finish sending it before anything else can send. Separate reply state does not mean separate send buffers.

Cancellation needs the same care. If the server may have seen an open, freeing it first settles the reply and closes any confirmed channel. An open that never reached the transport can be dropped locally. A refused open still has to be freed, but it no longer costs the whole SSH session.

The fixture checks that channels still work around a refusal, and that freeing an unfinished session-channel open gives the server its capacity back. A fast open that leaks a server channel is no improvement, so these checks count as much as the faster table row.

I think the per-channel design could go upstream, but the public API and lifetime rules need discussion. The packet fix is required before matching waits can overlap at all.

What I’d tell you if you build on libssh2

Find where retry state lives before you add concurrency. Ask which object owns a retry, which packet a waiter may consume, and who finishes cleanup after a cancel.

Then put delay in the test path and check what the server did. A local connection can hide serial waits. A stopwatch alone can hide a refusal or a leaked channel. I want both the saved round trip and proof that the session still works.

This is one chapter of One developer, a team of AI agents, and the stack under an iPhone app, the story of how Waycar and the libraries under it were built.