Repository navigation
derp: accept dup clients without closing prior's connection - #2745
Conversation
802bbcf to
70a024b
Compare
danderson
left a comment
There was a problem hiding this comment.
Some minor thoughts, but one big one: I think this leaves a race condition open where a legitimate client can get itself disabled because it reconnected, but the old TCP connection still has a frame in flight out on the internet. derper could process those two things in the wrong order and end up disabling a legit client. Not sure how to fix :/
| clientsReplaced expvar.Int | ||
| clientsReplaceLimited expvar.Int | ||
| clientsReplaceSleeping expvar.Int | ||
| dupClientKeys expvar.Int // current number of public keys we have 2+ connections for |
There was a problem hiding this comment.
If this 2+ active connections? All clients with 2+ connections even in the "happy idlers" case? Separate vars for each case so we can distinguish probably-not-cloned and cloned?
There was a problem hiding this comment.
It's tricky to compute this efficiently so I'm putting it off for now. Happy to do it in a future change. Also in a future change want to add read timeouts on the frame reads and prune the "happy idlers" more aggressively, so there won't be much of a difference probably.
| // it's not very bad. Only their new one is | ||
| // active. The last one, being dead, doesn't send and | ||
| // thus the new one doesn't get disabled. | ||
| run("small_overlap_replacement", func(t *testing.T) { |
There was a problem hiding this comment.
Hm, this test makes me realize there's still a small race here: a client could send then immediately switch networks and reconnect. Since the sclients aren't synchronized with each other, the derp server could process c2 first, mark it active, then receive a frame from c1 that was still in flight, and incorrectly disable c2 :/
Maybe there needs to be some kind of grace period immediately after c2 shows up, where we still allow a little crosstalk? Distributed systems are annoying :(
There was a problem hiding this comment.
given how many RTTs a DERP setup takes, I'm not particularly worried about that
| // A dupClientSet is a clientSet of more than 1 connection. | ||
| // | ||
| // This can occur in some reasonable cases (temporarily while users | ||
| // are changing networks) or in the case of a cloned key. In the |
There was a problem hiding this comment.
Is it possible for a single peer to rapidly switch between two networks because both are flaky (e.g., cellular hotspot and trashy WiFi)? Wouldn't that result in both connections being disabled, which seems like a strictly worse outcome?
There was a problem hiding this comment.
The client only has one connection open at a time per region so I don't think that can happen.
9ca1514 to
1d7b2cb
Compare
|
PTAL |
I'm not entirely convinced yet, but I don't feel super strongly about even disabling fighting dups at all. We could just have the policy (for now) that the last active sender is the sole active one. Want me to just disable the disabling for now and I can add it back later incrementally with some varz so we can understand what patterns are really happening in the wild first? |
|
Hmm, last active sender wins would mean that for actual duped machines, we end up ping-ponging between them as to who gets to have connectivity today, but it'd mostly work okay for other cases, I think. Worst case in the rare race case for a legit client, we have to wait until the ping-pong keepalive times out, at which point we'll reselect the correct (only) client left and it'll all work. Sure, let's try that. |
1d7b2cb to
2d326da
Compare
|
PTAL |
A public key should only have max one connection to a given DERP node (or really: one connection to a node in a region). But if people clone their machine keys (e.g. clone their VM, Raspbery Pi SD card, etc), then we can get into a situation where a public key is connected multiple times. Originally, the DERP server handled this by just kicking out a prior connections whenever a new one came. But this led to reconnect fights where 2+ nodes were in hard loops trying to reconnect and kicking out their peer. Then a909d37 tried to add rate limiting to how often that dup-kicking can happen, but empirically it just doesn't work and ~leaks a bunch of goroutines and TCP connections, tying them up for hour+ while more and more accumulate and waste memory. Mostly because we were doing a time.Sleep forever while not reading from their TCP connections. Instead, just accept multiple connections per public key but track which is the most recent. And if two both are writing back & forth, then optionally disable them both. That last part is only enabled in tests for now. The current default policy is just last-sender-wins while we gather the next round of stats. Updates #2751 Signed-off-by: Brad Fitzpatrick <[email protected]>
2d326da to
d45bc9b
Compare
crawshaw
left a comment
There was a problem hiding this comment.
My guess is "disable fighters" is going to cause rare but legitimate reconnects to fail. Consider: I have a working DERP connection (A), send some packets, they get buffered in a router, the router's upstream goes wonky for a few seconds, the user decides to switch their device to LTE, connects to DERP and starts talking (B), then the router's upstream comes back and it sends the buffered packets. Now DERP sees two live connections and have seen A B A so we disable both, and the user has to drop and reconnect to get traffic.
But it's disabled by default, so LGTM
| // ActiveClient returns the most recently added client to | ||
| // the set, as long as it hasn't been disabled, in which | ||
| // case it returns nil. | ||
| ActiveClient() *sclient |
There was a problem hiding this comment.
Always weird to see an exported method returning an unexported type. But it's fine I guess
| s.clients[c.key] = singleClient{c} | ||
| case singleClient: | ||
| s.dupClientKeys.Add(1) | ||
| s.dupClientConns.Add(2) // both old and new count |
There was a problem hiding this comment.
does the old client get unregistered? (I'm guessing not, because that would close it?) I don't really understand why it isn't already counted
There was a problem hiding this comment.
Connection 1 (C1) for Key 1 (K1) comes in, and it's not a dup, so there are zero dups of either.
Then C2 also for K1 comes in. Now dup keys is 1 but dup conns is 2 (it transitions from 0 to 2 initially: as C2 is a dup, but C1 retroactively is also a dup)
Updates tailscale#2745 Updates tailscale#7552 Signed-off-by: Spike Curtis <[email protected]>
Updates #2745 Updates #7552 Signed-off-by: Spike Curtis <[email protected]>
Updates tailscale#2745 Updates tailscale#7552 Signed-off-by: Spike Curtis <[email protected]>
Updates tailscale#2745 Updates tailscale#7552 Signed-off-by: Spike Curtis <[email protected]>
A public key should only have max one connection to a given
DERP node (or really: one connection to a node in a region).
But if people clone their machine keys (e.g. clone their VM, Raspbery
Pi SD card, etc), then we can get into a situation where a public key
is connected multiple times.
Originally, the DERP server handled this by just kicking out a prior
connections whenever a new one came. But this led to reconnect fights
where 2+ nodes were in hard loops trying to reconnect and kicking out
their peer.
Then a909d37 tried to add rate
limiting to how often that dup-kicking can happen, but empirically it
just doesn't work and ~leaks a bunch of goroutines and TCP
connections, tying them up for hour+ while more and more accumulate
and waste memory. Mostly because we were doing a time.Sleep forever
while not reading from their TCP connections.
Instead, just accept multiple connections per public key but track
which is the most recent. And if two both are writing back & forth,
then optionally disable them both. That last part is only enabled in
tests for now. The current default policy is just last-sender-wins
while we gather the next round of stats.
Updates #2751