Why our SSH engine cut off servers that did nothing wrong

By Pavel Stegnii · Published · All articles

Short version. MyXterm's built-in SSH engine closed a channel when the server sent data faster than the app took it: a shell after 16 MiB of unread output, an X11 window after 4 MiB. The server had done nothing wrong. The real cause sat in the SSH library we use, which re-opens a channel's receive window as soon as data arrives instead of when the reader takes it. We now carry a small, checked patch to that library, and a paused channel holds one 2 MiB window and stays open. Merged on 2 December 2026 in pull request #80.

How SSH says "slow down"

One SSH connection carries many channels: your shell, an SFTP session, port forwards, X11 windows. They share a single TCP stream, so TCP's own flow control can't slow one of them down without stalling all of them. SSH solves this with a window per channel [5]. The receiver tells the sender how many bytes it may send. Each data packet uses up part of that allowance, and the receiver tops it up with a WINDOW_ADJUST message. A sender that has used up its window must wait.

The whole idea depends on one choice: when the receiver sends that top-up. Send it when the data has actually been read, and a slow reader slows the sender down. Send it when the data merely arrives, and the window never closes. The sender keeps going at full speed, and the bytes pile up somewhere in the receiver.

The trap in the library

Our native engine is built on russh 0.65.4, a pure-Rust SSH library [2]. Upstream russh re-opens a channel's window when data arrives, then hands the data to a bounded queue for that channel. If the application reads slower than the server writes, that queue fills, and the library's session loop waits until there is room. That loop serves every channel on the connection, so one unread channel freezes the shell, keepalives and even disconnect.

This is not a niche case for us. A terminal in a background tab parses output but doesn't draw it; a cat of a big log produces output faster than any screen can show; an X11 client can outrun a busy local X server. Upstream knows the mechanism: issue #829 describes it on the server side, and two pull requests have proposed a fix, #831 (open) and #730 (closed without merging) [3].

Our first answer, and why it was wrong

When a design review of the engine pointed at the frozen loop on 27 November, we did what we could without touching the library. Every channel got its own pump task that always drained the library's queue into our own queue, capped in bytes. If a channel fell 16 MiB behind (4 MiB for X11), we closed that channel. One stuck channel no longer froze the others. We wrote that up as a fix.

It was only half of one. A code review on 2 December read the pump again and filed it as bug BUG-010, severity high [4]. Because the library still re-opened the window on arrival, the server never got told to slow down. An honest server sending a large log to a paused tab hit our cap and lost its channel. The cap was a memory limit dressed up as flow control. Raising it would only have moved the cutoff, and 16 MiB per channel times many tabs is already a lot of memory spent on bytes nobody is looking at.

When the window re-opens: on arrival versus on consumption Before: the client re-opens the window as soon as data arrives, so the server never stops, the queue grows to 16 MiB and the channel is closed. After: the client re-opens the window only when the reader takes data, so the server stops after one 2 MiB window and the channel stays open. Before: window re-opened on arrival Server never told to stop Client queue grows to 16 MiB Channel closed reader was paused After: window re-opened on consumption Server waits after 2 MiB Client queue at most one window Reader takes data then credit is sent WINDOW_ADJUST
The same pause, before and after: the only difference is when the client sends WINDOW_ADJUST.

The fix: credit on consumption

The right answer needed a change inside russh, so we carry one [3]. It is a single patch file, about 340 changed lines including tests and comments, behind a client option that is off by default. With consumer_window_adjust on, incoming data only uses up the window:

