[GH-ISSUE #6115] [Bug]: informTrafficToExternalAPI has no deadline/retry — a stalled receiver freezes traffic accounting indefinitely #4226

Closed
opened 2026-10-31 20:38:07 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @mrnickson-hue on GitHub (Jul 27, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6115

Describe the bug

Split out from #6114 (where a different, unrelated report was investigated and disproven) — this is a separate, confirmed finding from that review, worth tracking on its own.

informTrafficToExternalAPI() sends the ExternalTrafficInformURI POST via the package-level fasthttp.Do(request, response) — not DoTimeout/DoDeadline — so the call has no deadline at all. XrayTrafficJob.Run() is scheduled every 5s (cadenceXrayTraffic = "@every 5s", internal/web/web.go) and wrapped in cron.SkipIfStillRunning. If the configured receiver ever accepts the TCP connection and then stalls (no response, no close — a slow/misbehaving endpoint, not necessarily malicious), Run() blocks on that POST indefinitely. Because of SkipIfStillRunning, every subsequent 5s tick is skipped for as long as the hang lasts — local traffic accounting, online-client detection, and the WebSocket broadcasts all freeze along with it, not just the external POST.

The repo already has a working pattern for exactly this class of problem: createRobustFastHTTPClient (internal/web/service/tgbot/tgbot.go:366-396), used for the Telegram bot's HTTP calls — sets MaxIdleConnDuration: 60s and MaxIdemponentCallAttempts: 3 with a RetryIf that explicitly opts POST in. The traffic-inform webhook never received the same treatment.

How to reproduce the problem

  1. Enable Traffic Notify (ExternalTrafficInformEnable / ExternalTrafficInformURI) pointed at an endpoint you control.
  2. Make that endpoint accept the TCP/TLS connection but never write a response and never close (e.g. a listener that just reads the request and sleeps).
  3. Watch the panel: traffic stats, online status, and WebSocket-driven live updates all stop updating — not just for that one endpoint, for the whole panel — until the stalled connection times out at the OS/network level (which, with no fasthttp deadline set, could be a very long time).

Expected behavior

A slow or stalled ExternalTrafficInformURI receiver should not be able to block the panel's own traffic/online-status accounting. The outbound call should be bounded well under the 5s poll interval, and a stalled receiver should just mean that one tick's external notification is skipped — not that every subsequent tick stops running.

Actual behavior

XrayTrafficJob.Run() has no timeout protection against a stalled ExternalTrafficInformURI receiver; a hang there blocks the whole job (and, via SkipIfStillRunning, every following scheduled run) indefinitely.

Relevant logs

N/A — this is a code-review finding (confirmed by reading internal/web/job/xray_traffic_job.go and the cron wiring in internal/web/web.go), not something caught from a live incident yet.

3x-ui version

3.5.0 (confirmed present in the current main tree as well)

How did you install 3x-ui?

install.sh script

Operating system

Ubuntu 24.04.4 LTS / 26.04 LTS

Which parts of the panel are affected?

Statistics / traffic counters, Backend (API endpoints, login, settings)

Is the panel behind a reverse proxy or CDN?

No — direct access

Possible fix direction

  1. Give the webhook its own *fasthttp.Client, mirroring createRobustFastHTTPClient — sane MaxIdleConnDuration, MaxConnsPerHost, and a RetryIf that covers POST — or at minimum request.Header.SetConnectionClose() since reuse isn't needed for a call this infrequent.
  2. Replace fasthttp.Do with DoTimeout/DoDeadline, bounded comfortably under the 5s cadence, so a stalled receiver can't skip subsequent polls.
  3. Optionally skip the POST entirely when both traffics and clientTraffics are empty at the top of informTrafficToExternalAPI — cuts most of the idle-connection exposure and pointless network chatter outright.

Happy to open a PR for this (mirroring the tgbot client pattern) if a maintainer's fine with the direction.

Before submitting

  • I searched existing issues and this bug has not been reported.
  • I am running the latest released version of 3x-ui (v3.5.0).
  • This bug report is written in English.
  • I have redacted any sensitive data (tokens, real domains, IPs, client UUIDs).
Originally created by @mrnickson-hue on GitHub (Jul 27, 2026). Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6115 ### Describe the bug Split out from #6114 (where a different, unrelated report was investigated and disproven) — this is a separate, confirmed finding from that review, worth tracking on its own. `informTrafficToExternalAPI()` sends the `ExternalTrafficInformURI` POST via the package-level `fasthttp.Do(request, response)` — not `DoTimeout`/`DoDeadline` — so the call has **no deadline at all**. `XrayTrafficJob.Run()` is scheduled every 5s (`cadenceXrayTraffic = "@every 5s"`, `internal/web/web.go`) and wrapped in `cron.SkipIfStillRunning`. If the configured receiver ever accepts the TCP connection and then stalls (no response, no close — a slow/misbehaving endpoint, not necessarily malicious), `Run()` blocks on that POST indefinitely. Because of `SkipIfStillRunning`, every subsequent 5s tick is skipped for as long as the hang lasts — local traffic accounting, online-client detection, and the WebSocket broadcasts all freeze along with it, not just the external POST. The repo already has a working pattern for exactly this class of problem: `createRobustFastHTTPClient` (`internal/web/service/tgbot/tgbot.go:366-396`), used for the Telegram bot's HTTP calls — sets `MaxIdleConnDuration: 60s` and `MaxIdemponentCallAttempts: 3` with a `RetryIf` that explicitly opts `POST` in. The traffic-inform webhook never received the same treatment. ### How to reproduce the problem 1. Enable `Traffic Notify` (`ExternalTrafficInformEnable` / `ExternalTrafficInformURI`) pointed at an endpoint you control. 2. Make that endpoint accept the TCP/TLS connection but never write a response and never close (e.g. a listener that just reads the request and sleeps). 3. Watch the panel: traffic stats, online status, and WebSocket-driven live updates all stop updating — not just for that one endpoint, for the whole panel — until the stalled connection times out at the OS/network level (which, with no `fasthttp` deadline set, could be a very long time). ### Expected behavior A slow or stalled `ExternalTrafficInformURI` receiver should not be able to block the panel's own traffic/online-status accounting. The outbound call should be bounded well under the 5s poll interval, and a stalled receiver should just mean that one tick's external notification is skipped — not that every subsequent tick stops running. ### Actual behavior `XrayTrafficJob.Run()` has no timeout protection against a stalled `ExternalTrafficInformURI` receiver; a hang there blocks the whole job (and, via `SkipIfStillRunning`, every following scheduled run) indefinitely. ### Relevant logs N/A — this is a code-review finding (confirmed by reading `internal/web/job/xray_traffic_job.go` and the cron wiring in `internal/web/web.go`), not something caught from a live incident yet. ### 3x-ui version 3.5.0 (confirmed present in the current `main` tree as well) ### How did you install 3x-ui? install.sh script ### Operating system Ubuntu 24.04.4 LTS / 26.04 LTS ### Which parts of the panel are affected? Statistics / traffic counters, Backend (API endpoints, login, settings) ### Is the panel behind a reverse proxy or CDN? No — direct access ### Possible fix direction 1. Give the webhook its own `*fasthttp.Client`, mirroring `createRobustFastHTTPClient` — sane `MaxIdleConnDuration`, `MaxConnsPerHost`, and a `RetryIf` that covers `POST` — or at minimum `request.Header.SetConnectionClose()` since reuse isn't needed for a call this infrequent. 2. Replace `fasthttp.Do` with `DoTimeout`/`DoDeadline`, bounded comfortably under the 5s cadence, so a stalled receiver can't skip subsequent polls. 3. Optionally skip the POST entirely when both `traffics` and `clientTraffics` are empty at the top of `informTrafficToExternalAPI` — cuts most of the idle-connection exposure and pointless network chatter outright. Happy to open a PR for this (mirroring the `tgbot` client pattern) if a maintainer's fine with the direction. ### Before submitting - [x] I searched existing issues and this bug has not been reported. - [x] I am running the latest released version of 3x-ui (v3.5.0). - [x] This bug report is written in English. - [x] I have redacted any sensitive data (tokens, real domains, IPs, client UUIDs).
bsakaguchi 2026-10-31 20:38:07 +07:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@github-actions[bot] commented on GitHub (Jul 27, 2026):

Confirmed. The call site has no deadline, and the blocking relationship with cron.SkipIfStillRunning is real. Two corrections to the blast radius, and one caveat on the retry half of your proposal.

Title — informTrafficToExternalAPI POSTs via the package-level fasthttp.Do with no read/write deadline, so a stalled receiver blocks XrayTrafficJob.Run() and every subsequent 5s tick

Severity — Medium

Category — Reliability

Confirmed in the source (current main, v3.5.0):

  • internal/web/job/xray_traffic_job.go:223 — fasthttp.Do(request, response). Not DoTimeout/DoDeadline, and no *fasthttp.Client with ReadTimeout/WriteTimeout is constructed anywhere on this path, so nothing in this code bounds how long the response wait can take. It is the only fasthttp.Do call in the tree.
  • internal/web/web.go:289 and :322 — cadenceXrayTraffic = "@every 5s", registered after a 5s startup delay; :494-501 wraps every entry in cron.SkipIfStillRunning. The comment at :489-493 states the intent explicitly: keep a slow traffic poll from overlapping itself.
  • The POST is invoked at xray_traffic_job.go:68-69, which sits before RefreshLocalOnlineClients (:121), BroadcastTraffic (:170), BroadcastClientStats (:189) and BroadcastOutbounds (:193). So a stall freezes the in-flight tick's online-status and WebSocket work too, not only the ticks that get skipped afterwards.
  • internal/web/service/tgbot/tgbot.go:366-396 is as you describe: ReadTimeout/WriteTimeout 30s at :369-370, MaxIdleConnDuration 60s, MaxIdemponentCallAttempts: 3, and RetryIf at :383 opting POST in. The webhook path never got the same treatment.

Two scope corrections:

  1. What freezes is narrower than "the whole panel", and worse than statistics. The cron chain wraps each entry individually, so the IP-limit scan, node heartbeat, node traffic sync and periodic resets keep running, and the HTTP server, login and settings are unaffected. But AddTraffic (:47) is not just counters — internal/web/service/inbound_traffic.go:56 runs autoRenewClients and :64 runs disableInvalidClients inside the same call. While the job is stuck, quota/expiry enforcement and auto-renew stop with it, so an over-quota client keeps transiting for the whole duration of the hang.
  2. No traffic is lost, only delayed. internal/xray/api.go:679 queries with Reset_: false and derives deltas against StatsLastValues (:689-695), so whenever the job runs again the accumulated delta lands in one poll. The counters catch up; the enforcement and online-status decisions taken during the gap do not.

On the retry half of your fix direction: mirroring RetryIf for POST is not as clearly safe here as on the Telegram path. This payload carries per-tick deltas, so an attempt that actually reached the receiver but failed on the response leg would be counted twice on the receiver if retried. The deadline is the unambiguous half of the fix; retry semantics for a delta webhook are worth deciding deliberately.

Recommendation, for @MHSanaei to decide:

  1. Bound the call — a dedicated *fasthttp.Client with ReadTimeout/WriteTimeout, or DoTimeout/DoDeadline, sized comfortably under the 5s cadence. Note that SanitizePublicHTTPURL(informURL, false) at :205 already spends up to 5s of its own on DNS for a hostname URI (internal/web/service/url_safety.go:56 and :71), so any budget should cover both steps, not just the POST.
  2. request.Header.SetConnectionClose() is cheap at one call per 5s and removes the idle-pool exposure without needing pool tuning.
  3. Skipping the POST when both slices are empty is a straightforward reduction in exposure and chatter.
  4. If the goal is that a slow receiver can never gate local accounting, the stronger shape is moving the notification off the job goroutine entirely — accounting completes regardless of what the receiver does.
internal/web/job/xray_traffic_job.go
  func (j *XrayTrafficJob) informTrafficToExternalAPI
  line 223:  if err := fasthttp.Do(request, response); err != nil {
             package-level default client; no ReadTimeout/WriteTimeout, no DoTimeout/DoDeadline
  called synchronously from Run() at line 69, ahead of lines 121/170/189/193

Workaround until this is addressed: turn off externalTrafficInformEnable in Panel Settings → General (frontend/src/pages/settings/GeneralTab.tsx:265), or make the receiver acknowledge immediately and process asynchronously so it never holds the connection open.

gh search commits shows no change to this function, and the call is unchanged in v3.5.0 (released 2026-07-12) and in current main. Confidence is high on the mechanism; the one thing I could not verify offline is whether fasthttp's own default dialer bounds the connect phase, which does not affect your scenario since the receiver accepts before stalling. Recorded as related to #6114, not a duplicate — that report's stated defect was the stale-pooled-connection hypothesis, which you closed on your own evidence.

This reply was generated automatically; a maintainer may follow up.

<!-- gh-comment-id:5086318235 --> @github-actions[bot] commented on GitHub (Jul 27, 2026): Confirmed. The call site has no deadline, and the blocking relationship with `cron.SkipIfStillRunning` is real. Two corrections to the blast radius, and one caveat on the retry half of your proposal. Title — `informTrafficToExternalAPI` POSTs via the package-level `fasthttp.Do` with no read/write deadline, so a stalled receiver blocks `XrayTrafficJob.Run()` and every subsequent 5s tick Severity — Medium Category — Reliability Confirmed in the source (current `main`, v3.5.0): - `internal/web/job/xray_traffic_job.go:223` — `fasthttp.Do(request, response)`. Not `DoTimeout`/`DoDeadline`, and no `*fasthttp.Client` with `ReadTimeout`/`WriteTimeout` is constructed anywhere on this path, so nothing in this code bounds how long the response wait can take. It is the only `fasthttp.Do` call in the tree. - `internal/web/web.go:289` and `:322` — `cadenceXrayTraffic = "@every 5s"`, registered after a 5s startup delay; `:494-501` wraps every entry in `cron.SkipIfStillRunning`. The comment at `:489-493` states the intent explicitly: keep a slow traffic poll from overlapping itself. - The POST is invoked at `xray_traffic_job.go:68-69`, which sits **before** `RefreshLocalOnlineClients` (`:121`), `BroadcastTraffic` (`:170`), `BroadcastClientStats` (`:189`) and `BroadcastOutbounds` (`:193`). So a stall freezes the in-flight tick's online-status and WebSocket work too, not only the ticks that get skipped afterwards. - `internal/web/service/tgbot/tgbot.go:366-396` is as you describe: `ReadTimeout`/`WriteTimeout` 30s at `:369-370`, `MaxIdleConnDuration` 60s, `MaxIdemponentCallAttempts: 3`, and `RetryIf` at `:383` opting `POST` in. The webhook path never got the same treatment. Two scope corrections: 1. **What freezes is narrower than "the whole panel", and worse than statistics.** The cron chain wraps each entry individually, so the IP-limit scan, node heartbeat, node traffic sync and periodic resets keep running, and the HTTP server, login and settings are unaffected. But `AddTraffic` (`:47`) is not just counters — `internal/web/service/inbound_traffic.go:56` runs `autoRenewClients` and `:64` runs `disableInvalidClients` inside the same call. While the job is stuck, quota/expiry enforcement and auto-renew stop with it, so an over-quota client keeps transiting for the whole duration of the hang. 2. **No traffic is lost, only delayed.** `internal/xray/api.go:679` queries with `Reset_: false` and derives deltas against `StatsLastValues` (`:689-695`), so whenever the job runs again the accumulated delta lands in one poll. The counters catch up; the enforcement and online-status decisions taken during the gap do not. On the retry half of your fix direction: mirroring `RetryIf` for POST is not as clearly safe here as on the Telegram path. This payload carries per-tick deltas, so an attempt that actually reached the receiver but failed on the response leg would be counted twice on the receiver if retried. The deadline is the unambiguous half of the fix; retry semantics for a delta webhook are worth deciding deliberately. Recommendation, for @MHSanaei to decide: 1. Bound the call — a dedicated `*fasthttp.Client` with `ReadTimeout`/`WriteTimeout`, or `DoTimeout`/`DoDeadline`, sized comfortably under the 5s cadence. Note that `SanitizePublicHTTPURL(informURL, false)` at `:205` already spends up to 5s of its own on DNS for a hostname URI (`internal/web/service/url_safety.go:56` and `:71`), so any budget should cover both steps, not just the POST. 2. `request.Header.SetConnectionClose()` is cheap at one call per 5s and removes the idle-pool exposure without needing pool tuning. 3. Skipping the POST when both slices are empty is a straightforward reduction in exposure and chatter. 4. If the goal is that a slow receiver can never gate local accounting, the stronger shape is moving the notification off the job goroutine entirely — accounting completes regardless of what the receiver does. ``` internal/web/job/xray_traffic_job.go func (j *XrayTrafficJob) informTrafficToExternalAPI line 223: if err := fasthttp.Do(request, response); err != nil { package-level default client; no ReadTimeout/WriteTimeout, no DoTimeout/DoDeadline called synchronously from Run() at line 69, ahead of lines 121/170/189/193 ``` Workaround until this is addressed: turn off `externalTrafficInformEnable` in Panel Settings → General (`frontend/src/pages/settings/GeneralTab.tsx:265`), or make the receiver acknowledge immediately and process asynchronously so it never holds the connection open. `gh search commits` shows no change to this function, and the call is unchanged in v3.5.0 (released 2026-07-12) and in current `main`. Confidence is high on the mechanism; the one thing I could not verify offline is whether fasthttp's own default dialer bounds the connect phase, which does not affect your scenario since the receiver accepts before stalling. Recorded as related to #6114, not a duplicate — that report's stated defect was the stale-pooled-connection hypothesis, which you closed on your own evidence. *This reply was generated automatically; a maintainer may follow up.*
Author
Owner

@MHSanaei commented on GitHub (Jul 27, 2026):

Fixed in 0e69f64e. The analysis was accurate, including the SkipIfStillRunning consequence.

Confirmed the premise directly rather than by reading: against a listener that accepts the connection and then never answers and never closes, the package-level fasthttp.Do was still blocked after 8 seconds. The new client returns at its 3s budget. Both are pinned by tests now.

What landed, close to your proposed direction:

  • A dedicated *fasthttp.Client with ReadTimeout/WriteTimeout, and DoTimeout with a 3s budget — comfortably under the 5s cadence.
  • SetConnectionClose(), since pooling buys nothing at one call per 5s and it removes the idle-connection exposure.
  • The POST is skipped when both slices are empty.

One deliberate omission: no RetryIf. As the review on this issue noted, the Telegram path can retry safely but this payload carries per-tick deltas, so an attempt that reached the receiver and failed on the response leg would be double-counted there. The deadline is the unambiguous half; retry semantics for a delta webhook are not.

I also left the notification on the job goroutine rather than moving it off. With the call bounded at 3s the worst case is one delayed tick, and moving it off-goroutine changes delivery ordering for anyone parsing these deltas in sequence. Worth revisiting if it turns out 3s is still too much on a busy panel.

Thanks for the clean report — the reproduction steps made it straightforward to build a test that actually exercises the stall.

<!-- gh-comment-id:5091400840 --> @MHSanaei commented on GitHub (Jul 27, 2026): Fixed in 0e69f64e. The analysis was accurate, including the `SkipIfStillRunning` consequence. Confirmed the premise directly rather than by reading: against a listener that accepts the connection and then never answers and never closes, the package-level `fasthttp.Do` was still blocked after 8 seconds. The new client returns at its 3s budget. Both are pinned by tests now. What landed, close to your proposed direction: - A dedicated `*fasthttp.Client` with `ReadTimeout`/`WriteTimeout`, and `DoTimeout` with a 3s budget — comfortably under the 5s cadence. - `SetConnectionClose()`, since pooling buys nothing at one call per 5s and it removes the idle-connection exposure. - The POST is skipped when both slices are empty. One deliberate omission: no `RetryIf`. As the review on this issue noted, the Telegram path can retry safely but this payload carries per-tick deltas, so an attempt that reached the receiver and failed on the response leg would be double-counted there. The deadline is the unambiguous half; retry semantics for a delta webhook are not. I also left the notification on the job goroutine rather than moving it off. With the call bounded at 3s the worst case is one delayed tick, and moving it off-goroutine changes delivery ordering for anyone parsing these deltas in sequence. Worth revisiting if it turns out 3s is still too much on a busy panel. Thanks for the clean report — the reproduction steps made it straightforward to build a test that actually exercises the stall.
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#4226
No description provided.