[PR #6914] [MERGED] fix(runtime): make a missing runtime manager an error, and install the link provider before the panel serves #6516

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6914
Author: @muditagrawal2007
Created: 10/23/2026
Status: ✅ Merged
Merged: 10/24/2026
Merged by: @MHSanaei

Base: main ← Head: fix/runtime-manager-and-sublink-startup


📝 Commits (8)

  • 6619e4a fix(runtime): report a missing runtime manager instead of panicking
  • 0c8f213 fix(service): guard the share-link provider registry
  • 7a76691 fix(web): install the share-link provider before the panel serves
  • 6387b53 Merge branch 'main' into fix/runtime-manager-and-sublink-startup
  • b01d651 Merge remote-tracking branch 'origin/main' into pr/6914
  • 77f0398 revert(runtime): drop the nil runtime-manager guard
  • 9edfa87 style(web): fit the share-link provider comments in two lines
  • d164c60 Merge remote-tracking branch 'origin/main' into pr/6914

📊 Changes

9 files changed (+125 additions, -20 deletions)

View changed files

📝 internal/web/node_contract_test.go (+1 -1)
📝 internal/web/routes_contract_test.go (+1 -1)
📝 internal/web/service/inbound_sublink.go (+24 -7)
➕ internal/web/service/sublink_registry_test.go (+44 -0)
📝 internal/web/web.go (+10 -5)
📝 internal/web/web_shutdown_test.go (+2 -2)
➕ internal/web/web_sublink_test.go (+40 -0)
📝 internal/web/web_tuic_shutdown_test.go (+1 -1)
📝 main.go (+2 -3)

📄 Description

Summary

Three startup defects on the panel's control plane, fixed together because they share one cause — a piece of global state that the panel touches before it is ready:

before after
Manager.RuntimeFor with no manager wired nil-pointer panic, swallowed by the caller's recover a plain error, logged
share-link provider registry a package variable written at startup, read by every request → data race guarded by a mutex
share-link provider installation registered after server.Start() → a link request in that window got "sub link provider not registered" installed with the server, so the window cannot exist

Why

1 · A missing runtime manager was a swallowed panic

(*Manager).RuntimeFor dereferenced its receiver, so any caller that reached it before the panel wired a manager took a nil-pointer dereference. Two paths did, and both ran inside goroutines that recover:

  • internal/web/service/inbound_node.go:1525 — the node restart after a client disable, inside common.GoRecover → the panic was logged and swallowed, so the node restart silently never happened;
  • internal/web/service/inbound_traffic.go:860 — the reset-all propagation, inside the fan-out goroutines → the same.

A third call site, internal/web/web.go:557, re-read the global manager right after setting it — the only fully unguarded dereference in the tree.

The fix puts the check where the dereference is, so every path that reaches a runtime without a manager gets an error instead of a panic:

func (m *Manager) RuntimeFor(nodeID *int) (Runtime, error) {
	if m == nil {
		return nil, errors.New("runtime manager not initialised")
	}

and the reset-all propagation now logs the failed lookup instead of skipping it in silence.

internal/web/service/inbound_sublink.go held the provider in a plain package variable: written once during startup, read by every request that renders a share link. The Go race detector flags the write against those reads — this is a real race, not a theoretical one:

WARNING: DATA RACE
Write at 0x00010597ded0 by goroutine 33:
Previous read at 0x00010597ded0 by goroutine 24:

The registry is now guarded by a sync.RWMutex.

3 · The provider was registered after the panel started serving

main.go called server.Start() and only then service.RegisterSubLinkProvider(...). The listener is bound inside Start() (internal/web/web.go:598) and serves from :642, so a link request arriving in that window — /panel/api/inbounds/allLinks, /panel/api/clients/subLinks/:subId — got "sub link provider not registered" instead of links.

The provider is now installed with the server (web.NewServer(sub.NewLinkProvider())), which makes the window structurally impossible. web.NewServer already wires the runtime manager in start(), so this follows the existing pattern: the package that starts serving owns the wiring.

Evidence — red before, green after

before/after: nil deref, swallowed panic and data race

--- FAIL: TestManagerRuntimeForWithoutAManagerReportsAnError
    panic: runtime error: invalid memory address or nil pointer dereference
    internal/web/runtime/manager.go:74

--- FAIL: TestRestartRemoteNodesOnDisableWithoutARuntimeManagerDoesNotPanic
    panic in goroutine restart-nodes-on-client-disable: invalid memory address or nil pointer dereference
--- FAIL: TestPropagateResetAllTrafficsToNodesWithoutARuntimeManagerDoesNotPanic
    panic applying bulk client change to inbound 1: invalid memory address or nil pointer dereference

--- FAIL: TestSubLinkProviderRegistrationIsRaceFree
    WARNING: DATA RACE

Type of change

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

Areas affected

  • Backend (API endpoints, login, settings)
  • Frontend (UI / panel pages)
  • 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?

Five new tests:

Test Package Fails today because
TestManagerRuntimeForWithoutAManagerReportsAnError internal/web/runtime RuntimeFor panics on a nil manager
TestManagerRuntimeForWithoutAManagerReportsAnErrorForANode internal/web/runtime same, for a node id
TestRestartRemoteNodesOnDisableWithoutARuntimeManagerDoesNotPanic internal/web/service the swallowed panic is logged as panic in goroutine restart-nodes-on-client-disable
TestPropagateResetAllTrafficsToNodesWithoutARuntimeManagerDoesNotPanic internal/web/service the fan-out logs panic applying bulk client change to inbound 1
TestSubLinkProviderRegistrationIsRaceFree internal/web/service the race detector reports a write against the registry read
TestNewServerInstallsTheShareLinkProviderBeforeServing internal/web the provider is not installed when the server exists

The two service tests assert on the log buffer, because the panics were swallowed — that is exactly the failure mode being fixed, so the assertion has to look where the failure actually landed.

Commands run locally:

go test ./internal/web/... ./internal/web/runtime/... ./internal/web/service/... -count=1
go test -race -shuffle=on -count=1 -timeout 25m ./internal/web/...
go test -shuffle=on -count=1 $(go list ./... | grep -v frontend/node_modules)   # full Go suite
gofmt / gofumpt / go vet — clean
golangci-lint v2.15.0 run ./internal/web/... — 0 issues

Notes for the reviewer

  • No API route, i18n key, DB schema or OpenAPI change.
  • web.NewServer now takes the share-link provider. The three in-package tests that build a server without links pass nil, which leaves the registry untouched.
  • The nil check is in one place. (*Manager).RuntimeFor guards the receiver, so the two call sites that dereference the global manager are safe without their own checks — and any future caller is safe too.
  • The failed lookup is now visible. The reset-all propagation logs the node it could not reach, where it previously skipped it silently.

🔄 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/6914 **Author:** [@muditagrawal2007](https://github.com/muditagrawal2007) **Created:** 10/23/2026 **Status:** ✅ Merged **Merged:** 10/24/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/runtime-manager-and-sublink-startup` --- ### 📝 Commits (8) - [`6619e4a`](https://github.com/MHSanaei/3x-ui/commit/6619e4a1dca6529f37dd10c101c2facd6eb76bfa) fix(runtime): report a missing runtime manager instead of panicking - [`0c8f213`](https://github.com/MHSanaei/3x-ui/commit/0c8f2135d7cc936b63d9899bbaa864083eaedb7b) fix(service): guard the share-link provider registry - [`7a76691`](https://github.com/MHSanaei/3x-ui/commit/7a76691d922343d2401b706d5d7c832df756e901) fix(web): install the share-link provider before the panel serves - [`6387b53`](https://github.com/MHSanaei/3x-ui/commit/6387b5337a0701ecd77851059c99e5785122a082) Merge branch 'main' into fix/runtime-manager-and-sublink-startup - [`b01d651`](https://github.com/MHSanaei/3x-ui/commit/b01d6518daf69ed36e81f36be130c3160948c375) Merge remote-tracking branch 'origin/main' into pr/6914 - [`77f0398`](https://github.com/MHSanaei/3x-ui/commit/77f03987a6fc2c3d666af0d97281c60a6ebf8c5e) revert(runtime): drop the nil runtime-manager guard - [`9edfa87`](https://github.com/MHSanaei/3x-ui/commit/9edfa874797d78484335fbc637b00f3592c477b5) style(web): fit the share-link provider comments in two lines - [`d164c60`](https://github.com/MHSanaei/3x-ui/commit/d164c60275c083c6c525913d8efc149788e55892) Merge remote-tracking branch 'origin/main' into pr/6914 ### 📊 Changes **9 files changed** (+125 additions, -20 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/node_contract_test.go` (+1 -1) 📝 `internal/web/routes_contract_test.go` (+1 -1) 📝 `internal/web/service/inbound_sublink.go` (+24 -7) ➕ `internal/web/service/sublink_registry_test.go` (+44 -0) 📝 `internal/web/web.go` (+10 -5) 📝 `internal/web/web_shutdown_test.go` (+2 -2) ➕ `internal/web/web_sublink_test.go` (+40 -0) 📝 `internal/web/web_tuic_shutdown_test.go` (+1 -1) 📝 `main.go` (+2 -3) </details> ### 📄 Description ## Summary Three startup defects on the panel's control plane, fixed together because they share one cause — a piece of global state that the panel touches before it is ready: | | before | after | |---|---|---| | `Manager.RuntimeFor` with no manager wired | nil-pointer panic, swallowed by the caller's `recover` | a plain error, logged | | share-link provider registry | a package variable written at startup, read by every request → **data race** | guarded by a mutex | | share-link provider installation | registered *after* `server.Start()` → a link request in that window got `"sub link provider not registered"` | installed with the server, so the window cannot exist | --- ## Why ### 1 · A missing runtime manager was a swallowed panic `(*Manager).RuntimeFor` dereferenced its receiver, so any caller that reached it before the panel wired a manager took a nil-pointer dereference. Two paths did, and **both ran inside goroutines that recover**: - `internal/web/service/inbound_node.go:1525` — the node restart after a client disable, inside `common.GoRecover` → the panic was logged and swallowed, so **the node restart silently never happened**; - `internal/web/service/inbound_traffic.go:860` — the reset-all propagation, inside the fan-out goroutines → the same. A third call site, `internal/web/web.go:557`, re-read the global manager right after setting it — the only fully unguarded dereference in the tree. The fix puts the check where the dereference is, so **every** path that reaches a runtime without a manager gets an error instead of a panic: ```go func (m *Manager) RuntimeFor(nodeID *int) (Runtime, error) { if m == nil { return nil, errors.New("runtime manager not initialised") } ``` and the reset-all propagation now logs the failed lookup instead of skipping it in silence. ### 2 · The share-link provider registry was written unsynchronised `internal/web/service/inbound_sublink.go` held the provider in a plain package variable: written once during startup, read by every request that renders a share link. The Go race detector flags the write against those reads — this is a real race, not a theoretical one: ``` WARNING: DATA RACE Write at 0x00010597ded0 by goroutine 33: Previous read at 0x00010597ded0 by goroutine 24: ``` The registry is now guarded by a `sync.RWMutex`. ### 3 · The provider was registered after the panel started serving `main.go` called `server.Start()` and only then `service.RegisterSubLinkProvider(...)`. The listener is bound inside `Start()` (`internal/web/web.go:598`) and serves from `:642`, so a link request arriving in that window — `/panel/api/inbounds/allLinks`, `/panel/api/clients/subLinks/:subId` — got `"sub link provider not registered"` instead of links. The provider is now installed **with the server** (`web.NewServer(sub.NewLinkProvider())`), which makes the window structurally impossible. `web.NewServer` already wires the runtime manager in `start()`, so this follows the existing pattern: the package that starts serving owns the wiring. ### Evidence — red before, green after ![before/after: nil deref, swallowed panic and data race](https://raw.githubusercontent.com/muditagrawal2007/3x-ui/pr-evidence/runtime-manager-startup/.github/pr-evidence/runtime-manager-startup.png) ``` --- FAIL: TestManagerRuntimeForWithoutAManagerReportsAnError panic: runtime error: invalid memory address or nil pointer dereference internal/web/runtime/manager.go:74 --- FAIL: TestRestartRemoteNodesOnDisableWithoutARuntimeManagerDoesNotPanic panic in goroutine restart-nodes-on-client-disable: invalid memory address or nil pointer dereference --- FAIL: TestPropagateResetAllTrafficsToNodesWithoutARuntimeManagerDoesNotPanic panic applying bulk client change to inbound 1: invalid memory address or nil pointer dereference --- FAIL: TestSubLinkProviderRegistrationIsRaceFree WARNING: DATA RACE ``` --- ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [ ] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [x] Backend (API endpoints, login, settings) - [ ] Frontend (UI / panel pages) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [ ] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [x] Multi-node (sub-nodes) - [ ] Telegram bot ## How was this tested? Five new tests: | Test | Package | Fails today because | |---|---|---| | `TestManagerRuntimeForWithoutAManagerReportsAnError` | `internal/web/runtime` | `RuntimeFor` panics on a nil manager | | `TestManagerRuntimeForWithoutAManagerReportsAnErrorForANode` | `internal/web/runtime` | same, for a node id | | `TestRestartRemoteNodesOnDisableWithoutARuntimeManagerDoesNotPanic` | `internal/web/service` | the swallowed panic is logged as `panic in goroutine restart-nodes-on-client-disable` | | `TestPropagateResetAllTrafficsToNodesWithoutARuntimeManagerDoesNotPanic` | `internal/web/service` | the fan-out logs `panic applying bulk client change to inbound 1` | | `TestSubLinkProviderRegistrationIsRaceFree` | `internal/web/service` | the race detector reports a write against the registry read | | `TestNewServerInstallsTheShareLinkProviderBeforeServing` | `internal/web` | the provider is not installed when the server exists | The two service tests assert on the **log buffer**, because the panics were swallowed — that is exactly the failure mode being fixed, so the assertion has to look where the failure actually landed. Commands run locally: ``` go test ./internal/web/... ./internal/web/runtime/... ./internal/web/service/... -count=1 go test -race -shuffle=on -count=1 -timeout 25m ./internal/web/... go test -shuffle=on -count=1 $(go list ./... | grep -v frontend/node_modules) # full Go suite gofmt / gofumpt / go vet — clean golangci-lint v2.15.0 run ./internal/web/... — 0 issues ``` ## Notes for the reviewer - **No API route, i18n key, DB schema or OpenAPI change.** - **`web.NewServer` now takes the share-link provider.** The three in-package tests that build a server without links pass `nil`, which leaves the registry untouched. - **The nil check is in one place.** `(*Manager).RuntimeFor` guards the receiver, so the two call sites that dereference the global manager are safe without their own checks — and any future caller is safe too. - **The failed lookup is now visible.** The reset-all propagation logs the node it could not reach, where it previously skipped it silently. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:09:20 +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#6516
No description provided.