[PR #7029] [MERGED] fix: panel restart left the panel unreachable for up to a minute #6586

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

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/7029
Author: @Plus98ir
Created: 10/30/2026
Status: ✅ Merged
Merged: 11/2/2026
Merged by: @MHSanaei

Base: main ← Head: fix/panel-restart-hang


📝 Commits (3)

  • fb0507d fix(jobs): stop the CPU alarm job from blocking panel restarts for a minute
  • 90f5ccf fix(tgbot): end in-flight Telegram calls when the bot stops
  • 5ded2a7 fix(jobs): keep iowait out of the CPU alarm's busy share

📊 Changes

8 files changed (+304 additions, -42 deletions)

View changed files

➕ internal/util/sys/cpu_times.go (+36 -0)
📝 internal/web/job/check_cpu_usage.go (+21 -8)
➕ internal/web/job/check_cpu_usage_test.go (+104 -0)
📝 internal/web/service/server.go (+2 -26)
➕ internal/web/service/tgbot/stoppable_caller.go (+43 -0)
➕ internal/web/service/tgbot/stoppable_caller_test.go (+68 -0)
📝 internal/web/service/tgbot/tgbot.go (+26 -5)
📝 internal/web/web.go (+4 -3)

📄 Description

Problem

Restarting the panel from Settings left it unreachable (HTTP 502 behind nginx) for anywhere from a few seconds to a minute. On a live server the outages measured 4s, 19s and 45s. On a server where Telegram is unreachable it gets worse.

stop() closes the HTTP listener first and then waits for background work. Two parts of that work could block for a long time:

  1. CheckCpuJob sampled with cpu.Percent(1*time.Minute). Each run blocked for its full @every 1m period, so the job was nearly always running, and cron.Stop() waits for running jobs. This alone causes a 0–60s outage on every restart, Telegram or not.
  2. Telegram calls when Telegram is unreachable (TLS handshake timing out). fasthttp honours deadlines but not context cancellation, so:
    • bus.Stop() waited for the Telegram notifier's in-flight sendMessage (~30s);
    • tgbot.Start ran getMe + setMyCommands inline (~60s) on the signal loop, so the sub server started late and a second restart request sat unhandled.

Fix

  • CheckCpuJob keeps the previous cpu.Times sample and reports the busy share since its last run. That is still a one-minute average, but nothing blocks. The delta formula moves to sys.CPUBusyPercent, which the status page's gopsutil fallback now shares (it had the same formula inline).
  • Each bot gets a stoppableCaller that returns as soon as StopBot closes that bot's stop channel, or as soon as the call's context ends. The bot is now stopped before the event bus, and the screen-art/commands warm-up runs in the background.

Verification

  • Reproduced locally by sending the bot through a SOCKS5 proxy that completes CONNECT and then never relays a byte. Goroutine dumps taken during the outage showed stop() blocked first on cron.Stop() (in CheckCpuJob.Run), then on bus.Stop() (in liveNotice → SendMessage).
  • Restart outage: 60s before → 28s after the first commit → under 1s after the second. Three back-to-back restarts were each handled immediately, and the sub server came back in the same second.
  • New tests, each confirmed red against the old code or under a mutation:
    • TestCheckCpuJobRunDoesNotBlockForTheSamplePeriod: red on the old job, still blocking after 5s.
    • TestCheckCpuJobPublishesUsageSinceThePreviousRun: red with the baseline guard removed (reports 1.5% instead of 75%).
    • TestStopBotEndsInFlightTelegramCall: red with the channel close removed (getMe still blocked 2s after StopBot).
  • go test -shuffle=on ./internal/... green, -race green on the changed packages, golangci-lint 0 issues.

The stop ordering change in web.go and the background warm-up are wiring only. No unit test reaches them; the restart timing above is what demonstrates them.


🔄 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/7029 **Author:** [@Plus98ir](https://github.com/Plus98ir) **Created:** 10/30/2026 **Status:** ✅ Merged **Merged:** 11/2/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/panel-restart-hang` --- ### 📝 Commits (3) - [`fb0507d`](https://github.com/MHSanaei/3x-ui/commit/fb0507ddee8a81daa9ec188bea3aa7e04ea175bc) fix(jobs): stop the CPU alarm job from blocking panel restarts for a minute - [`90f5ccf`](https://github.com/MHSanaei/3x-ui/commit/90f5ccf05834b46df00eec3ac2ce07090a3c5e5c) fix(tgbot): end in-flight Telegram calls when the bot stops - [`5ded2a7`](https://github.com/MHSanaei/3x-ui/commit/5ded2a7bd45696b50bd264371bbf302f405e59cd) fix(jobs): keep iowait out of the CPU alarm's busy share ### 📊 Changes **8 files changed** (+304 additions, -42 deletions) <details> <summary>View changed files</summary> ➕ `internal/util/sys/cpu_times.go` (+36 -0) 📝 `internal/web/job/check_cpu_usage.go` (+21 -8) ➕ `internal/web/job/check_cpu_usage_test.go` (+104 -0) 📝 `internal/web/service/server.go` (+2 -26) ➕ `internal/web/service/tgbot/stoppable_caller.go` (+43 -0) ➕ `internal/web/service/tgbot/stoppable_caller_test.go` (+68 -0) 📝 `internal/web/service/tgbot/tgbot.go` (+26 -5) 📝 `internal/web/web.go` (+4 -3) </details> ### 📄 Description ## Problem Restarting the panel from Settings left it unreachable (HTTP 502 behind nginx) for anywhere from a few seconds to a minute. On a live server the outages measured 4s, 19s and 45s. On a server where Telegram is unreachable it gets worse. `stop()` closes the HTTP listener first and then waits for background work. Two parts of that work could block for a long time: 1. **`CheckCpuJob`** sampled with `cpu.Percent(1*time.Minute)`. Each run blocked for its full `@every 1m` period, so the job was nearly always running, and `cron.Stop()` waits for running jobs. This alone causes a 0–60s outage on every restart, Telegram or not. 2. **Telegram calls** when Telegram is unreachable (TLS handshake timing out). fasthttp honours deadlines but not context cancellation, so: - `bus.Stop()` waited for the Telegram notifier's in-flight `sendMessage` (~30s); - `tgbot.Start` ran `getMe` + `setMyCommands` inline (~60s) on the signal loop, so the sub server started late and a second restart request sat unhandled. ## Fix - `CheckCpuJob` keeps the previous `cpu.Times` sample and reports the busy share since its last run. That is still a one-minute average, but nothing blocks. The delta formula moves to `sys.CPUBusyPercent`, which the status page's gopsutil fallback now shares (it had the same formula inline). - Each bot gets a `stoppableCaller` that returns as soon as `StopBot` closes that bot's stop channel, or as soon as the call's context ends. The bot is now stopped before the event bus, and the screen-art/commands warm-up runs in the background. ## Verification - Reproduced locally by sending the bot through a SOCKS5 proxy that completes CONNECT and then never relays a byte. Goroutine dumps taken during the outage showed `stop()` blocked first on `cron.Stop()` (in `CheckCpuJob.Run`), then on `bus.Stop()` (in `liveNotice → SendMessage`). - Restart outage: **60s before → 28s after the first commit → under 1s after the second**. Three back-to-back restarts were each handled immediately, and the sub server came back in the same second. - New tests, each confirmed red against the old code or under a mutation: - `TestCheckCpuJobRunDoesNotBlockForTheSamplePeriod`: red on the old job, still blocking after 5s. - `TestCheckCpuJobPublishesUsageSinceThePreviousRun`: red with the baseline guard removed (reports 1.5% instead of 75%). - `TestStopBotEndsInFlightTelegramCall`: red with the channel close removed (`getMe` still blocked 2s after `StopBot`). - `go test -shuffle=on ./internal/...` green, `-race` green on the changed packages, `golangci-lint` 0 issues. The stop ordering change in `web.go` and the background warm-up are wiring only. No unit test reaches them; the restart timing above is what demonstrates them. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi changed title from [PR #7029] fix: panel restart left the panel unreachable for up to a minute to [PR #7029] [MERGED] fix: panel restart left the panel unreachable for up to a minute 2026-11-07 14:34: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#6586
No description provided.