[PR #6742] [MERGED] test: stop swapping http.DefaultTransport in the Happ network test #6437

Closed
opened 2026-11-02 04:08:46 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6742
Author: @AyaseEli-Bing
Created: 10/5/2026
Status: ✅ Merged
Merged: 10/8/2026
Merged by: @MHSanaei

Base: main ← Head: fix/race-public-ip-lookup-transport


📝 Commits (6)

  • 249a3b8 test: stop swapping http.DefaultTransport in the Happ network test
  • 735f283 test: prove the Happ network guard intercepts panel egress
  • 6b6b917 Merge branch 'main' into fix/race-public-ip-lookup-transport
  • 45a3ffe test(service): stop the cold-status test leaking a public-IP resolver
  • 147d4f1 Merge remote-tracking branch 'origin/main' into pr-6742
  • ecfbda2 Merge branch 'main' into fix/race-public-ip-lookup-transport

📊 Changes

1 file changed (+3 additions, -1 deletions)

View changed files

📝 internal/web/service/server_cold_status_test.go (+3 -1)

📄 Description

None filed — I checked first: no open issue mentions TestHappGenerateLocallyWithoutNetwork, and no open issue reports the race job flaking. Happy to file one if you would rather track it separately.

What changed

internal/web/service/happ_local_test.go no longer assigns http.DefaultTransport. The panel's own ad-hoc HTTP clients are now built through one swap point (internal/web/service/server.go: panelEgressTransport + panelEgressLookups, with panelEgressClient / applyPanelEgressTransport), which the test stubs atomically, waits to drain, and proves is live before asserting on it.

Why

ServerService.GetStatus starts resolvePublicIPsInBackground (server.go:507), a fire-and-forget goroutine that ends up in getPublicIP building &http.Client{Timeout: 3s} with a nil Transport — i.e. it reads the http.DefaultTransport variable at request time. TestHappGenerateLocallyWithoutNetwork wrote that same global to refuse sockets. Tests run sequentially inside a package, but the resolver goroutine does not: it outlives the test that scheduled it, so a later test writes the global while that goroutine is reading it.

That is the race CI reported on main after #6740 (run 37345781756, job race, 26 reports, every stack inside internal/web/service / net/http). It only surfaces when -shuffle=on happens to schedule a status-sampling test before the Happ one — which is why the three pushes before mine were green and this one was not. Nothing in #6740 is involved: internal/sub appears in none of the stacks.

The secondary effect is worth fixing in the same stroke: a leaked resolver that dials through the swapped transport also bumps the test's own calls counter, so the "zero network attempts" assertion was measuring other tests' traffic. Draining before installing the stub makes the tally mean what its name says.

Review follow-up (735f283)

The review correctly pointed out that stubbing only the public-IP lookup left the assertion vacuous: HappService.Generate never reaches that code, so the tally could not move, and a future outbound call from local generation would pass unnoticed. Two changes address it:

  • Wider seam. SettingService.NewProxiedHTTPClient — the helper the panel itself uses for its own egress (subscription fetches, warp, sponsor, Telegram) — now routes through the same swap point, so code added to generation later meets the override at the door rather than silently bypassing it.
  • Canary. Before exercising Generate, the test sends one request through the seam and requires both an error and that the dial tally moved. panel-egress-canary.invalid is reserved by RFC 6761, so the error alone proves nothing; the tally check is what turns a dead stub into a loud failure.

Negative control, run on purpose by disabling the Load() branch in panelEgressClient:

--- FAIL: TestHappGenerateLocallyWithoutNetwork (2.18s)
    happ_local_test.go:61: network tally did not move on a refused request: the stub is not intercepting panel egress

Verification

# Reproduced on the parent commit (e897b09), unfixed:
go test -race -count=1 -shuffle=6 -run 'TestCurrentStatusSamplesBeforeFirstTick|TestHappGenerateLocallyWithoutNetwork' ./internal/web/service/
#   WARNING: DATA RACE
#   Read at ... by goroutine 98:  service.getPublicIP() server.go:406
#   Previous write at ... by goroutine 104: TestHappGenerateLocallyWithoutNetwork() happ_local_test.go:25

On this branch:

  • Same seed, same two tests → ok 6.5s; the whole package under -shuffle=6 → ok 80.5s, and under -shuffle=on → ok 64.1s; 0 races.
  • golangci-lint run internal/web/service/... → 0 issues.
  • gofmt -l internal/web/service/ → empty
  • go vet ./internal/web/service/ ./internal/util/netproxy/ → clean
  • Canary negative control above fails for the right reason, then passes once restored.

Not run here: make node-e2e, the live-Postgres job, make vulncheck, and the frontend targets (no frontend file touched). CI is the real gate for those.

Impact

No behaviour change in production: panelEgressTransport is nil unless a test sets it, so panelEgressClient keeps Transport == nil and the http.Client default applies exactly as before, and applyPanelEgressTransport leaves clients that already carry a transport (a configured proxy) untouched. The added cost is two atomic operations per public-IP lookup, which runs at most once per address family per ServerService.

AI assistance

This patch was produced by an AI coding agent (Qoder) at the request of the account owner, who reviewed the diff and the reproduction above. Same author as #6740.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable). — the change is to a test; it now fails loudly (waitForPanelEgressLookups fatal) if a resolver never settles, and fails loudly (canary) if the stub is not intercepting.
  • go build ./... and the test suite pass locally.
  • Frontend lint/typecheck/build — N/A, no frontend files touched.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR. (3 files, +76/−10, all in internal/web/service.)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6742 **Author:** [@AyaseEli-Bing](https://github.com/AyaseEli-Bing) **Created:** 10/5/2026 **Status:** ✅ Merged **Merged:** 10/8/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/race-public-ip-lookup-transport` --- ### 📝 Commits (6) - [`249a3b8`](https://github.com/MHSanaei/3x-ui/commit/249a3b87c15d74c7d271dabc86e80b905b8829bc) test: stop swapping http.DefaultTransport in the Happ network test - [`735f283`](https://github.com/MHSanaei/3x-ui/commit/735f2834799907731695b441b790ca7d46bfa24e) test: prove the Happ network guard intercepts panel egress - [`6b6b917`](https://github.com/MHSanaei/3x-ui/commit/6b6b917fbf27ee568deb8cc16f6779c5760e5a6c) Merge branch 'main' into fix/race-public-ip-lookup-transport - [`45a3ffe`](https://github.com/MHSanaei/3x-ui/commit/45a3ffe55ce88b6398b77fac787173a270663116) test(service): stop the cold-status test leaking a public-IP resolver - [`147d4f1`](https://github.com/MHSanaei/3x-ui/commit/147d4f1b87441ea0d0116eeaae0fedcd03557551) Merge remote-tracking branch 'origin/main' into pr-6742 - [`ecfbda2`](https://github.com/MHSanaei/3x-ui/commit/ecfbda2b504195c02c21b913d19bcda69fdd09b3) Merge branch 'main' into fix/race-public-ip-lookup-transport ### 📊 Changes **1 file changed** (+3 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/server_cold_status_test.go` (+3 -1) </details> ### 📄 Description ## Related issue None filed — I checked first: no open issue mentions `TestHappGenerateLocallyWithoutNetwork`, and no open issue reports the `race` job flaking. Happy to file one if you would rather track it separately. ## What changed `internal/web/service/happ_local_test.go` no longer assigns `http.DefaultTransport`. The panel's own ad-hoc HTTP clients are now built through one swap point (`internal/web/service/server.go`: `panelEgressTransport` + `panelEgressLookups`, with `panelEgressClient` / `applyPanelEgressTransport`), which the test stubs atomically, waits to drain, and proves is live before asserting on it. ## Why `ServerService.GetStatus` starts `resolvePublicIPsInBackground` (`server.go:507`), a fire-and-forget goroutine that ends up in `getPublicIP` building `&http.Client{Timeout: 3s}` with a nil `Transport` — i.e. it reads the `http.DefaultTransport` variable at request time. `TestHappGenerateLocallyWithoutNetwork` wrote that same global to refuse sockets. Tests run sequentially inside a package, but the resolver goroutine does not: it outlives the test that scheduled it, so a later test writes the global while that goroutine is reading it. That is the race CI reported on `main` after #6740 (run `37345781756`, job `race`, 26 reports, every stack inside `internal/web/service` / `net/http`). It only surfaces when `-shuffle=on` happens to schedule a status-sampling test before the Happ one — which is why the three pushes before mine were green and this one was not. Nothing in #6740 is involved: `internal/sub` appears in none of the stacks. The secondary effect is worth fixing in the same stroke: a leaked resolver that dials through the swapped transport also bumps the test's own `calls` counter, so the "zero network attempts" assertion was measuring other tests' traffic. Draining before installing the stub makes the tally mean what its name says. ## Review follow-up (735f283) The review correctly pointed out that stubbing only the public-IP lookup left the assertion vacuous: `HappService.Generate` never reaches that code, so the tally could not move, and a future outbound call from local generation would pass unnoticed. Two changes address it: - **Wider seam.** `SettingService.NewProxiedHTTPClient` — the helper the panel itself uses for its own egress (subscription fetches, warp, sponsor, Telegram) — now routes through the same swap point, so code added to generation later meets the override at the door rather than silently bypassing it. - **Canary.** Before exercising `Generate`, the test sends one request through the seam and requires both an error *and* that the dial tally moved. `panel-egress-canary.invalid` is reserved by RFC 6761, so the error alone proves nothing; the tally check is what turns a dead stub into a loud failure. Negative control, run on purpose by disabling the `Load()` branch in `panelEgressClient`: ``` --- FAIL: TestHappGenerateLocallyWithoutNetwork (2.18s) happ_local_test.go:61: network tally did not move on a refused request: the stub is not intercepting panel egress ``` ## Verification ```sh # Reproduced on the parent commit (e897b09), unfixed: go test -race -count=1 -shuffle=6 -run 'TestCurrentStatusSamplesBeforeFirstTick|TestHappGenerateLocallyWithoutNetwork' ./internal/web/service/ # WARNING: DATA RACE # Read at ... by goroutine 98: service.getPublicIP() server.go:406 # Previous write at ... by goroutine 104: TestHappGenerateLocallyWithoutNetwork() happ_local_test.go:25 ``` On this branch: - Same seed, same two tests → `ok 6.5s`; the whole package under `-shuffle=6` → `ok 80.5s`, and under `-shuffle=on` → `ok 64.1s`; 0 races. - `golangci-lint run internal/web/service/...` → `0 issues.` - `gofmt -l internal/web/service/` → empty - `go vet ./internal/web/service/ ./internal/util/netproxy/` → clean - Canary negative control above fails for the right reason, then passes once restored. Not run here: `make node-e2e`, the live-Postgres job, `make vulncheck`, and the frontend targets (no frontend file touched). CI is the real gate for those. ## Impact No behaviour change in production: `panelEgressTransport` is nil unless a test sets it, so `panelEgressClient` keeps `Transport == nil` and the `http.Client` default applies exactly as before, and `applyPanelEgressTransport` leaves clients that already carry a transport (a configured proxy) untouched. The added cost is two atomic operations per public-IP lookup, which runs at most once per address family per `ServerService`. ## AI assistance This patch was produced by an AI coding agent (Qoder) at the request of the account owner, who reviewed the diff and the reproduction above. Same author as #6740. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). — the change *is* to a test; it now fails loudly (`waitForPanelEgressLookups` fatal) if a resolver never settles, and fails loudly (canary) if the stub is not intercepting. - [x] `go build ./...` and the test suite pass locally. - [ ] Frontend lint/typecheck/build — N/A, no frontend files touched. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. (3 files, +76/−10, all in `internal/web/service`.) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:08:46 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6437
No description provided.