fix(p2p): reclaim connTracker lastConnect entries once their window elapses - #3918
fix(p2p): reclaim connTracker lastConnect entries once their window elapses#3918bdchatham wants to merge 1 commit into
Conversation
…lapses RemoveConn drops an address's lastConnect entry only when the connection outlived the window, which is correct: inside the window a reconnect still has to be refused, so the entry is still live state. Nothing revisited it afterwards though, so every address whose connection died inside the window kept an entry for the life of the process. On a public listener that set is unbounded, and failed handshakes, port scans and protocol probes all land in it. Sweep expired entries, at most once per window, from AddConn and RemoveConn. An entry is only consulted while it is inside the window, so anything older is dead weight, and RemoveConn already handles the long-lived case, leaving exactly the short-lived addresses for the sweep. The map is now bounded by the addresses seen within one window rather than by every address ever seen. 100k short-lived connections leave under 10k entries instead of 100k, and the reconnect window is unchanged. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
PR SummaryMedium Risk Overview Adds New tests cover that 100k distinct short-lived connections do not retain every address in Reviewed by Cursor Bugbot for commit 63b0fdf. Bugbot is set up for automated code reviews on this repo. Configure here. |
|
The latest Buf updates on your PR. Results from workflow Buf / buf (pull_request).
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3918 +/- ##
==========================================
- Coverage 59.48% 58.47% -1.02%
==========================================
Files 2325 2229 -96
Lines 198647 188024 -10623
==========================================
- Hits 118160 109938 -8222
+ Misses 69258 67699 -1559
+ Partials 11229 10387 -842
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
The sweep is correct — it only drops entries older than the window, which are exactly the entries AddConn can no longer refuse on, so the rate limit is unchanged and the lastConnect map is now bounded by roughly one window's arrivals. Both new tests, however, leave the sweep's window check unexercised and the accumulation bound timing-dependent; no blocking issues.
Findings: 0 blocking | 3 non-blocking | 2 posted inline
Blockers
- None at the file/PR level.
Non-blocking
- No existing or new test covers the
now.Sub(last) > rat.windowcondition insidesweepLocked: replacing that loop body with an unconditionaldeletefor every entry keepsTestConnTrackerSweepPreservesWindow,TestConnTrackerShortLivedConnsDoNotAccumulate,TestConnTracker/WindowandTestConnTracker/VeryShortall green. See the inline comments for the two cases that would close this. - 2 suggestion(s)/nit(s) flagged inline on specific lines.
|
|
||
| // Reclaiming entries must not let an address reconnect inside its window. | ||
| func TestConnTrackerSweepPreservesWindow(t *testing.T) { | ||
| ct := newConnTracker(10, time.Hour) |
There was a problem hiding this comment.
[suggestion] With window = time.Hour this test never runs the sweep against a populated map, so it does not guard what its comment claims. Trace it: the first AddConn sweeps an empty map and sets nextSweep = now + 1h; the following RemoveConn and AddConn both hit now.Before(rat.nextSweep) and return early. Replacing the loop body in sweepLocked with an unconditional delete(rat.lastConnect, address) still leaves this test passing (as it does TestConnTracker/Window and /VeryShort), so the now.Sub(last) > rat.window guard is currently untested.
The case that exercises it needs an entry created after the last sweep but still inside its window when the next sweep fires — e.g. with a short window, prime nextSweep with a throwaway address, sleep until just before it elapses, AddConn/RemoveConn the address under test, then sleep past nextSweep and make one more call so the sweep runs while that entry is only a fraction of a window old, and assert the reconnect is still refused.
|
|
||
| // Bounded by the addresses seen within one window rather than by every address | ||
| // seen. The margin is wide because the sweep is driven by elapsed time. | ||
| require.Less(t, len(ct.lastConnect), conns/10) |
There was a problem hiding this comment.
[suggestion] This bound is a function of loop throughput rather than of the sweep: the surviving entries are those added since the last sweep, i.e. roughly window / per-iteration cost. At an estimated ~0.5–1µs per iteration that lands around 1–3k, comfortably under 10k, and -race in CI only widens the margin — but the relationship is inverted from what you want (a faster machine retains more), so the headroom shrinks precisely where the test is cheapest to run.
You can make it deterministic and much stronger at the same time by forcing the final sweep instead of sampling mid-stream: after the loop, time.Sleep(2 * time.Millisecond) then AddConn/RemoveConn one fresh address. That call sweeps (nextSweep has certainly elapsed) and every loop entry is now older than the window, so require.Len(t, ct.lastConnect, 1) holds exactly, and it still fails without the sweep.
Problem
connTrackerkeeps two maps.cachecounts an address's live connections and is cleaned up when it reaches zero.lastConnectrecords when an address last connected, and is consulted only whencache[addr] == 0, to refuse a reconnect insideIncomingConnectionWindow(100ms by default).RemoveConndrops thelastConnectentry only when the connection outlived the window:That condition is correct. Inside the window a reconnect still has to be refused, so the entry is still live state and deleting it would weaken the limit.
The bug is that nothing revisits it afterwards. An address whose connection died inside the window keeps its entry for the life of the process. On a public listener the set of such addresses is unbounded, and it is fed by exactly the traffic a public listener sees most: failed handshakes, port scans, protocol probes, connections dropped mid-negotiation.
Change
Sweep expired entries, at most once per window, from both
AddConnandRemoveConn:An entry is only consulted while it is inside the window, so anything older is dead weight.
RemoveConnalready handles connections that outlived the window, which leaves exactly the short-lived addresses for the sweep to reclaim.The map is now bounded by the addresses seen within one window rather than by every address ever seen. Sweep cost is amortised: at most one pass per window, over a map that the sweep itself keeps small.
Tests
TestConnTrackerShortLivedConnsDoNotAccumulateopens and immediately closes 100k connections from distinct addresses and asserts the map does not retain them. Without the sweep it fails with"100000" is not less than "10000"— every address retained. With it, the map stays small.TestConnTrackerSweepPreservesWindowguards the other direction: reclaiming entries must not let an address reconnect inside its window. That one passes before and after, which is the point.The bound is asserted loosely (
conns/10) because the sweep is driven by elapsed time rather than by call count.go test ./internal/p2p/,gofmt,goimportsandgolangci-lint(v2.8.0) are clean.