mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[GH-ISSUE #6300] [Bug]: Calendar auto-renew mishandles end-of-month 23:59:59 expiries #4277
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#4277
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?
Originally created by @JacktheRanger on GitHub (Aug 24, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6300
Describe the bug
Calendar auto-renew introduced by #6239 correctly prevents fixed-day drift, but it does not normalize the common inclusive end-of-month expiry convention.
For a provider whose billing window turns over on day 1 at 00:00, operators commonly store the visible client expiry as the previous month's final second (for example,
2026-08-31 23:59:59). WithresetDay = 1, the renewal loop can treat the one-second move to2026-09-01 00:00:00as one renewal and then require another renewal to reach the next future boundary.This also exposes a presentation mismatch: the internal exclusive renewal boundary is the first day of the next month, while users normally expect clients to show the inclusive "valid through" date as the previous month's last day.
This is a follow-up to #6106 and #6239, not a request to return to fixed 30-day renewal.
How to reproduce
Use panel time zone UTC on 3x-ui v3.7.0.
2026-08-31 23:59:590resetDay):1resetMax):12026-09-01 00:00:00.autoRenewClients.The first step advances the expiry to
2026-09-01 00:00:00, which is already in the past by the time the job runs. Reaching2026-10-01 00:00:00requires a second loop step. WithresetMax = 1, the allowance can be exhausted at the already-passed boundary and the client remains expired. With an unlimited cap, the client reaches October butresetCountcan increase twice for one intended monthly renewal.Relevant code:
Expected behavior
For a day-1 calendar plan:
2026-08-31 23:59:59and the exclusive boundary2026-09-01 00:00:00should represent the same period end for renewal purposes.2026-10-01 00:00:00.resetCountshould increase exactly once.resetMax = 1should grant one complete next calendar period rather than one second.2026-09-30 23:59:59, while scheduling and traffic reset remain aligned to the internal October 1 midnight boundary.Actual behavior
An inclusive end-of-month timestamp is treated as a distinct expiry before the day-1 calendar boundary. The first calendar step can land on an already-passed boundary, consuming an allowance before the client receives the intended next monthly period.
The subscription
expirevalue also exposes the internal first-of-month boundary directly, so a correctly aligned period is displayed as October 1 rather than "valid through September 30."Suggested direction
Keep day 1 at 00:00 in the panel time zone as the canonical internal renewal boundary. Normalize an immediately preceding end-of-day expiry to that boundary for renewal accounting, and derive an inclusive display value separately instead of moving the renewal schedule to day 31.
Please add coverage for:
23:59:59+resetDay = 1;Environment
Checklist
@github-actions[bot] commented on GitHub (Aug 24, 2026):
Confirmed for the renewal-accounting half of this report: with
resetDayset and an expiry stored just before the billing-day midnight,autoRenewClientsspends a full renewal allowance on a step worth one second, and withresetMax = 1the client is left permanently expired. The subscriptionexpirehalf is not a defect — detail below. Still present on the default branch (f727d04f, tagged v3.7.0).Where it goes wrong
The date helper is not the problem.
nextCalendarRenewalis documented as returning "the next renewal strictly after from" (internal/web/service/calendar_renew.go:5-7), so from2026-08-31 23:59:59withday = 1it returns2026-09-01 00:00:00and not October. That is deliberate and pinned byTestNextCalendarRenewal_ClampsToShortMonths/"later in the same month renews this month"(internal/web/service/calendar_renew_test.go:46-51): an unaligned expiry is meant to be pulled onto the billing calendar first.The accounting around it is where the harm is, in
internal/web/service/inbound_traffic.go:442-455:Trace of your case (
resetDay = 1,resetMax = 1,resetCount = 0, expiry2026-08-31 23:59:59, panel zone UTC). One correction to the repro, which does not change your conclusion:nowis truncated to whole seconds at line 333 (now := time.Now().Unix() * 1000), so the tick that falls inside the expiry's own second does nothing —newExpiryTime < nowis false and line 456if renewals == 0 { continue }takes it out. The pass runs at least every 5 s (internal/web/web.go:293,cadenceXrayTraffic = "@every 5s"), so the tick that does act is already past midnight:2026-09-01 00:00:00,renewals = 1;< now), the cap check0+1 >= 1breaks;traffic.ResetCount += renewals→1, the single allowance is gone;if newExpiryTime <= nowtakes the "cap ran out mid-catch-up" branch: not re-enabled,up/downnot zeroed. The client stays expired with an expiry one second past where it started.With
resetMax = 0the loop takes a second step to2026-10-01 00:00:00in the same pass, so the expiry ends up right butresetCountgains 2 for one month — which then bites anyone who caps that client later.Both outcomes contradict the loop's own stated rule two lines above it — "One allowance per period, not per tick" (line 432-433) and "step whole months" (line 447). The alignment step here is one second, not a period.
Scope — narrower than the report implies
The damage needs the next boundary to be at or before
nowwhen the pass runs, i.e. an expiry within roughly a tick of the billing-day midnight. A mid-period unaligned expiry is fine:2026-08-20 12:00withresetDay = 1renews to2026-09-01 00:00:00while that is still in the future, so the client is re-enabled with counters zeroed and one allowance buys the alignment — the intended behaviour. So this is specifically the inclusive end-of-day convention, not unaligned expiries in general.Offline catch-up is also unaffected and should stay as it is:
TestAutoRenewClients_CalendarModeSpendsOneAllowancePerMonth(internal/web/service/inbound_autorenew_calendar_test.go:346-391) pins one allowance per month stepped and a truncated catch-up leaving the client expired.Not a defect: the subscription
expireinternal/sub/controller.go:426emitsThat is the instant access actually ends, which is what
subscription-userinfomeans; a client valid until2026-10-01 00:00:00is correctly reported as expiring then. An inclusive "valid through2026-09-30 23:59:59" display is a presentation feature, independent of the renewal defect, and is better filed on its own than bundled into this fix.Workaround today
Store the expiry as the billing-day midnight itself —
2026-09-01 00:00:00forresetDay = 1— instead of the previous day's23:59:59. That path is already correct:nextCalendarRenewal(2026-09-01 00:00:00, 1, UTC)returns2026-10-01 00:00:00, because the same-day candidate is rejected byif !candidate.After(local)(internal/web/service/calendar_renew.go:19-26) — one allowance, one full month.For a client already stuck, nothing clears
reset_count(its only writers areinbound_traffic.go:461and the node sync atinbound_node.go:959), so raise Maximum renewals (resetMax) by one in the client dialog and set the expiry to the boundary midnight.Verdict: Confirmed bug
Severity: Medium
Confidence: High
Root cause:
internal/web/service/inbound_traffic.go,autoRenewClients— the catch-up loop at 442-455 together with the already-past guard at 462. AresetDayclient whose expiry sits just before its billing-day midnight is charged a full allowance at line 461 for a one-second step, and when that exhaustsresetMaxthe 462 branch leaves it expired; uncapped, the same misalignment incrementsresetCounttwice for one month. Introduced byb8903fadfeat(clients): renew on a calendar day instead of a rolling interval (#6239).Already fixed: Still present on the default branch (
f727d04f, tagged v3.7.0)Duplicate of: None. Related: #6106 and #6239, the feature this follows up
Evidence:
inbound_traffic.go:333now := time.Now().Unix() * 1000(second granularity);:432-433"One allowance per period, not per tick";:442-455the step loop;:461traffic.ResetCount += renewals;:462-467the "cap ran out mid-catch-up" branch;calendar_renew.go:5-7"returns the next renewal strictly after from" and:19-26if !candidate.After(local);calendar_renew_test.go:46-51pins the same-month partial step as intended;inbound_autorenew_calendar_test.go:346-391pins one allowance per month stepped;web.go:293cadenceXrayTraffic = "@every 5s";sub/controller.go:426expire=istraffic.ExpiryTime/1000Not determined: Which path sets a
23:59:59expiry in practice. The panel's own picker isYYYY-MM-DD HH:mm:ss(frontend/src/components/form/DateTimePicker.tsx:53), so it is typeable but not a default, and a date picked without touching the time is midnight. Saying whether these expiries come from the panel UI or from an external billing script/bot through the API settles how common this is. I read the source only — I cannot build or run the panel or its tests here.Suggested fix: the smallest change looks like a pre-loop normalisation in
autoRenewClients— whentraffic.ResetDay > 0and the expiry falls inside the last day before its own billing-day midnight, snapat/newExpiryTimeup to that boundary without counting a renewal, so the first charged step lands a full month on. That leavesnextCalendarRenewal, interval mode and the multi-month catch-up accounting untouched, soTestAutoRenewClients_CalendarModeSpendsOneAllowancePerMonthstays green. The inclusive "valid through" display is a separate change and is not needed to fix this.This reply was generated automatically; a maintainer may follow up.
@JacktheRanger commented on GitHub (Sep 14, 2026):
Thanks for fixing the renewal-accounting bug in #6457.
From the final diff (
f2481ed, merged as5cce246), the free normalization now applies only to the last second before the billing midnight, not the entire preceding day. The added tests coverresetMax = 1, unlimited renewals, and preservation of ordinary mid-day alignment behavior. That addresses the extra allowance spent on the23:59:59input; I am not asking to reopen this accounting bug. This assessment is from source review and the final green CI, not a live-panel test.I also want to clarify the remaining part of my original use case: AWS/Azure monthly lines should renew on the first of each month without calendar drift, while the client-visible period is presented as valid through the previous month's last day, e.g. real cutoff
2026-10-01 00:00:00versus inclusive display2026-09-30 23:59:59, in the same billing/panel timezone.I agree that exporting the real cutoff as subscription
expireis not itself a protocol bug. The final PR leaves that presentation unchanged, so I have filed the inclusive "Valid through" display as a separate feature request: https://github.com/MHSanaei/3x-ui/issues/6516. A derived panel/remark-template field could preserve existing defaults and backend scheduling; however, it would not by itself change a subscription client's native expiry field. That native-field goal needs an explicit decision about client support or a documented opt-in presentation mode, not an unconditional one-second subtraction.It may also help to document that the fix does not automatically refund renewal counts already consumed by the old bug, so already-exhausted capped clients can still need operator intervention.
Thanks again — the accounting fix and the display enhancement should remain separate.