mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 19:11:53 +07:00
[GH-ISSUE #6368] [Bug]: Updating an AmneziaWG inbound hangs the API request and leaves the device stopped #4321
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#4321
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 @IvanSavoskin on GitHub (Aug 30, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6368
Describe the bug
POST /panel/api/inbounds/update/:idon anamneziawginbound that has active clients doesnot return, and afterwards the inbound's UDP port is no longer bound. The panel keeps running
and serving the web UI, and the new value is in fact persisted — only the embedded device
stays down.
systemctl restart x-uibrings the listener back with the new settings.Reproduced several times on an inbound with 11 peers while changing a single field inside
settings.server.How to reproduce the problem
protocol: amneziawgand add ~10 clients.GET /panel/api/inbounds/get/:idand take the returned object as-is.settings.server(mtuin our case,h1-h4behavesthe same) and send the whole object back to
POST /panel/api/inbounds/update/:id.ss -lunp | grep <port>— nothing is listening any more.GET /panel/api/inbounds/get/:id— the new value is stored.systemctl restart x-ui— the port is bound again and the new value is in effect.Expected behavior
The update either applies and the embedded device is restarted with the new settings, or it
fails with an error response and the device keeps running with the old ones. In both cases
the request should return.
Actual behavior
The request hangs, the embedded interface stays down, the port is unbound, and nothing is
written to the log. The setting is saved, so the panel and the running device disagree until
the service is restarted manually. For an inbound with live users this is an outage that
gives no signal anywhere.
Relevant logs
Screenshots
No response
3x-ui version
3.7.0
Xray-core version
26.7.28
How did you install 3x-ui?
install.sh script
Operating system
Ubuntu 24.04 LTS, x86_64
Which parts of the panel are affected?
Backend (API endpoints, login, settings)
Browser (only if it is a UI bug)
No response
Is the panel behind a reverse proxy or CDN?
None
Before submitting
@github-actions[bot] commented on GitHub (Aug 30, 2026):
This is a real bug, and it is a lock inversion inside the embedded AmneziaWG manager:
Manager.Ensureholds its own mutex across the device teardown, while the packet-delivery path re-enters that same mutex — so one UDP packet in flight at the moment of the update wedges both goroutines permanently. Every symptom you listed (hang, unbound port, value persisted, nothing logged, restart required) falls out of that single line.The chain
1. The API saves first, then applies to the runtime synchronously in the request goroutine.
internal/web/service/inbound.go:1716commits the row (tx.Save(oldInbound)), and only afterwards, atinternal/web/service/inbound.go:1742:That is exactly why step 6 of your repro finds the new value stored while the request never returns.
2.
postCommitApply→rt.UpdateInbound→internal/web/runtime/local.go:196:amneziawgnet.GetManager().Ensure(...).3.
Ensureholds the manager mutex for the whole apply —internal/amneziawgnet/manager.go:86:4.
mtuis part of the rebuild fingerprint —internal/amneziawgnet/manager.go:258:So an
mtuedit takes the rebuild branch and callscur.dev.Close()atinternal/amneziawgnet/manager.go:165while still holdingm.mu.5. The UDP handler runs inline on the packet-delivery goroutine and re-enters the manager. Unlike the TCP forwarder, which dispatches each connection onto its own goroutine (
internal/amneziawgnet/forwarder.go:27,go func(r *tcp.ForwarderRequest)), the UDP handler is called synchronously —internal/amneziawgnet/udp.go:34-40. And the closureensureLockedattaches,internal/amneziawgnet/manager.go:199:Lookuptakes the same mutex —internal/amneziawgnet/manager.go:335-337.6. That delivery goroutine is a peer's receive goroutine, which the repo documents itself at
internal/amneziawgnet/device.go:113: "WARNING: DATA RACE between stack.(*nic).DeliverTransportPacket (the peer's receive goroutine, reading the handler table)". Closing an amneziawg-go device stops every peer, which means joining exactly those receive goroutines.Request goroutine holds
m.muand waits for the peer receive goroutine; the peer receive goroutine is parked inLookupwaiting form.mu. Neither ever completes.Why each of your observations matches
manager.go:165(cur.dev.Close()) and the new device'sUp(), and rules out the later steps (applyV6Aliases,portForwards.Reconcile), which run after the new device is already listening.logger.Infof("amneziawgnet: started embedded interface ...")isinternal/amneziawgnet/manager.go:239, past the block, and the device's own logger is silent unlessAMNEZIAWGNET_DEBUGis set (internal/amneziawgnet/manager.go:27-32).AmneziaWGJob(internal/web/job/amneziawg_job.go:54→Reconcile) blocks on the same mutex, so no AmneziaWG interface can ever be (re)started in that process again.h1–h4behaves the same — those are not in the fingerprint, so they take the in-place branch,internal/amneziawgnet/manager.go:151(cur.dev.IpcSet(conf)), still underm.mu. Per the code's own note atinternal/amneziawgnet/manager.go:96: "amneziawg-go's IpcSet always includes replace_peers=true ... and its own implementation of that op is device.RemoveAllPeers(), unconditionally" — which also has to stop every peer. Same deadlock through a different door. One useful discriminator: in theh1–h4case the UDP port should stay bound (no bind close on that path). If you saw the port survive there, that confirms theIpcSetvariant.Why it needs active clients: at least one UDP packet must be in flight through the tunnel at the instant of the apply. TCP alone cannot trigger it. With 11 live peers, DNS/QUIC/keepalive traffic makes that near-certain, which is why it reproduces for you every time and not on an idle inbound.
What you can do now
The edit is already persisted before the hang, so
systemctl restart x-uiapplies it correctly — that is the recovery, and there is no way to avoid it once the request has hung. To avoid hitting it, makemtu/ address / obfuscation edits during a window with no peer traffic. Note that disabling the inbound first does not help:Remove(internal/amneziawgnet/manager.go:295) takes the same lock and closes the same device.If you can reproduce it once more, a goroutine dump would nail the pair conclusively. Set
XUI_PPROF=truein/etc/default/x-ui(that is the environment file on Ubuntu),systemctl restart x-ui, then during the hang:The two stacks that matter are one in
amneziawgnet.(*Manager).Ensuresitting in the device close, and one inamneziawgnet.(*Manager).Lookupsitting insync.(*Mutex).Lock. It is not required for triage — the inversion is visible in the source — but it would remove the last inferred link.Verdict: Confirmed bug
Severity: High
Confidence: High
Root cause:
internal/amneziawgnet/manager.go:86—Manager.Ensureholdsm.mufor the entireensureLockedcall, which then tears down (manager.go:165,cur.dev.Close()) or replace_peers-reconfigures (manager.go:151,cur.dev.IpcSet) the running device. Both must stop every peer's receive goroutine, and that goroutine is the one that runs the inline UDP handler atmanager.go:199-201, which callsm.Lookup→m.mu(manager.go:335-337). Trigger: any AmneziaWG inbound update concurrent with at least one in-flight UDP packet. Introduced byeffcccce("feat(amneziawg): add native AmneziaWG protocol support (#6105)", 2026-08-24) —internal/amneziawgnet/manager.gohas no other commit.Already fixed: Still present on the default branch (
git log -- internal/amneziawgnet/manager.goreturns onlyeffcccce;git tag --contains effcccce→ v3.7.0, dev-latest).Duplicate of: None. #6323 is the same subsystem but a different symptom (TCP never relayed) and is not established to share this root cause.
Evidence:
internal/amneziawgnet/manager.go:86-90—m.mu.Lock(); defer m.mu.Unlock(); return m.ensureLocked(d)internal/amneziawgnet/manager.go:162-167— rebuild branch:cur.udpRelay.Close(); cur.portForwards.Close(); cur.dev.Close()under that lockinternal/amneziawgnet/manager.go:151— in-place branch:if err := cur.dev.IpcSet(conf); err != nil {under that lockinternal/amneziawgnet/manager.go:96-99— in-repo statement thatIpcSet'sreplace_peers=trueis implemented asdevice.RemoveAllPeers(), unconditionallyinternal/amneziawgnet/manager.go:199-201/:335-337— the UDP handler closure callsm.Lookup, which locksm.muinternal/amneziawgnet/udp.go:34-40—SetTransportProtocolHandlercallshandler(src, dst, data)inline, no goroutineinternal/amneziawgnet/forwarder.go:27— the TCP path does spawn a goroutine, which is why TCP alone does not reproduce itinternal/amneziawgnet/device.go:113-117— in-repo confirmation that packets are delivered into the stack on "the peer's receive goroutine"internal/web/service/inbound.go:1716then:1742-1744— DB commit precedes the synchronous runtime applyinternal/web/runtime/local.go:196—updateAmneziaWGInboundcallsEnsurein the request goroutineinternal/web/job/amneziawg_job.go:54— the 10s reconcile takes the same lock, so the wedge is permanentNot determined: amneziawg-go v3.1.20260814's own
Device.Close/Peer.Stopsource was not readable here (no module cache in this checkout), so the exact join that blocks —peer.stopping.Wait()in upstream wireguard-go lineage — is inferred from the upstream contract rather than quoted. The goroutine dump above settles it. Also unverified: the panel'sGetStatuscallsHasRunning()(internal/web/service/server.go:633), which takes the same mutex, so I would expect the dashboard status endpoint to hang too after the deadlock — you reported the web UI as still serving, so that is worth a second look.Suggested fix: Stop holding
m.muacross the device mutation inensureLocked— snapshot the*managedunder the lock, release it, thenClose/IpcSetoutside it. Alternatively, give the peer index its own lock (or store it atomically) so the data-pathLookupcan never be blocked by an admin apply.@MHSanaei
This reply was generated automatically; a maintainer may follow up.
@dawNotPoi commented on GitHub (Sep 3, 2026):
I would like to work on this. I will keep lifecycle operations serialized while removing the data-path re-entry into the manager lock, and add a focused local regression test that calls the production UDP handler while the lifecycle lock is held.