## Summary
Fixes a flaky `TestServerTailnet_ReverseProxy_TCPDialFailure` (~2% CI failure rate) caused by a port race.
Previously, `testutil.RandomPort` selected a port by briefly opening and closing a listener. Another parallel test could claim that port before the connection attempt, causing an unexpected success instead of a connection refusal.
This PR introduces `closedPort`, which selects an unused port from **20000–29999**, outside the default ephemeral port ranges on Linux, macOS, and Windows. It checks both `127.0.0.1` and `::1`, since the agent may attempt either address.
Refs [CORE-756](https://linear.app/codercom/issue/CORE-756/flake-testservertailnet-reverseproxy-tcpdialfailure)
## Changes
- Add `closedPort` in `coderd/tailnet_test.go` and replace the `testutil.RandomPort` call.
- Skip ports already accepting connections on IPv4 or IPv6.
- Keep existing assertions unchanged. No production code changes.
**Limitation:** This mitigates rather than eliminates the race. Another process could explicitly bind the selected port after the check, although the OS will not assign it automatically as an ephemeral port.
## Testing
| Scenario | Result |
|---|---|
| Before fix: 9,600 parallel Linux runs | 1 failure |
| After fix: 9,600 parallel Linux runs | **0 failures** |
| Ports 20000 and 20001 occupied | Pass; selects 20002 |
| Remove expected dial log fields | Test fails as expected |
| macOS: 20 consecutive runs | Pass |
| `golangci-lint` | 0 issues |
Windows remains untested.
## Expected Behavior
The test continues to verify that a failed TCP dial returns HTTP 502 and logs `agent_id`, `dial_phase=tcp_dial`, and `tcp_dial_after`, while reducing failures caused by port collisions.
Add default-level metrics and warn-level diagnostics to investigate
ServerTailnet reachability failures causing agent-specific 502s, without
restarting coderd or enabling debug logging.
- Enrich request logs and PTY/chatd warnings with agent ID, failure
reason, wait duration, coordinator state, WireGuard handshake and
byte counts, and DERP regions.
- Add metrics for unreachable attempts by reason, reachability latency,
embedded DERP connections, and offline app/terminal requests.
- Distinguish reachability failures from subsequent TCP dial failures,
cap reverse proxy dials at one minute, and exclude expected chatd
readiness polls and superseded-agent dials from unreachable counts.
- Report missing handshakes as zero time instead of 1970, also fixing
`coder ping`.
Part of PLAT-680, a sub-issue of PLAT-360.
Use typed atomics (atomic.Int64, atomic.Int32, etc.) in test files to prevent
mixing atomic and non-atomic access on the same value, guarantee 64-bit
alignment on 32-bit platforms, and provide a cleaner API.
Refactors our use of `slogtest` to instantiate a "standard logger" across most of our tests. This standard logger incorporates https://github.com/coder/slog/pull/217 to also ignore database query canceled errors by default, which are a source of low-severity flakes.
Any test that has set non-default `slogtest.Options` is left alone. In particular, `coderdtest` defaults to ignoring all errors. We might consider revisiting that decision now that we have better tools to target the really common flaky Error logs on shutdown.
chore of #14729
Refactors the `ServerTailnet` to use `tailnet.Controller` so that we reuse logic around reconnection and handling control messages, instead of reimplementing. This unifies our "client" use of the tailscale API across CLI, coderd, and wsproxy.
re: #14715
This PR introduces the Coder service prefix: `fd60:627a:a42b::/48` and refactors our existing code as calling the Tailscale service prefix explicitly (rather than implicitly).
Removes the unused `Addresses` agent option. All clients today assume they can compute the Agent's IP address based on its UUID, so an agent started with a custom address would break things.
Currently, importing `codersdk` just to interact with the API requires
importing tailscale, which causes builds to fail unless manually using
our fork.
I noticed a possible race where tailnet.Conn can try to dial the embedded region before we've set our custom dialer that send the DERP in-memory. This closes that race and adds a test case for servertailnet with no STUN and an embedded relay
Fixes#8218
Removes `wsconncache` and related "is legacy?" functions and API calls that were used by it.
The only leftover is that Agents still use the legacy IP, so that back level clients or workspace proxies can dial them correctly.
We should eventually remove this: #11819
This one is huge, and I'm sorry.
The problem is that once I change `tailnet.Conn` to start doing v2 behavior, I kind of have to change it everywhere, including in CoderSDK (CLI), the agent, wsproxy, and ServerTailnet.
There is still a bit more cleanup to do, and I need to add code so that when we lose connection to the Coordinator, we mark all peers as LOST, but that will be in a separate PR since this is big enough!
* chore: add /v2 to import module path
go mod requires semantic versioning with versions greater than 1.x
This was a mechanical update by running:
```
go install github.com/marwan-at-work/mod/cmd/mod@latest
mod upgrade
```
Migrate generated files to import /v2
* Fix gen