I routed every terminal through a server that had no reason to see it

Last week a user’s open terminal died during a routine cluster node drain. The machine they were typing into was healthy. Their device was healthy. The private mesh network connecting the two was healthy. The terminal died anyway, because I had designed it to pass every keystroke through a central controller, and the controller’s ingress, routing peer and database all blinked at once. None of those components had anything to do with the bytes the terminal was carrying. I had put them in the path regardless.

This post is about how that happened, because the mistake was not in the code. The code did exactly what the design said. The mistake was in the design, and then in a second design decision that had every opportunity to correct the first one and didn’t.

What I built

The tool is a private development environment. It has a controller running in a cluster, agents running on each development machine, and a client the operator uses from a laptop or phone. The whole premise, stated in the very first design document, is that every one of these is a peer on one private mesh network. That is the deployment model. It is not optional, and it is not an add-on.

When I first sketched how a terminal session reaches the interface, I wrote that it was “reachable from the interface” and left the path unspecified. A couple of weeks later, while writing up how a shell works, I wrote that its keystrokes and output “pass through the controller.” I did not deliberate over that sentence. It felt natural: the agent already held one outbound connection to the controller, the client already talked to the controller for everything else, so the controller was simply where things met.

So the agent read PTY output, framed it, and sent it up its single connection to the controller. The controller decoded it, handed it to the API layer, and forwarded it down a second WebSocket to the browser. Input went the other way. Port forwards used the same path. Every byte crossed the mesh to the cluster, through the ingress, through two WebSockets and the controller process, then back across the mesh to the agent. In each direction.

The moment I should have caught it

About a month later, someone reported that typing into a terminal could take up to ten seconds to echo when the machine was under load. I investigated and wrote up the cause accurately: the terminal’s bytes shared one ordered queue with everything else the agent sent, and the controller recorded every frame in arrival order before acknowledging the next. Terminal bytes were not recorded at all, so they were waiting behind work that had nothing to do with them.

I wrote, in that very issue, the sentence “terminal bytes are stored nowhere, so nothing waits on the store for them.” Then I designed a priority lane so terminal and forward frames could jump the queue in both directions.

Read that again, because I had to. I had in front of me a component that did not record the stream, did not read it, did not transform it, and existed in the path only to pass it along. I had the design’s own premise that both endpoints were peers on one private network. And the fix I chose was to make the unnecessary hop faster rather than ask why it was there.

The lane worked. Latency went down. That is the dangerous kind of fix: it is correct, it is measurable, and it entrenches the wrong topology by making it tolerable.

What it actually cost

  • Terminal latency had a floor set by a round trip through the cluster and two relays, and a ceiling set by whatever else the agent’s connection was doing. The lane lowered the ceiling. It could not touch the floor.
  • Every open terminal and every port forward consumed controller CPU and cluster ingress bandwidth to move bytes the controller never looked at.
  • A terminal depended on the controller, its ingress, its routing peer and its database all being up. A controller outage would drop every terminal to every healthy machine simultaneously. That is what happened during the node drain.
  • Port forwards carried real application traffic through the same path, with the same costs.

What the design should have said

From the first time a terminal appeared in the design, and certainly when I filed the latency issue, the design should have stated that a terminal or a forward is a direct connection between the device and the machine’s agent across the mesh. The controller’s job is what it was already actually doing: authorizing the attachment and recording that it opened and closed.

That is now the shape:

  • The agent listens for terminal and forward connections on its mesh address only and reports that address to the controller.
  • When a device wants to attach, the controller issues it a short-lived signed grant naming the device, the machine and the target, along with the machine’s mesh address and the key the device uses to verify the agent.
  • The agent admits a connection only on a valid grant and serves it directly. No byte passes through the controller.
  • Once admitted, a connection survives the controller being unreachable.
  • If a device cannot reach the machine’s mesh address, it says so. There is no fallback relay, because by design there is no device that isn’t on the mesh.

The authorization pattern here is not novel. The agent already held a key the controller trusted. Issuing a grant signed against it is a standard move. The only thing stopping me was an unexamined assumption that the agent dials out and never listens, which I had never written down as a requirement and therefore never weighed against what it was costing.

What I’m changing about how I work

Relay-and-discard is a flag, not a feature. If I find myself writing “relayed and stored nowhere” in a doc comment, that is the moment to stop and ask what the component is for. A hop that neither records, reads nor transforms a stream is almost always there because nobody drew the topology.

A priority lane inside a shared hop is a symptom fix. When latency-sensitive interactive traffic contends with bulk traffic, the first question is why they share a path at all. Prioritization is the answer only after that question has been asked and answered with a reason.

Reread the premises before designing on top of them. The design document said, in a table, that every machine and every device is a peer on one private mesh. I had written that sentence myself. A design that holds that premise and still routes peer-to-peer traffic through a central service owes an explanation, and if I cannot write the explanation, the routing is wrong.

Write assumptions down so they can be challenged. “The agent serves no inbound connections” lived in my head as a constraint for two months. Had it been in the design as a stated requirement, the latency issue would have forced it into the open, and someone, probably me, would have asked whether it was worth ten seconds of echo delay.

The user who reported this put it more plainly than I have: the path should be client to mesh to agent, not client to mesh to cluster to agent. They were right, the design already contained everything needed to know they were right, and I built the long path anyway because I never stopped to look at the map.

Advanced search: filter by model or tag