mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #6491] [MERGED] fix(tgbot): read the admin list and running flag under their mutex #6304
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#6304
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/MHSanaei/3x-ui/pull/6491
Author: @BlindMaster24
Created: 9/12/2026
Status: ✅ Merged
Merged: 9/13/2026
Merged by: @MHSanaei
Base:
main← Head:fix/tgbot-admin-list-race📝 Commits (1)
5b1c3a2fix(tgbot): read the admin list and running flag under their mutex📊 Changes
5 files changed (+90 additions, -7 deletions)
View changed files
📝
internal/web/service/tgbot/tgbot.go(+8 -0)➕
internal/web/service/tgbot/tgbot_admin_list_race_test.go(+73 -0)📝
internal/web/service/tgbot/tgbot_report.go(+4 -3)📝
internal/web/service/tgbot/tgbot_router.go(+1 -1)📝
internal/web/service/tgbot/tgbot_send.go(+4 -3)📄 Description
Summary
adminIdsis replaced undertgBotMutexbyStart/Stop, but five readerstook it without the lock. They now read a snapshot under the same mutex, and
SendMsgToTgbotuses the existingIsRunning()accessor instead of reading theisRunningflag directly.Why
Writers, both holding
tgBotMutex:Start(tgbot.go:290) — assigns the parsed chat IDsStop(tgbot.go:460) — setsadminIds = nil; reached by the bot settingsreload, and by panel shutdown through
main.go's signal handlerReaders, none of them holding it:
checkAdmin(tgbot_router.go:1324) — every callback and command the botreceives runs through it, on the 10-goroutine worker pool
SendMsgToTgbotAdmins(tgbot_send.go:182) — every event-bus notificationSendBackupToAdmins(tgbot_report.go:56) andsendExhaustedToAdmins(
tgbot_report.go:70) — the Report / backup cronWhat the operator meets
An operator opens Settings → Telegram bot, fixes the Chat ID and saves. The
panel reloads the bot (
SetReloadTgbotFunc), which callsStop()→adminIds = nil, while the Report cron scheduled at the same second is insideSendReport. Two things can then go wrong, and both are visible:while it is replaced can observe a new pointer with the old length (or the
reverse), so
slices.Contains/rangewalks a length that does not belongto the array it is reading: the report or the DB backup goes to nobody, to
some admins, or to a stale set. The outcome is non-deterministic and nothing
logs an error.
SendBackupToAdminssleeps onesecond between uploads by comparing against
len(adminIds) - 1inside theloop (
tgbot_report.go:59). Once the list is nil'd mid-loop, that expressionis
-1, the sleep is skipped for every remaining admin, and the uploads goout back-to-back — the exact burst the sleep exists to avoid, hitting the
same Telegram rate limit as
#6340and#6490.-racereports the read regardless of which symptom an operator happens to hit;the
racejob runs it on every PR.Scope
isRunninghad the same shape — written under the mutex byOnReceive(
tgbot_router.go:68) andStopBot(tgbot.go:473), read unlocked bySendMsgToTgbot(tgbot_send.go:124) — so that read now uses the accessor thepackage already has. The telego client pointer
bothas the same problem inabout a dozen places, but it has no accessor yet and every reader would have to
move to it; that is a separate change and is left out of this one.
Type of change
Areas affected
How was this tested?
internal/web/service/tgbot/tgbot_admin_list_race_test.goruns one writer thatreassigns
adminIdsandisRunningundertgBotMutex— the same lock orderStart/Stopuse — against four goroutines callingcheckAdmin,IsRunning()and
SendMsgToTgbot, the last being the reader this PR changed and which had nocoverage before. The
adminIdshalf trips the detector throughcheckAdmin; theisRunninghalf does so throughSendMsgToTgbot, which would stay green under-raceif only this test'sIsRunning()calls were left in. A mocked TelegramAPI backs
SendMsgToTgbotso the live path stays cheap.go test -raceis what CI'sracejob runs. The test cannot fail without it —that is inherent to a race, and the race job covers it.
Screenshots / recordings
N/A — no UI.
Breaking changes
None. Same behaviour for every caller, and the backup pacing now uses the
snapshot's length instead of a list that may be replaced mid-loop.
Checklist
-race.go build ./...and the test suite pass locally. — same reason as above.npm run lint,npm run typecheck, andnpm run buildpass. — no frontend files touched.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.