[PR #6734] [MERGED] fix(node): don't delete clients when a node reports an empty snapshot #6434

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

📋 Pull Request Information

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

Base: main ← Head: fix/node-empty-snapshot-client-deletion


📝 Commits (4)

  • 4295ee8 fix(node): don't delete clients when a node reports an empty snapshot
  • 2fc4dc7 Merge branch 'main' into fix/node-empty-snapshot-client-deletion
  • 23c5107 fix(node): keep hub settings on an empty node snapshot; address review
  • 0a9e706 fix(node): re-push hub clients to a node that reports an empty snapshot

📊 Changes

6 files changed (+284 additions, -13 deletions)

View changed files

📝 internal/web/node_contract_test.go (+1 -0)
📝 internal/web/runtime/remote.go (+11 -0)
📝 internal/web/service/client_sync_orphan_test.go (+21 -12)
📝 internal/web/service/inbound_amneziawg_test.go (+3 -0)
📝 internal/web/service/inbound_node.go (+43 -1)
➕ internal/web/service/node_degraded_snapshot_test.go (+205 -0)

📄 Description

Problem

In a hub + managed-nodes setup, the hub reconciles each node by pulling a snapshot and mirroring it into the central DB (setRemoteTrafficLocked → SyncInbound). SyncInbound makes an inbound's client_inbounds links match the snapshot exactly: any client missing from the snapshot loses its link. A client left with no links anywhere is then soft-orphaned (markSyncOrphan) and hard-deleted by ReapSyncOrphans after the grace period.

The problem: a node that comes back reporting zero clients for an inbound the hub still has clients on is treated as the authoritative truth "every client was removed". But a zero-client snapshot is indistinguishable from a degraded node — one that was just deleted, reset, restarted, or answered before its config finished loading. The existing guard only skips the sweep when SyncInbound returns an error; a successful-but-empty snapshot passes straight through.

Impact

Removing (or momentarily resetting) a single node cascaded into clients being stripped of their links and reaped across the whole hub — including clients that were still valid and configured on the other nodes. Recovery required restoring the DB from backup.

A subtle tell: TCP traffic by domain keeps working (the name is resolved by the remote node), while the client records quietly disappear and raw DNS/UDP paths break, so it first looks like a DNS problem rather than data loss.

Fix

Treat a zero-client snapshot for an inbound that still has clients attached as non-authoritative: skip the link rebuild and the orphan sweep for that inbound (the same handling a failed SyncInbound already gets) and wait for a snapshot that actually carries clients.

  • A node's last client is removed from the hub side (which updates links and pushes to the node), not by making the node report empty.
  • A node that still serves other clients continues to prune a genuinely-removed one through the existing partial-snapshot path (≥1 client reported ⇒ authoritative). The guard is deliberately narrow: only the all-empty case is treated as degraded.

~21 lines in setRemoteTrafficLocked, no schema or API change.

Tests

  • node_degraded_snapshot_test.go (new):
    • TestSetRemoteTraffic_EmptySnapshotKeepsClients — an empty snapshot keeps links and does not orphan-mark.
    • TestSetRemoteTraffic_EmptySnapshotSurvivesReap — even a backdated ReapSyncOrphans can't delete them.
    • TestSetRemoteTraffic_PartialSnapshotStillPrunes — a partial snapshot still prunes + orphan-marks a really-removed client (guard stays narrow).
  • TestSyncOrphanSurvivesMergeUntilGraceElapses / TestSyncOrphanMarkClearedOnReattach updated to drive removal via a partial snapshot (the authoritative path) instead of an empty one; the soft-orphan / grace / reattach mechanics they cover are unchanged.

Verified: both new guard tests fail on main without the change and pass with it; the full ./internal/web/service/ suite is green; go build ./... is clean.

🤖 Generated with Claude Code


🔄 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/6734 **Author:** [@yukh975](https://github.com/yukh975) **Created:** 10/5/2026 **Status:** ✅ Merged **Merged:** 10/5/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/node-empty-snapshot-client-deletion` --- ### 📝 Commits (4) - [`4295ee8`](https://github.com/MHSanaei/3x-ui/commit/4295ee8a28611efa8f77121eaf03481a0505bc7a) fix(node): don't delete clients when a node reports an empty snapshot - [`2fc4dc7`](https://github.com/MHSanaei/3x-ui/commit/2fc4dc7b422452ab1e498ab6e15863fc3263cc2f) Merge branch 'main' into fix/node-empty-snapshot-client-deletion - [`23c5107`](https://github.com/MHSanaei/3x-ui/commit/23c51074a2675445ae149f8ee4faf3385e44408c) fix(node): keep hub settings on an empty node snapshot; address review - [`0a9e706`](https://github.com/MHSanaei/3x-ui/commit/0a9e7067ff2597a07f3742ebb78757f872571dcd) fix(node): re-push hub clients to a node that reports an empty snapshot ### 📊 Changes **6 files changed** (+284 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/node_contract_test.go` (+1 -0) 📝 `internal/web/runtime/remote.go` (+11 -0) 📝 `internal/web/service/client_sync_orphan_test.go` (+21 -12) 📝 `internal/web/service/inbound_amneziawg_test.go` (+3 -0) 📝 `internal/web/service/inbound_node.go` (+43 -1) ➕ `internal/web/service/node_degraded_snapshot_test.go` (+205 -0) </details> ### 📄 Description ## Problem In a hub + managed-nodes setup, the hub reconciles each node by pulling a snapshot and mirroring it into the central DB (`setRemoteTrafficLocked` → `SyncInbound`). `SyncInbound` makes an inbound's `client_inbounds` links match the snapshot **exactly**: any client missing from the snapshot loses its link. A client left with no links anywhere is then soft-orphaned (`markSyncOrphan`) and **hard-deleted by `ReapSyncOrphans`** after the grace period. The problem: a node that comes back reporting **zero clients** for an inbound the hub still has clients on is treated as the authoritative truth "every client was removed". But a zero-client snapshot is indistinguishable from a **degraded node** — one that was just deleted, reset, restarted, or answered before its config finished loading. The existing guard only skips the sweep when `SyncInbound` returns an *error*; a successful-but-empty snapshot passes straight through. ### Impact Removing (or momentarily resetting) a single node cascaded into clients being stripped of their links and reaped across the whole hub — including clients that were still valid and configured on the other nodes. Recovery required restoring the DB from backup. A subtle tell: **TCP** traffic by domain keeps working (the name is resolved by the remote node), while the client records quietly disappear and raw DNS/UDP paths break, so it first looks like a DNS problem rather than data loss. ## Fix Treat a zero-client snapshot for an inbound that still has clients attached as **non-authoritative**: skip the link rebuild and the orphan sweep for that inbound (the same handling a failed `SyncInbound` already gets) and wait for a snapshot that actually carries clients. - A node's **last** client is removed from the hub side (which updates links and pushes to the node), not by making the node report empty. - A node that still serves **other** clients continues to prune a genuinely-removed one through the existing **partial-snapshot** path (≥1 client reported ⇒ authoritative). The guard is deliberately narrow: only the *all-empty* case is treated as degraded. ~21 lines in `setRemoteTrafficLocked`, no schema or API change. ## Tests - `node_degraded_snapshot_test.go` (new): - `TestSetRemoteTraffic_EmptySnapshotKeepsClients` — an empty snapshot keeps links and does not orphan-mark. - `TestSetRemoteTraffic_EmptySnapshotSurvivesReap` — even a backdated `ReapSyncOrphans` can't delete them. - `TestSetRemoteTraffic_PartialSnapshotStillPrunes` — a partial snapshot still prunes + orphan-marks a really-removed client (guard stays narrow). - `TestSyncOrphanSurvivesMergeUntilGraceElapses` / `TestSyncOrphanMarkClearedOnReattach` updated to drive removal via a **partial** snapshot (the authoritative path) instead of an empty one; the soft-orphan / grace / reattach mechanics they cover are unchanged. Verified: both new guard tests fail on `main` without the change and pass with it; the full `./internal/web/service/` suite is green; `go build ./...` is clean. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <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:44 +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#6434
No description provided.