Skip to content

derp: accept dup clients without closing prior's connection - #2745

Merged
bradfitz merged 1 commit into
mainfrom
bradfitz/derp_dup
Aug 31, 2021
Merged

bradfitz merged 1 commit into
mainfrom
bradfitz/derp_dup

Conversation

@bradfitz

@bradfitz bradfitz commented Aug 30, 2021 •

Copy link
Copy Markdown
Member

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

@bradfitz
bradfitz requested review from crawshaw and danderson August 30, 2021 22:56
@bradfitz bradfitz changed the title derp: accept dup clients but don't send them traffic derp: accept dup clients but don't send fighting dups traffic Aug 30, 2021

@danderson danderson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :/

Comment thread derp/derp_server.go
clientsReplaced expvar.Int
clientsReplaceLimited expvar.Int
clientsReplaceSleeping expvar.Int
dupClientKeys expvar.Int // current number of public keys we have 2+ connections for

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread derp/derp_server.go Resolved
Comment thread derp/derp_server.go Resolved
Comment thread derp/derp_server.go Resolved
Comment thread derp/derp_server.go Resolved
Comment thread derp/derp_server.go Resolved
Comment thread derp/derp_server.go Resolved
Comment thread derp/derp_server.go Resolved
Comment thread derp/derp_test.go Outdated
// 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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given how many RTTs a DERP setup takes, I'm not particularly worried about that

Comment thread derp/derp_test.go Resolved
Comment thread derp/derp_server.go Resolved
Comment thread derp/derp_server.go
// 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The client only has one connection open at a time per region so I don't think that can happen.

Comment thread derp/derp_server.go Resolved
Comment thread derp/derp_server.go Resolved
Comment thread derp/derp_server.go Resolved
Comment thread derp/derp_server.go Resolved
Comment thread derp/derp_server.go Resolved
Comment thread derp/derp_server.go Resolved
@bradfitz
bradfitz force-pushed the bradfitz/derp_dup branch 2 times, most recently from 9ca1514 to 1d7b2cb Compare August 31, 2021 04:28
@bradfitz

Copy link
Copy Markdown
Member Author

PTAL

@bradfitz

Copy link
Copy Markdown
Member Author

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.

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?

@danderson

Copy link
Copy Markdown
Member

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.

@bradfitz bradfitz changed the title derp: accept dup clients but don't send fighting dups traffic derp: accept dup clients without closing prior's connection Aug 31, 2021
@bradfitz

Copy link
Copy Markdown
Member Author

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]>

@crawshaw crawshaw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread derp/derp_server.go
// 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always weird to see an exported method returning an unexported type. But it's fine I guess

Comment thread derp/derp_server.go
s.clients[c.key] = singleClient{c}
case singleClient:
s.dupClientKeys.Add(1)
s.dupClientConns.Add(2) // both old and new count

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@bradfitz
bradfitz merged commit 7328059 into main Aug 31, 2021
@bradfitz
bradfitz deleted the bradfitz/derp_dup branch August 31, 2021 15:21
spikecurtis added a commit to coder/tailscale that referenced this pull request May 31, 2024
bradfitz pushed a commit that referenced this pull request May 31, 2024
Updates #2745
Updates #7552

Signed-off-by: Spike Curtis <[email protected]>
chen8945 pushed a commit to Ckid-Home/tailscale that referenced this pull request Jul 31, 2024
Asutorufa pushed a commit to Asutorufa/tailscale that referenced this pull request Aug 23, 2024
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.

4 participants