[PR #6397] [MERGED] fix(amneziawg): avoid manager lock inversion #6251

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6397
Author: @dawNotPoi
Created: 9/3/2026
Status: ✅ Merged
Merged: 9/3/2026
Merged by: @MHSanaei

Base: main ← Head: fix/6368-amneziawg-lock-inversion


📝 Commits (2)

  • 142d375 fix(amneziawg): avoid manager lock inversion
  • 40bb341 test(amneziawg): exercise UDP relay hit path

📊 Changes

2 files changed (+78 additions, -53 deletions)

View changed files

📝 internal/amneziawgnet/manager.go (+49 -53)
📝 internal/amneziawgnet/manager_test.go (+29 -0)

📄 Description

Summary

Prevent embedded AmneziaWG updates from deadlocking with active UDP traffic by keeping packet-path peer lookup independent from the manager lifecycle lock.

Why

Manager.Ensure, Reconcile, Remove, and StopAll serialize device mutations with Manager.mu. The UDP receive handler previously called Manager.Lookup, which tried to acquire that same mutex while IpcSet or Device.Close could be waiting for the receive goroutine to exit.

This change publishes immutable peer indexes through an atomic pointer owned by each managed device. TCP and UDP handlers read that snapshot directly, while lifecycle operations remain serialized. Teardown now stops packet delivery before closing UDP relay sessions so an in-flight handler cannot recreate a session after cleanup.

Closes #6368

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

How was this tested?

  • Added a regression test that calls the production UDP handler while the manager lifecycle lock is held.
  • go test ./internal/amneziawgnet -run TestManagedUDPHandlerDoesNotWaitForManagerLock -count=50
  • go test -shuffle=on -count=1 ./internal/amneziawgnet
  • go test -race -shuffle=on -count=1 ./internal/amneziawgnet
  • make test-go
  • go vet ./internal/amneziawgnet
  • go build ./...

Screenshots / recordings

N/A

Breaking changes

None.

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally.
  • For frontend changes: not applicable; this PR does not touch the frontend.
  • I updated the Wiki / README / API docs if user-facing behavior changed (not applicable).
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 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/6397 **Author:** [@dawNotPoi](https://github.com/dawNotPoi) **Created:** 9/3/2026 **Status:** ✅ Merged **Merged:** 9/3/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/6368-amneziawg-lock-inversion` --- ### 📝 Commits (2) - [`142d375`](https://github.com/MHSanaei/3x-ui/commit/142d3757491fa199de16a3bf2fa366d2c08f9972) fix(amneziawg): avoid manager lock inversion - [`40bb341`](https://github.com/MHSanaei/3x-ui/commit/40bb341cc0ebcc1657f59486d1e5ae194aba0253) test(amneziawg): exercise UDP relay hit path ### 📊 Changes **2 files changed** (+78 additions, -53 deletions) <details> <summary>View changed files</summary> 📝 `internal/amneziawgnet/manager.go` (+49 -53) 📝 `internal/amneziawgnet/manager_test.go` (+29 -0) </details> ### 📄 Description ## Summary Prevent embedded AmneziaWG updates from deadlocking with active UDP traffic by keeping packet-path peer lookup independent from the manager lifecycle lock. ## Why `Manager.Ensure`, `Reconcile`, `Remove`, and `StopAll` serialize device mutations with `Manager.mu`. The UDP receive handler previously called `Manager.Lookup`, which tried to acquire that same mutex while `IpcSet` or `Device.Close` could be waiting for the receive goroutine to exit. This change publishes immutable peer indexes through an atomic pointer owned by each managed device. TCP and UDP handlers read that snapshot directly, while lifecycle operations remain serialized. Teardown now stops packet delivery before closing UDP relay sessions so an in-flight handler cannot recreate a session after cleanup. Closes #6368 ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [ ] Frontend (UI / panel pages) - [x] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [ ] Telegram bot ## How was this tested? - Added a regression test that calls the production UDP handler while the manager lifecycle lock is held. - `go test ./internal/amneziawgnet -run TestManagedUDPHandlerDoesNotWaitForManagerLock -count=50` - `go test -shuffle=on -count=1 ./internal/amneziawgnet` - `go test -race -shuffle=on -count=1 ./internal/amneziawgnet` - `make test-go` - `go vet ./internal/amneziawgnet` - `go build ./...` ## Screenshots / recordings N/A ## Breaking changes None. ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build ./...` and the test suite pass locally. - [x] For frontend changes: not applicable; this PR does not touch the frontend. - [x] I updated the Wiki / README / API docs if user-facing behavior changed (not applicable). - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:07:25 +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#6251
No description provided.