[PR #5319] [MERGED] fix(nodes): sync "start after first connect" expiry so un-activated nodes do not reset it #5847

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/5319
Author: @rqzbeh
Created: 6/14/2026
Status: ✅ Merged
Merged: 6/15/2026
Merged by: @MHSanaei

Base: main ← Head: fix/node-expiry-first-connect-sync


📝 Commits (4)

  • 6505ce7 fix(nodes): stop un-activated nodes from resetting "start after first connect" expiry
  • 54fb2e6 Merge branch 'main' into fix/node-expiry-first-connect-sync
  • af60c1d Merge branch 'main' into fix/node-expiry-first-connect-sync
  • 3898849 fix(nodes): cast expiry merge params to BIGINT for Postgres

📊 Changes

2 files changed (+173 additions, -4 deletions)

View changed files

📝 internal/web/service/inbound_node.go (+32 -4)
➕ internal/web/service/node_client_expiry_sync_test.go (+141 -0)

📄 Description

Summary

Fixes multi-node sync of the "start expiry after first connect" timer. A node where a client never connected was resetting the activated expiry deadline that another node had already started, so the main panel showed the timer "not started" and the subscription reported a wrong, perpetually-resetting expiry/usage.

Why

A user with inbounds spread across several nodes reported that traffic usage syncs between nodes but the expiry time does not: one node showed 28 days remaining (timer running) while the main panel still showed the full 30 days "not started", and the subscription's expiry/usage were wrong as a result.

Root cause:

  • A client attached to inbounds on multiple nodes shares one client_traffics row — Email is gorm:"unique" (internal/xray/client_traffic.go).
  • With "start after first connect", expiryTime is stored as a negative duration; each node converts it to an absolute deadline (now + duration) the first time the client connects on that node (adjustTraffics in inbound_traffic.go).
  • The master's per-node merge (setRemoteTrafficLocked in internal/web/service/inbound_node.go) wrote expiry_time = ? unconditionally for every node sync. A node where the client never connected keeps reporting the un-activated negative value and overwrites the absolute deadline the active node had set — last writer wins, and the un-activated nodes keep re-asserting the negative value.
  • The subscription (internal/sub/service.go → subscriptionExpiryFromClient) reads that shared row and recomputes a negative value as now + duration on every fetch, so the client's expiry never counts down and the usage shown is wrong.

The fix

Guard the merge: an un-activated (<= 0) value reported by a node can never reset an already-activated absolute deadline. A positive node value is still adopted unchanged, so a node that legitimately moves the deadline forward (traffic reset / auto-renew) still propagates — the guard only blocks the negative-clobbers-positive case that causes the bug.

The rule is expressed both in the SQL CASE used by the merge and a tiny mergeActivationExpiry helper kept in lockstep with it; the structural-change check reuses the helper so the guard doesn't cause spurious config re-pushes to nodes.

Type of change

  • Bug fix

Areas affected

  • Backend (API endpoints, login, settings)
  • Multi-node (sub-nodes)
  • Subscription (share links / Clash / JSON)

How was this tested?

go build ./..., go vet ./..., and go test ./... all pass. New tests in internal/web/service/node_client_expiry_sync_test.go, built on the existing node-merge harness (real SQLite DB):

  • TestMergeActivationExpiry — the pure rule (master unset → take node; activation adopted over stored duration; un-activated/zero node value never resets an activated deadline; positive node value always adopted, incl. forward renewal).
  • TestNodeFirstConnectExpiry_NotClobbered — end-to-end through setRemoteTrafficLocked: node 1 activates an absolute deadline; a later node-2 sync still reporting the negative duration must not reset it.
  • TestNodeFirstConnectExpiry_NotClobbered_WithSettings — same, but snapshots carry real settings JSON, exercising the GetClients/SyncInbound branch to prove that path doesn't re-derive the per-email expiry from the node's still-negative settings.
  • TestNodeRenewExtendsExpiry — guards against over-correcting: a node that moves the deadline forward (auto-renew) still propagates to the master.

All existing node-merge/traffic tests (TestTwoNodesShareEmail_SumsCorrectly, TestInboundRemoval_KeepsSharedEmailRow, TestStatsUnderSiblingInbound_KeepsNodeBaseline, TestCentralReset_NoReAdd, …) still pass — traffic accounting is unchanged.

Screenshots / recordings

N/A (backend-only).

Breaking changes

None. Only an un-activated (<= 0) expiry reported by a node is prevented from overwriting an already-activated, positive deadline. Traffic accounting, auto-renew forward extensions, and the authoritative master→node config push are unchanged; the shared expiry row simply converges to the first activation instead of flip-flopping. No migration required.

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: npm run lint, npm run typecheck, and npm run build pass. (No frontend changes.)
  • I updated the Wiki / README / API docs if user-facing behavior changed. (No doc/API surface change.)
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🤖 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/5319 **Author:** [@rqzbeh](https://github.com/rqzbeh) **Created:** 6/14/2026 **Status:** ✅ Merged **Merged:** 6/15/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/node-expiry-first-connect-sync` --- ### 📝 Commits (4) - [`6505ce7`](https://github.com/MHSanaei/3x-ui/commit/6505ce725cf8fd3d17b200b04b56d58c851f9fb3) fix(nodes): stop un-activated nodes from resetting "start after first connect" expiry - [`54fb2e6`](https://github.com/MHSanaei/3x-ui/commit/54fb2e6ab88cd9051b0d7114b21f22b2ded1399e) Merge branch 'main' into fix/node-expiry-first-connect-sync - [`af60c1d`](https://github.com/MHSanaei/3x-ui/commit/af60c1d33a5f5136380d144d1c90aa386801ef12) Merge branch 'main' into fix/node-expiry-first-connect-sync - [`3898849`](https://github.com/MHSanaei/3x-ui/commit/38988493a377b3dba640e457a619d14d1acf445c) fix(nodes): cast expiry merge params to BIGINT for Postgres ### 📊 Changes **2 files changed** (+173 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/inbound_node.go` (+32 -4) ➕ `internal/web/service/node_client_expiry_sync_test.go` (+141 -0) </details> ### 📄 Description ## Summary Fixes multi-node sync of the **"start expiry after first connect"** timer. A node where a client never connected was resetting the activated expiry deadline that another node had already started, so the main panel showed the timer "not started" and the subscription reported a wrong, perpetually-resetting expiry/usage. ## Why A user with inbounds spread across several nodes reported that **traffic usage syncs between nodes but the expiry time does not**: one node showed 28 days remaining (timer running) while the main panel still showed the full 30 days "not started", and the subscription's expiry/usage were wrong as a result. Root cause: - A client attached to inbounds on multiple nodes shares **one** `client_traffics` row — `Email` is `gorm:"unique"` (`internal/xray/client_traffic.go`). - With "start after first connect", `expiryTime` is stored as a **negative duration**; each node converts it to an absolute deadline (`now + duration`) the first time the client connects *on that node* (`adjustTraffics` in `inbound_traffic.go`). - The master's per-node merge (`setRemoteTrafficLocked` in `internal/web/service/inbound_node.go`) wrote `expiry_time = ?` **unconditionally** for every node sync. A node where the client never connected keeps reporting the un-activated **negative** value and overwrites the absolute deadline the active node had set — last writer wins, and the un-activated nodes keep re-asserting the negative value. - The subscription (`internal/sub/service.go` → `subscriptionExpiryFromClient`) reads that shared row and recomputes a negative value as `now + duration` on **every fetch**, so the client's expiry never counts down and the usage shown is wrong. ## The fix Guard the merge: an **un-activated (`<= 0`) value reported by a node can never reset an already-activated absolute deadline**. A positive node value is still adopted unchanged, so a node that legitimately moves the deadline forward (traffic reset / auto-renew) still propagates — the guard only blocks the negative-clobbers-positive case that causes the bug. The rule is expressed both in the SQL `CASE` used by the merge and a tiny `mergeActivationExpiry` helper kept in lockstep with it; the structural-change check reuses the helper so the guard doesn't cause spurious config re-pushes to nodes. ## Type of change - [x] Bug fix ## Areas affected - [x] Backend (API endpoints, login, settings) - [x] Multi-node (sub-nodes) - [x] Subscription (share links / Clash / JSON) ## How was this tested? `go build ./...`, `go vet ./...`, and `go test ./...` all pass. New tests in `internal/web/service/node_client_expiry_sync_test.go`, built on the existing node-merge harness (real SQLite DB): - `TestMergeActivationExpiry` — the pure rule (master unset → take node; activation adopted over stored duration; un-activated/zero node value never resets an activated deadline; **positive node value always adopted, incl. forward renewal**). - `TestNodeFirstConnectExpiry_NotClobbered` — end-to-end through `setRemoteTrafficLocked`: node 1 activates an absolute deadline; a later node-2 sync still reporting the negative duration must **not** reset it. - `TestNodeFirstConnectExpiry_NotClobbered_WithSettings` — same, but snapshots carry real settings JSON, exercising the `GetClients`/`SyncInbound` branch to prove that path doesn't re-derive the per-email expiry from the node's still-negative settings. - `TestNodeRenewExtendsExpiry` — guards against over-correcting: a node that moves the deadline **forward** (auto-renew) still propagates to the master. All existing node-merge/traffic tests (`TestTwoNodesShareEmail_SumsCorrectly`, `TestInboundRemoval_KeepsSharedEmailRow`, `TestStatsUnderSiblingInbound_KeepsNodeBaseline`, `TestCentralReset_NoReAdd`, …) still pass — traffic accounting is unchanged. ## Screenshots / recordings N/A (backend-only). ## Breaking changes None. Only an un-activated (`<= 0`) expiry reported by a node is prevented from overwriting an already-activated, positive deadline. Traffic accounting, auto-renew forward extensions, and the authoritative master→node config push are unchanged; the shared expiry row simply converges to the first activation instead of flip-flopping. No migration required. ## 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. - [ ] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. _(No frontend changes.)_ - [ ] I updated the Wiki / README / API docs if user-facing behavior changed. _(No doc/API surface change.)_ - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. 🤖 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:04:31 +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#5847
No description provided.