[PR #5614] [MERGED] fix(runtime): refresh cached node remotes on identity change #5930

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/5614
Author: @n0ctal
Created: 6/27/2026
Status: ✅ Merged
Merged: 6/28/2026
Merged by: @MHSanaei

Base: main ← Head: upstream-c-remote-cache-convergence


📝 Commits (4)

  • 060fad5 fix(runtime): refresh cached node remotes on identity change
  • c8565a8 Merge branch 'main' into upstream-c-remote-cache-convergence
  • 1fae610 Merge branch 'main' into upstream-c-remote-cache-convergence
  • 64053e0 Merge branch 'main' into upstream-c-remote-cache-convergence

📊 Changes

2 files changed (+162 additions, -3 deletions)

View changed files

📝 internal/web/runtime/manager.go (+40 -3)
➕ internal/web/runtime/manager_convergence_test.go (+122 -0)

📄 Description

Summary

Manager.RemoteFor caches one *Remote per node id and returns the cached instance on every later call, keyed only on node.Id. If a node's connection identity changes (address, port, base path, scheme, API token, TLS verify mode, pinned cert, egress/outbound tag, allow-private), the cached Remote keeps using the stale identity until the process restarts or the entry is explicitly invalidated. This PR makes the cache converge: a cached remote whose identity no longer matches the requested node is transparently replaced.

Why

A node's identity can change at runtime (token rotation, address/port move, switching TLS verify mode, repinning a cert). With the id-only cache, the master can keep talking to a worker with an old token or old address after the operator has already changed it — silently, with no error, until a restart. That is both a correctness bug (config changes don't take effect) and a security gap (a rotated-out token stays live in the cached path). Convergence on identity change closes both.

Scope

internal/web/runtime/manager.go:

  • RemoteFor now compares the cached remote's identity against the requested node via sameRemoteIdentity. On a match it returns the cached instance (fast path under RLock); on a mismatch it rebuilds the remote under the write lock and replaces the map entry. Standard double-checked locking — no network I/O is performed under the lock.
  • sameRemoteIdentity compares exactly the fields that affect how we connect/authenticate: Id, Scheme, Address, Port, BasePath, ApiToken, AllowPrivateAddress, TlsVerifyMode, PinnedCertSha256, OutboundTag. Display/metrics fields are intentionally excluded so cosmetic edits don't churn the cache.
  • cloneRemoteNode stores a defensive copy of the node in the cache (deep-copying InboundTags) so later mutation of the caller's *Node cannot retroactively change the cached identity and mask a real divergence.

Tests: manager_convergence_test.go covers same-identity cache hit (no rebuild), each identity-field change forcing a rebuild, and the no-churn case for display fields.

Validation

  • go test ./internal/web/runtime -run 'Convergence|RemoteFor|Identity|Refresh' — green
  • go test -race ./internal/web/runtime -run 'Convergence|RemoteFor|Identity|Refresh' — green
  • go vet ./internal/web/runtime — clean

Risk

Low. Behavior is unchanged when a node's identity is unchanged (same cached instance returned). The only new behavior is that an identity change now produces a fresh remote instead of reusing a stale one; any in-flight request on the old instance completes normally and the old remote is GC'd. No public API or schema change.


🔄 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/5614 **Author:** [@n0ctal](https://github.com/n0ctal) **Created:** 6/27/2026 **Status:** ✅ Merged **Merged:** 6/28/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `upstream-c-remote-cache-convergence` --- ### 📝 Commits (4) - [`060fad5`](https://github.com/MHSanaei/3x-ui/commit/060fad5287878d17dbb73c2fca44f4fb9ea40259) fix(runtime): refresh cached node remotes on identity change - [`c8565a8`](https://github.com/MHSanaei/3x-ui/commit/c8565a81fb6c75515610856b1e68fd37830885f7) Merge branch 'main' into upstream-c-remote-cache-convergence - [`1fae610`](https://github.com/MHSanaei/3x-ui/commit/1fae610ef77d6cdf81af90019fa047c75a87e10e) Merge branch 'main' into upstream-c-remote-cache-convergence - [`64053e0`](https://github.com/MHSanaei/3x-ui/commit/64053e091607819aeae401e359103a9cb69d88fe) Merge branch 'main' into upstream-c-remote-cache-convergence ### 📊 Changes **2 files changed** (+162 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/runtime/manager.go` (+40 -3) ➕ `internal/web/runtime/manager_convergence_test.go` (+122 -0) </details> ### 📄 Description ## Summary `Manager.RemoteFor` caches one `*Remote` per node id and returns the cached instance on every later call, keyed only on `node.Id`. If a node's connection identity changes (address, port, base path, scheme, API token, TLS verify mode, pinned cert, egress/outbound tag, allow-private), the cached `Remote` keeps using the **stale** identity until the process restarts or the entry is explicitly invalidated. This PR makes the cache converge: a cached remote whose identity no longer matches the requested node is transparently replaced. ## Why A node's identity can change at runtime (token rotation, address/port move, switching TLS verify mode, repinning a cert). With the id-only cache, the master can keep talking to a worker with an old token or old address after the operator has already changed it — silently, with no error, until a restart. That is both a correctness bug (config changes don't take effect) and a security gap (a rotated-out token stays live in the cached path). Convergence on identity change closes both. ## Scope `internal/web/runtime/manager.go`: - `RemoteFor` now compares the cached remote's identity against the requested node via `sameRemoteIdentity`. On a match it returns the cached instance (fast path under `RLock`); on a mismatch it rebuilds the remote under the write lock and replaces the map entry. Standard double-checked locking — no network I/O is performed under the lock. - `sameRemoteIdentity` compares exactly the fields that affect how we connect/authenticate: `Id, Scheme, Address, Port, BasePath, ApiToken, AllowPrivateAddress, TlsVerifyMode, PinnedCertSha256, OutboundTag`. Display/metrics fields are intentionally excluded so cosmetic edits don't churn the cache. - `cloneRemoteNode` stores a defensive copy of the node in the cache (deep-copying `InboundTags`) so later mutation of the caller's `*Node` cannot retroactively change the cached identity and mask a real divergence. Tests: `manager_convergence_test.go` covers same-identity cache hit (no rebuild), each identity-field change forcing a rebuild, and the no-churn case for display fields. ## Validation - `go test ./internal/web/runtime -run 'Convergence|RemoteFor|Identity|Refresh'` — green - `go test -race ./internal/web/runtime -run 'Convergence|RemoteFor|Identity|Refresh'` — green - `go vet ./internal/web/runtime` — clean ## Risk Low. Behavior is unchanged when a node's identity is unchanged (same cached instance returned). The only new behavior is that an identity change now produces a fresh remote instead of reusing a stale one; any in-flight request on the old instance completes normally and the old remote is GC'd. No public API or schema change. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:05:06 +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#5930
No description provided.