Repository navigation
Conversation
Two overlapping deliveries of the same Lightning or on-chain renew request could each create a pending payment: the pending lookup and the invoice creation were separate steps, and the lookup only considered renewal rows. An interactive Lightning or on-chain request without a discount code now takes a per-subscription database lock (a named lock on its own connection, released when the connection closes) around the lookup and the creation. Under the lock the request is priced again, and an unpaid, unexpired payment of the same subscription from an earlier manual request, with the same method, payment type, period, currency and amounts and with no credit or discount attached, is returned instead of creating another. The first purchase is included. Requests after that invoice was paid or expired create a new one as before. Saved-method, card, swap, gateway and discounted requests, requests that use account credit, and Lightning requests on an account with an enabled saved wallet take the existing path unchanged. No migration. The renew sections of the API documentation describe the behaviour, and a database test covers the overlap.
douglaz
marked this pull request as ready for review
November 28, 2026 23:03
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A client asks for a Lightning invoice or an on-chain address to pay a subscription, either the first purchase of a new VM or a renewal. The client may repeat the request: a retry after a lost reply, a second click, a second tab. For a first purchase each repeat got another unpaid invoice for the same terms, because the existing lookup for a pending payment only considered renewal rows. For a VM renewal that lookup already returned a pending renewal, but it and the creation of a new invoice were separate steps, so two overlapping requests could each create one.
What this changes
For an interactive Lightning or on-chain request without a discount code, the handler now takes a per-subscription database lock around the lookup and the creation. Under the lock it returns an unpaid, unexpired payment of the same subscription when all of these match:
time_value, which must be non-zero, so only subscriptions with a VM can match);Manual;Otherwise it runs the existing creation path, still under the lock. A second overlapping request on any API process sharing the database waits for the lock, prices the request again and gets the first one's row back if that row still matches on all of the terms above and is still unpaid and unexpired; otherwise it follows the existing renewal path, which prices the request again and can still return a pending renewal from the existing lookup. A request after that invoice was paid or expired creates a new one, as before. If a pending renewal is already found by the pricing engine's existing renewal lookup, it is returned as before, without the lock.
The lock is a MySQL named lock (
GET_LOCK) taken on its own connection outside the pool and held while the payment row is created. It is released when that connection closes, including when the process dies. The locked section runs in a spawned task, so a client that disconnects does not cancel it halfway. The in-process database used by the existing unit tests holds no lock.Scope
Affected routes:
GET /api/v1/vm/{id}/renewandGET /api/v1/subscriptions/{id}/renew, which both reachrenew_subscription_with_discount. No request or response field changes; an in-scope repeat gets back the payment object of the invoice already issued. The "Renew/Extend VM" and "Renew Subscription" sections ofAPI_DOCUMENTATION.mddescribe the behaviour.These take the existing path unchanged:
Gatewaypayment method and any other method (an otherwise eligible on-chain request whose address comes from a configured payment gateway is in scope);Compatibility and database
No migration and no API shape change. Each in-scope request opens one extra database connection, outside the pool, for as long as it holds or waits for the lock. A request that cannot get the lock within 30 seconds fails and creates nothing. The client sees the generic internal error (HTTP 500, "An internal error occurred"); the specific reason, that another payment request for the subscription is in progress, is only logged.
Testing
One new database test,
overlapping_invoice_requests_share_one_pending_invoice, runs two independent pools against one MariaDB 12.3.3 database as two API processes would, with the existing mock Lightning node. It checks that overlapping requests give one pending row and the same invoice with one node call, that a sequential repeat returns it, that a request after expiry gets one new invoice, and that saved-method requests do not wait on the lock or receive the interactive invoice.On the base commit with only this test added, it fails: the overlap created two pending payments. With the change it passes (1 passed, 0 failed). The rest of the test suite was not run for this change.
Like the other database tests, the test runs when
LNVPS_TEST_DATABASE_URLis set and returns early, passing, when it is not. It creates its own uniquely named schema on the server in that URL, so the account needs to be able to create and drop databases; it migrates and seeds that schema and drops it at the end, also when a check fails. To run it, set the variable to a MariaDB database URL and run thelnvps_apilibrary tests filtered topending_renewal::overlapping_invoice_requests_share_one_pending_invoice, with--test-threads=1. With the variable unset it returned early and passed.A signed HTTP run against two API processes sharing one migrated MariaDB, on loopback with stand-in Lightning and on-chain backends. Overlapping Lightning requests shared one payment row and one invoice. A sequential repeat and a restart kept it, and expiry allowed exactly one replacement. Overlapping on-chain requests shared one address, a repeat kept it, and expiry allowed one new address. A saved-method request and a request with an unknown discount code created nothing and gave the same replies as the base build. The same run against the base build created two pending rows and two invoices for the overlap.
This change also runs on a private instance. The records available when this text was written do not show a repeated invoice request there, so no result from that instance is claimed.
The branch was then rebased onto the current
masterwithout conflicts and compile-checked (thelnvps_apilibrary, binaries and tests). The tests were not run again after the rebase.Known limits
A repeat can still get a second unpaid invoice
These are limits of the new matcher, which runs only when the existing renewal lookup finds nothing. In these cases a repeat misses the pending invoice and the existing creation path runs, as before this change.
time_value0, which does not identify the period, so they never match.time_valueis measured in calendar time from now. A repeat on the other side of some UTC midnights (month ends) gets a differenttime_valueand misses. Storing the interval count would fix this and the two cases above.Requests outside the scope (swaps, cards, the
Gatewaypayment method, saved-method requests, requests with a discount code and requests that use account credit) keep their existing behaviour, whatever that is for the method.Other edge cases
methodis resolved to interactive Lightning before this code sees it, so such a request now reuses a pending Lightning invoice. It already got a Lightning invoice before this change.time_valueonly. A request without a discount code can therefore still get a pending renewal created with a discount code or with credit, as it can today.Cost and coverage
max_connections. The subscription renew route has no account rate limit.