mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 19:11:53 +07:00
[PR #6912] [MERGED] fix(service): run inbound deletes and depleted-client purges on the serial traffic writer #6515
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#6515
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/6912
Author: @muditagrawal2007
Created: 10/23/2026
Status: ✅ Merged
Merged: 10/24/2026
Merged by: @MHSanaei
Base:
main← Head:fix/serialized-writer-invariant📝 Commits (5)
1d970aafix(service): delete inbounds on the serial traffic writer4c96767fix(service): purge depleted clients on the serial traffic writer10501a1Merge branch 'main' into fix/serialized-writer-invariantdf6e7ccMerge remote-tracking branch 'origin/main' into pr/69123795227test(service): keep the serial-writer delete tests with the traffic writer's📊 Changes
3 files changed (+125 additions, -3 deletions)
View changed files
📝
internal/web/service/inbound.go(+1 -1)📝
internal/web/service/inbound_traffic.go(+1 -2)📝
internal/web/service/traffic_writer_test.go(+123 -0)📄 Description
Summary
Deleting an inbound opened its own database transaction, so it could run outside the panel's single serial traffic writer. It now goes through
runSerializedTx, the same helper every other inbound write already uses. A second commit closes the same escape hatch in the depleted-client purge.delInbounddb.TransactionrunSerializedTx→ queued on the traffic writerDelDepletedClientsdb.TransactionrunSerializedTx→ queued on the traffic writerWhy
The panel serialises inbound and traffic writes on one goroutine for a documented reason —
internal/web/service/traffic_writer.go:117-123:1 · The inbound delete — operator-reachable
internal/web/service/inbound.go:1601(on currentmain::1651)client_inboundsfirst —DetachInbound(internal/web/service/client_link.go:459) — and only then theinboundsrow.autoRenewClientssaves the loaded inbounds first (internal/web/service/inbound_traffic.go:537) and rewrites the client links after (:550,SyncInbound); the traffic-sync path does the same (:339→:353).Two transactions taking the same two tables in opposite orders is a deadlock cycle on Postgres — one of them is aborted with
SQLSTATE 40P01. Putting the delete on the same queue as the poll removes the cycle by construction.A second, independent failure mode. When the delete commits between a poll's (or an edit's) read and its
tx.Save, theUPDATEmatches zero rows — and GORM'sSavefalls back to aCREATE, so the inbound the operator deleted comes back. Verified against the module version this repo pins (gorm.io/gorm v1.31.2,finisher_api.go):(The SQLite build cannot show this: it holds a single database write lock, so the delete waits instead of interleaving.)
2 · The depleted-client purge — a latent escape hatch, not an operator-reachable bug
internal/web/service/inbound_traffic.go:902opened its own transaction over the same two tables. To be precise about its reach: it has no production caller. The panel's "Delete depleted" button goes toClientService.DelDepleted(internal/web/service/client_bulk.go:1525) →BulkDelete→ already on the writer (client_bulk.go:987); only tests callInboundService.DelDepletedClients.This commit removes the last inbound/traffic writer in that file that bypasses the single writer, so the invariant holds for every path in it — including any future caller. No operator-visible behaviour changes.
Scope
Two other writers touch
inboundsoutside the writer:SetInboundSubSortIndex(internal/web/service/inbound.go:1739) andSetInboundEnable(:1795). They writeinboundsand thennodes— the order the poll uses — so they are not part of the cycle above and are deliberately left untouched.The PostgreSQL deadlock is inferred from the lock order, not reproduced: the repo has no Postgres-based concurrency reproduction, and the always-on test asserts the invariant (the write reaches the writer) rather than the deadlock itself.
Evidence — red before, green after
Type of change
Areas affected
How was this tested?
Two new tests in
internal/web/service/serialized_writer_invariant_test.go:TestDelInboundRunsOnTheSerialWriter— parks the traffic writer inside an open transaction, starts the delete, and asserts the write was handed to the writer's queue instead of running on a goroutine of its own; then asserts the inbound row is gone.TestDelDepletedClientsRunsOnTheSerialWriter— same shape for the purge; then asserts the depleted inbound and itsclient_trafficsrow are gone.Both fail on the current code with the reason shown above and pass with the fix. The writer queue is the thing under test, so the assertion cannot pass by accident.
Commands run locally:
Notes for the reviewer
TestRouteRegistryContractand the locale-key test are unaffected by construction.DelInboundscallsdelInboundper id, so it is serialised as well; the writer drains one request at a time, so a bulk delete cannot overflow the queue.DetachInboundandMarkNodeDirtyTx(internal/web/service/node.go:1043) are plain statements; the node push and the runtime cleanup still run after the commit (internal/web/service/inbound.go:1634).submitTrafficWritestill falls back to running inline when the writer was never started (internal/web/service/traffic_writer.go:162). During shutdown the cron is stopped before the writer (internal/web/web.go:776→:801), so no traffic poll can be running at that point, and an existing test pins that behaviour — changing it would be a contract change, not a fix.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.