Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Get invoice endpoint sometimes returns stale data if you manually mark invoice #5049

Closed
1 task done
pirus111 opened this issue Jun 6, 2023 · 0 comments
Closed
1 task done

Comments

@pirus111
Copy link

pirus111 commented Jun 6, 2023

What is your BTCPay version?

v1.10.0

How did you deploy BTCPay Server?

Docker

What happened?

If you manually mark an invoice through the frontend, and then quickly make a get invoice greenfield API request, sometimes you get stale data back.

How did you encounter this bug?

  1. I created an invoice through the greenfield API
  2. I manually marked the invoice as complete through the btcpay frontend's invoices list page
  3. My configured webhook (on send everything, redelivery on) to my application's endpoint. The received event is not stale (i.e. invoice is complete, and marked)
  4. I then call the greenfield API's get invoice endpoint which returns stale data (i.e. invoice is incomplete, and not marked).

Context: My endpoint does not trust the webhook due to a library limitation in my programming language of choice :(

Relevant log output

n/a

What browser do you use?

Firefox latest

Additional information

The bug is presumably in InvoiceRepository#MarkInvoiceStatus.
You can see in the following that the event is fired before the db context is saved.

invoiceData.Status = legacyStatus.ToLowerInvariant();
invoiceData.ExceptionStatus = InvoiceExceptionStatus.Marked.ToString().ToLowerInvariant();
_eventAggregator.Publish(new InvoiceEvent(ToEntity(invoiceData), eventName)); // <-- event sent before save
await context.SaveChangesAsync(); // <-- now we save, but the event could already have been processed by third parties

We cannot blindly reverse the order, we would have to consider its impact on the system wrt error handling. Since in the above example, if publishing the event fails then the db transaction would be rolled back.

But there's another method in the same repository called UpdateInvoiceExpiry which (at a glance) doesn't handle the situation well.
I would double check my findings since this stuff could be a pain to debug if it goes wrong, and I did not look into whether the event publisher delays sending or sends on demand.

Are you sure this is a bug report?

  • I confirm this is a bug report
@Kukks Kukks closed this as completed in aafb4a7 Jun 7, 2023
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

No branches or pull requests

1 participant