if self.common.config.consumer_window_adjust {
    // MyXterm: the window re-opens when the consumer credits it (WindowCredit).
    enc.consume_window(channel_num, data.len());
} else if enc.adjust_window_size(channel_num, &data, target)? {

The application gives the window back with WindowCredit::consumed(bytes). That call is synchronous, never blocks and never allocates: it adds to a per-channel counter and wakes the session loop, which has one extra branch that sends a WINDOW_ADJUST for every channel with credit. A credit can never push the window above its configured size, a credit for a closed channel does nothing, and each channel has its own counter, so a stale handle can't top up a newer channel that reuses the same id.

Our engine credits when the reader takes data, in batches of a quarter window, with no timer. It also credits bytes it throws away on purpose and whatever is still queued when a reader goes away; forget one of those and a channel would stall forever. Each channel's window is 2 MiB. There are now three different close reasons instead of one: a server that sends more than it was allowed (a protocol violation), a memory backstop that an honest server can't reach, and the 64 KiB limit for X11 data before its cookie is checked.

Switching the window logic flushed out a second bug. When a request on a new channel failed (a refused pty, say), the engine dropped the channel without sending CLOSE. Under the old logic nobody noticed. Under the new one, the server side of that channel would block after 2 MiB and stay that way until the connection ended. That became BUG-011 and was fixed in the same pull request.

What a patched library costs

Carrying a modified dependency is a supply-chain decision, so we wrote down what it costs [2]. The copy lives in third_party/russh: the crates.io tarball, unmodified, plus the patch. A script rebuilds it from the checksummed tarball and fails CI on any drift, and the same CI job runs russh's own test suite against the copy.

The less obvious cost: cargo-deny and cargo-audit only match security advisories against registry packages. We checked with a fake advisory: reported for the registry lockfile, silent for the patched one. So a second script audits a copy of our lockfile with russh pointed back at crates.io, on every push and weekly. The exit plan is written down as well: re-vendor within 14 days of any upstream security release, and drop the copy when upstream ships an equivalent client-side mechanism. A public fork would have the same advisory blind spot, so it is not an exit. Proposing the patch upstream is the owner's decision and hasn't happened yet.

How we tested it

The release gate is crates/ssh/tests/flow_control.rs, against an in-process test server that counts exactly what it sends [1]. The main test pauses a reader while the server floods it, keeps echoing through a shell on the same connection, and checks that:

  • the server sent exactly one window, 2048 KiB, and then stopped;
  • the channel is still open, and the shell's echo stayed under 500 ms;
  • after the reader resumes, every byte arrives in order (16 MiB locally, 128 MiB with a 5 s pause in CI);
  • once everything is read, no WINDOW_ADJUST is sent while idle.

Other tests cover 1-byte and 32-byte packet floods, a reader dropped with a full queue, credit arriving after close, a rekey every 1 MiB in the middle of a flood, a forward whose local peer stalls, and every channel kind moving more than three windows. A counting-allocator test checks that taking data and crediting it allocates nothing.

Before and after

Same binary, same machine, with a test-only switch between the old and new window logic [6]. Loopback, debug build, one run each (SFTP: median of three):

mode       exec MiB/s  SFTP MiB/s  echo p50 / p99   paused 128 MiB flood after 3 s
receipt        92         189      3.42 / 3.94 ms   closed: Overflow { cap: 16 MiB }
consumer       93         190      3.48 / 3.98 ms   open, 2052 KiB queued

Throughput and echo latency are the same within single-run noise. The difference is in the last column: the paused channel survives, and it holds one window instead of up to 16 MiB.

What is still open

  • Not verified by hand on Windows or Linux; those ran in CI only.
  • Not tested against a real OpenSSH server at full volume in CI. A 256 MiB paused-flood test against the Docker OpenSSH fixtures exists, but it runs only by hand.
  • No release-build numbers and no WAN latency numbers yet.
  • BUG-012 is open: after we close a channel first, russh keeps its handle until the connection ends, so one parked task stays per closed channel. Found by reading the code, not yet reproduced.

The lesson we keep relearning: a cap is not flow control. If the protocol has a way to say "slow down", the fix belongs where that signal is sent, even when that place is inside someone else's library.

Sources

Read on 3 December 2026. Pull requests, commits, decision records and benchmark results are in the MyXterm repository.

  1. Pull request #80, consumer-driven SSH flow control via a vendored russh patch: github.com/adramelech/MyXterm/pull/80, commits 671f0f6 and e59108c
  2. ADR-0004, native SSH engine on russh, point 5 (flow control): docs/architecture/adr/ADR-0004-native-ssh-russh.md
  3. What the russh patch changes, and the related upstream issue and pull requests: third_party/russh/MYXTERM_PATCH.md
  4. BUG-010, BUG-011 and BUG-012: docs/bugs.toml
  5. RFC 4254, section 5.2, data transfer and window adjustment: rfc-editor.org/rfc/rfc4254
  6. Benchmark, 2 December 2026: benchmarks/results/2026-12-02-ssh-flow-control

Last updated