mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[GH-ISSUE #6115] [Bug]: informTrafficToExternalAPI has no deadline/retry — a stalled receiver freezes traffic accounting indefinitely #4226
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#4226
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 theExternalTrafficInformURIPOST via the package-levelfasthttp.Do(request, response)— notDoTimeout/DoDeadline— so the call has no deadline at all.XrayTrafficJob.Run()is scheduled every 5s (cadenceXrayTraffic = "@every 5s",internal/web/web.go) and wrapped incron.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 ofSkipIfStillRunning, 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 — setsMaxIdleConnDuration: 60sandMaxIdemponentCallAttempts: 3with aRetryIfthat explicitly optsPOSTin. The traffic-inform webhook never received the same treatment.How to reproduce the problem
Traffic Notify(ExternalTrafficInformEnable/ExternalTrafficInformURI) pointed at an endpoint you control.fasthttpdeadline set, could be a very long time).Expected behavior
A slow or stalled
ExternalTrafficInformURIreceiver 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 stalledExternalTrafficInformURIreceiver; a hang there blocks the whole job (and, viaSkipIfStillRunning, every following scheduled run) indefinitely.Relevant logs
N/A — this is a code-review finding (confirmed by reading
internal/web/job/xray_traffic_job.goand the cron wiring ininternal/web/web.go), not something caught from a live incident yet.3x-ui version
3.5.0 (confirmed present in the current
maintree 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
*fasthttp.Client, mirroringcreateRobustFastHTTPClient— saneMaxIdleConnDuration,MaxConnsPerHost, and aRetryIfthat coversPOST— or at minimumrequest.Header.SetConnectionClose()since reuse isn't needed for a call this infrequent.fasthttp.DowithDoTimeout/DoDeadline, bounded comfortably under the 5s cadence, so a stalled receiver can't skip subsequent polls.trafficsandclientTrafficsare empty at the top ofinformTrafficToExternalAPI— cuts most of the idle-connection exposure and pointless network chatter outright.Happy to open a PR for this (mirroring the
tgbotclient pattern) if a maintainer's fine with the direction.Before submitting
@github-actions[bot] commented on GitHub (Jul 27, 2026):
Confirmed. The call site has no deadline, and the blocking relationship with
cron.SkipIfStillRunningis real. Two corrections to the blast radius, and one caveat on the retry half of your proposal.Title —
informTrafficToExternalAPIPOSTs via the package-levelfasthttp.Dowith no read/write deadline, so a stalled receiver blocksXrayTrafficJob.Run()and every subsequent 5s tickSeverity — Medium
Category — Reliability
Confirmed in the source (current
main, v3.5.0):internal/web/job/xray_traffic_job.go:223—fasthttp.Do(request, response). NotDoTimeout/DoDeadline, and no*fasthttp.ClientwithReadTimeout/WriteTimeoutis constructed anywhere on this path, so nothing in this code bounds how long the response wait can take. It is the onlyfasthttp.Docall in the tree.internal/web/web.go:289and:322—cadenceXrayTraffic = "@every 5s", registered after a 5s startup delay;:494-501wraps every entry incron.SkipIfStillRunning. The comment at:489-493states the intent explicitly: keep a slow traffic poll from overlapping itself.xray_traffic_job.go:68-69, which sits beforeRefreshLocalOnlineClients(:121),BroadcastTraffic(:170),BroadcastClientStats(:189) andBroadcastOutbounds(: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-396is as you describe:ReadTimeout/WriteTimeout30s at:369-370,MaxIdleConnDuration60s,MaxIdemponentCallAttempts: 3, andRetryIfat:383optingPOSTin. The webhook path never got the same treatment.Two scope corrections:
AddTraffic(:47) is not just counters —internal/web/service/inbound_traffic.go:56runsautoRenewClientsand:64runsdisableInvalidClientsinside 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.internal/xray/api.go:679queries withReset_: falseand derives deltas againstStatsLastValues(: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
RetryIffor 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:
*fasthttp.ClientwithReadTimeout/WriteTimeout, orDoTimeout/DoDeadline, sized comfortably under the 5s cadence. Note thatSanitizePublicHTTPURL(informURL, false)at:205already spends up to 5s of its own on DNS for a hostname URI (internal/web/service/url_safety.go:56and:71), so any budget should cover both steps, not just the POST.request.Header.SetConnectionClose()is cheap at one call per 5s and removes the idle-pool exposure without needing pool tuning.Workaround until this is addressed: turn off
externalTrafficInformEnablein 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 commitsshows no change to this function, and the call is unchanged in v3.5.0 (released 2026-07-12) and in currentmain. 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.
@MHSanaei commented on GitHub (Jul 27, 2026):
Fixed in
0e69f64e. The analysis was accurate, including theSkipIfStillRunningconsequence.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.Dowas 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:
*fasthttp.ClientwithReadTimeout/WriteTimeout, andDoTimeoutwith 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.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.