Skip to content

Drop rate-limited static invoice requests - #1101

Open
tnull wants to merge 1 commit into
lightningdevkit:mainfrom
tnull:2026-09-fix-static-invoice-rate-limits
Open

tnull wants to merge 1 commit into
lightningdevkit:mainfrom
tnull:2026-09-fix-static-invoice-rate-limits

Conversation

@tnull

@tnull tnull commented Sep 16, 2026 •

Copy link
Copy Markdown
Collaborator

Drop static invoice requests when the rate limit is reached instead of retrying them and delaying later events. Keep retrying storage errors and only acknowledge invoices after saving them.

Fixes an issue reported by @ajaysehwal.

@tnull
tnull requested a review from joostjager September 16, 2026 10:33
@ldk-reviews-bot

ldk-reviews-bot commented Sep 16, 2026 •

Copy link
Copy Markdown

👋 Thanks for assigning @joostjager as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@tnull
tnull force-pushed the 2026-09-fix-static-invoice-rate-limits branch from d393f3e to ce0d9cf Compare September 16, 2026 10:50

@ajaysehwal ajaysehwal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tnull , one gap in thorough test suite: there’s no test covering a sender that gets rate-limited once and then successfully retries after the bucket refills.

Not required for correctness — the existing tests already show that the rate-limited request is dropped cleanly without affecting other requests. But a small test using a short refill interval or deterministic bucket advancement would make the intended contract explicit: the request is dropped, and the sender is free to retry later rather than being blocked permanently.

Purely a documentation-via-test nice-to-have.

@tnull
tnull force-pushed the 2026-09-fix-static-invoice-rate-limits branch from ce0d9cf to 264852e Compare September 16, 2026 12:01

@ajaysehwal ajaysehwal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread src/event.rs
self.channel_manager.static_invoice_persisted(invoice_persisted_path);
},
Err(e) => {
Err(StaticInvoiceStoreError::RateLimited) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we want to skip the rate limiter once we are retrying persistence, so that we don't exit the retry loop because new requests came in?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a fixup for this.

@tnull
tnull force-pushed the 2026-09-fix-static-invoice-rate-limits branch from 264852e to 0033e6b Compare October 16, 2026 13:14
@tnull
tnull requested a review from joostjager October 16, 2026 13:16
}
}

fn refund_on_io_error(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this a bit of a strange function. The "on_io_error" sounds like it checks something, but when this is called, we are already handling an io error?

Also the wrapping seems to be independent.

Isn't it better to just inline this?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't inline, but should be improved.

Drop static invoice requests when the rate limit is reached instead
of retrying them and delaying later events. Keep retrying storage
errors and only acknowledge invoices after saving them.

Requests that fail due to storage errors return their rate-limit
token, so replayed events are not dropped before storage recovers.

Fixes an issue reported by Ajay Sehwal.

Co-Authored-By: HAL 9000
@tnull
tnull force-pushed the 2026-09-fix-static-invoice-rate-limits branch from 0033e6b to cbb52cf Compare November 17, 2026 10:01
@tnull
tnull requested a review from joostjager November 17, 2026 10:02
Comment thread src/event.rs
// Drop silently: logging each rejected request, even at trace level,
// can cause excessive log output and I/O under sustained load.
},
Err(StaticInvoiceStoreError::Io(e)) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A decode failure (InvalidData) also lands here, so a corrupted record gets replayed forever and holds up every later event, though this isn't new with this PR. Since it won't recover, should we log it and drop the request instead, and only replay real storage errors?

Comment thread src/event.rs
},
Err(e) => {
Err(StaticInvoiceStoreError::RateLimited) => {
// Drop silently: logging each rejected request, even at trace level,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude had one thing on this still:

An async recipient comes online and requests offer paths for ~10 cached offer slots, which produces ~10 PersistStaticInvoice events for one recipient_id in one batch. The first 5 are persisted; the rest hit RateLimited and are dropped with no ack. Those slots are not stored until the client's retry timer fires, so payers asking for those slots get no invoice in the meantime. Before this PR the same events were replayed and succeeded about 100ms later.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That mostly means we should align the rate limit to allow for this, no?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's a bit of a weird coupling, but it seems the pragmatic fix for now

}
},
)
.or_else(|e| if e.kind() == lightning::io::ErrorKind::NotFound { Ok(None) } else { Err(e) })

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A persisted invoice that fails to decode still blocks the event queue: the decode error (L123) becomes InvalidData, passes through here as a regular error, surfaces as StaticInvoiceStoreError::Io, and StaticInvoiceRequested returns ReplayEvent for it. With the refund, every replay is admitted, so the event is retried indefinitely and nothing queued behind it (e.g. PaymentClaimable) gets handled until the key is removed by hand.

Since a decode failure won't fix itself on retry, could we treat it as permanent, i.e. log it and return Ok(None) (or remove the entry)? Happy for this to be a follow-up, as it predates this PR.

Comment thread src/event.rs
// Drop silently: logging each rejected request, even at trace level,
// can cause excessive log output and I/O under sustained load.
},
Err(StaticInvoiceStoreError::Io(e)) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For StaticInvoiceRequested, replaying on a storage read error means a slow or temporarily failing store holds up every event behind it, including time-critical ones like HTLC claims. Nothing is lost by dropping the request instead, as the payer can retry. Would it make sense to log and drop on read errors here, and keep ReplayEvent only for PersistStaticInvoice (L2527), where we can't lose the write?

pub(crate) async fn handle_static_invoice_requested(
&self, recipient_id: &[u8], invoice_slot: u16,
) -> Result<Option<(StaticInvoice, BlindedMessagePath)>, StaticInvoiceStoreError> {
if !self.request_rate_limiter.lock().expect("lock").allow(recipient_id) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the request limiter is keyed only by recipient_id (5 tokens, refilled every 100ms), all payers of a recipient share one bucket. Anyone sending more than ~10 requests/s for a public offer keeps it empty, and with this change honest payers' requests for that recipient are then silently dropped.

Not new to this PR, but worth considering whether the limit should also be scoped per requester/reply path, or at least documenting that it is shared across payers.

Comment thread src/event.rs
self.channel_manager.static_invoice_persisted(invoice_persisted_path);
},
Err(e) => {
Err(StaticInvoiceStoreError::RateLimited) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with not logging every rejected request, but right now rate limiting is completely invisible: an operator debugging failed async payments sees nothing at any log level. Could we log once when a recipient's bucket first runs empty (or keep a simple counter), so the condition is observable without per-request logging? Same for the arm at L2563.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants