Skip to content

Exponential backoff to prevent aggressive resends - #836

Open
cgull wants to merge 3 commits into
mobile-shell:masterfrom
cgull:rate-decay
Open

cgull wants to merge 3 commits into
mobile-shell:masterfrom
cgull:rate-decay

Conversation

@cgull

@cgull cgull commented Dec 22, 2016

Copy link
Copy Markdown
Member

If a client has one-way connectivity to a server such that it keeps requesting the same state from the server, the server may spam the client with updates on its interactivity-timeout interval of 40ms until 10s have gone by, at which point it reverts to a 3s interval. With large terminal windows, this can cause excessive bandwidth usage. If the link has limited bandwidth causing it to drop some packets from a large message, the flood of these packets can also prevent the client from receiving a new state successfully. See #747 and #832. I also think this issue might be responsible for some of the "VPN" issues that we have lumped under #299.

This implements a rather crude exponential backoff for resends, simply delaying the next send by the amount of time since the first unsuccessfully sent state. The actual delays calculated are rather noisy and sometimes larger than they need to be. The backoff is initiated after at least two messages have been sent to the remote and not acked; we need to work from the timestamp of the first unsuccessful send, not the timestamp of the last successful send (which may have been unusefully long ago), and one of the messages sent in this time may have been an ack-only message, which doesn't count. This inherently adds a bit of conservatism to the backoff. The backoff is limited by the existing calculations for next_send_timer and next_ack_timer which currently impose a ceiling of 3 seconds.

I was concerned about the impact of exponential backoff-- I thought that its much less frequent sends on lossy networks would result in laggier interactive behavior. But in testing with Network Simulator on OS X with 50% packet loss and 1s+ latency, I don't see any change in behavior-- Mosh seems fine.

Comments and testing greatly appreciated.

@cgull

cgull commented Aug 3, 2017

Copy link
Copy Markdown
Member Author

Testing this code again, I don't see it working reliably on a link with no delay and 50% loss. Mosh still is too aggressive about resends but this isn't the fix for it.

mogwai added a commit to mogwai/mosh that referenced this pull request Jul 30, 2026
Four related changes to the transport layer.

Settle on an empty diff. When the computed diff is empty the receiver
already renders what we hold, but current_state can still differ from the
last sent state under operator==, which compares terminal internals the
display never emits. calculate_timers() only consults that comparison, so
it re-armed a send every pass, tick() produced no bytes, and the sender
spun at the mindelay rate until an empty ack happened to record the state.
Recording it directly costs nothing: the empty diff is itself the proof
that the peer's rendering matches.

Do not schedule sends with nothing to send. Settling fixes the first timer
branch; the retry branches schedule from a fixed sent_states.back()
timestamp, which is permanently in the past once passed. Gate them on a
nothing_to_send flag, set only when the diff is still empty *after*
attempt_prospective_resend_optimization() -- that optimisation is what
legitimately resends from the acknowledged state, so an empty diff after
it means there is genuinely nothing to retransmit. The flag is recorded
against the assumed receiver state it was judged from and re-evaluated if
that moves, because update_assumed_receiver_state() decays it backwards
when a send goes unacknowledged, and that decay is how a lost state gets
retransmitted.

Back off keepalives on an idle session. An idle session sent an empty ack
every three seconds in each direction forever, which keeps a phone or
laptop radio from settling. The interval now doubles up to 15 s while the
session is idle, where idle means the peer has acknowledged everything we
sent -- an unacknowledged state is equally consistent with a peer that has
stopped listening, which is when we least want to go quiet. Both ends use
the same ceiling: the client's keepalives hold the NAT mapping open so it
must wake on that schedule regardless, and 30 s is a common floor for NAT
UDP binding lifetime. The client relaxes its "last contact" threshold to
match, so deliberate silence is not reported as a stalled link.

Replace the replay high-water mark with a sliding window. A single
high-water sequence number cannot distinguish a duplicate from a packet
that merely arrived late, and replayed datagrams were still passed up to
the transport layer rather than dropped. ReplayWindow tracks the highest
sequence number plus a bitmap of the 64 below it, so reordering is
accepted exactly once and replays are dropped, with unit tests.

Also fix ECN on IPv6, which was broken three ways: the setup guard
compared an address family against a protocol constant and so was always
false, IPv6 sockets never requested IPV6_TCLASS/IPV6_RECVTCLASS, and the
receive path inspected only the first control message and only for IPv4.

Measured against unmodified master, using the real binaries: idle traffic
over 150 s falls from 111 datagrams to 30 and server wakeups from 105 to
29; a workload of screen updates that change nothing falls from 1795
server wakeups to 69 over 20 s. Sessions still converge through a UDP
relay dropping 15%, 30% and 50% of datagrams in both directions.

Also holds off resends when sent states are going unacknowledged, rather
than retrying at the frame rate for the full ACTIVE_RETRY_TIMEOUT (the
backoff_send() helper, ported from upstream PR mobile-shell#836, unmerged since 2016).
On a link with one-way connectivity that retry rate is a flood, and on a
merely lossy link the flood is itself what prevents a large state getting
through once connectivity returns.

Measured with a UDP relay that blocks the client->server direction for
10 s: unmodified master pushes 402 datagrams that nothing can acknowledge,
this build pushes 60. That is a whole-build figure -- the settling and
nothing-to-send changes above suppress sends on the same path, and the
contributions have not been separated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant