Merge pull request #7780 from tumutech/fix-inventory-counts
What changed, and why it matters
This commit fixes a bug where BTCPay Server's Point of Sale and Crowdfund inventory counts could become wrong after unpaid or expired invoices. Previously, the system could add inventory back multiple times for the same invoice, making it look like more items were available than actually were. The fix tracks the invoice's previous status so inventory is only adjusted once. The commit also adds a way for tests to wait until background event processing is finished.
Review the inventory-updater logic to confirm no other event pairs can double-adjust inventory, and consider adding an idempotency guard (e.g., processed-event tracking) for production safety beyond status checks. The new WaitForPendingEventsAsync helper should be used in tests that assert on asynchronous event-driven side effects.
Security signals we found
Business-logic flaw: inventory counts could be inflated by duplicate restocking events
State-machine fix: previous status now tracked to prevent double-counting
Race/ordering concern mitigated by deterministic event-barrier for tests
No input validation, authentication, cryptography, or injection changes observed
Evidence from the diff
The patch addresses an inventory-count inconsistency in AppInventoryUpdaterHostedService. It adds a PreviousStatus property to InvoiceEvent and populates it at all invoice state transitions (Created, PaidInFull, Confirmed, Completed, Expired, ExpiredPaidPartial, FailedToConfirm, MarkedCompleted, MarkedInvalid). The inventory updater now ignores Expired/MarkedInvalid events whose PreviousStatus is already Expired, preventing duplicate restocking. A new WaitForPendingEventsAsync barrier is added to EventHostedServiceBase to allow tests to await queued event handlers deterministically. Extensive tests verify status transitions and inventory behavior.
Changed components
BTCPayServer/HostedServices/AppInventoryUpdaterHostedService.csBTCPayServer/HostedServices/EventHostedServiceBase.csBTCPayServer/HostedServices/InvoiceWatcher.csBTCPayServer/Services/Invoices/InvoiceRepository.csBTCPayServer/Events/InvoiceEvent.csInspect captured patch +187 / −16
### BTCPayServer.Tests/BitpayTests.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Concurrent;
using System.Linq;
using System.Net;
using System.Net.Http;
@@ -115,6 +116,9 @@ public async Task CanSendIPN()
await acc.GrantAccessAsync();
acc.RegisterDerivationScheme("BTC");
await acc.ModifyGeneralSettings(p => p.SpeedPolicy = SpeedPolicy.LowSpeed);
+ var invoiceEvents = new ConcurrentQueue<InvoiceEvent>();
+ using var subscription = tester.PayTester.GetService<EventAggregator>()
+ .Subscribe<InvoiceEvent>(evt => invoiceEvents.Enqueue(evt));
var invoice = await acc.BitPay.CreateInvoiceAsync(new Invoice
{
Price = 5.0m,
@@ -171,6 +175,15 @@ public async Task CanSendIPN()
}
var invoice2 = await acc.BitPay.GetInvoiceAsync(invoice.Id);
Assert.NotNull(invoice2);
+ var events = invoiceEvents.Where(evt => evt.InvoiceId == invoice.Id).ToArray();
+ Assert.Null(Assert.Single(events, evt => evt.Name == InvoiceEvent.Created).PreviousStatus);
+ Assert.Null(Assert.Single(events, evt => evt.Name == InvoiceEvent.ReceivedPayment).PreviousStatus);
+ Assert.Equal(InvoiceStatus.New,
+ Assert.Single(events, evt => evt.Name == InvoiceEvent.PaidInFull).PreviousStatus);
+ Assert.Equal(InvoiceStatus.Processing,
+ Assert.Single(events, evt => evt.Name == InvoiceEvent.Confirmed).PreviousStatus);
+ Assert.Equal(InvoiceStatus.Processing,
+ Assert.Single(events, evt => evt.Name == InvoiceEvent.Completed).PreviousStatus);
}
[Fact]
### BTCPayServer.Tests/DatabaseTests.cs
@@ -60,16 +60,24 @@ public async Task MarkInvoiceStatusReportsFailures()
}
await ctx.SaveChangesAsync();
- var published = new System.Collections.Generic.List<string>();
- using var sub = events.Subscribe<InvoiceEvent>(e => published.Add(e.Name));
+ var published = new System.Collections.Generic.List<InvoiceEvent>();
+ using var sub = events.Subscribe<InvoiceEvent>(e => published.Add(e));
// Transition not allowed: reported as false, nothing published
Assert.False(await invoiceRepository.MarkInvoiceStatus("Settled1", InvoiceStatus.Settled));
Assert.Empty(published);
// Allowed transition: true, published once
Assert.True(await invoiceRepository.MarkInvoiceStatus("New1", InvoiceStatus.Settled));
- Assert.Equal([InvoiceEvent.MarkedCompleted], published);
+ var completed = Assert.Single(published);
+ Assert.Equal(InvoiceEvent.MarkedCompleted, completed.Name);
+ Assert.Equal(InvoiceStatus.New, completed.PreviousStatus);
+ published.Clear();
+
+ Assert.True(await invoiceRepository.MarkInvoiceStatus("New1", InvoiceStatus.Invalid));
+ var invalid = Assert.Single(published);
+ Assert.Equal(InvoiceEvent.MarkedInvalid, invalid.Name);
+ Assert.Equal(InvoiceStatus.Settled, invalid.PreviousStatus);
published.Clear();
// The save fails: the exception reaches the caller and no event is published
### BTCPayServer.Tests/EventHostedServiceTests.cs
@@ -0,0 +1,65 @@
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+using BTCPayServer.HostedServices;
+using BTCPayServer.Logging;
+using Xunit;
+
+namespace BTCPayServer.Tests
+{
+ public class EventHostedServiceTests
+ {
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ [Trait("Fast", "Fast")]
+ public async Task CanCancelBarrierWhileEventIsProcessing(bool cancelBeforeWaiting)
+ {
+ var logs = new Logs();
+ using var events = new EventAggregator(logs);
+ var service = new BlockingEventService(events, logs);
+ await service.StartAsync(CancellationToken.None);
+ try
+ {
+ service.Enqueue();
+ await service.Started.Task.WaitAsync(TimeSpan.FromSeconds(5));
+ using var cts = new CancellationTokenSource();
+ if (cancelBeforeWaiting)
+ cts.Cancel();
+ var cancelledBarrier = service.WaitForPendingEventsAsync(cts.Token);
+ cts.Cancel();
+ await Assert.ThrowsAnyAsync<OperationCanceledException>(
+ () => cancelledBarrier.WaitAsync(TimeSpan.FromSeconds(5)));
+
+ // Cancelling one wait must not release the handler or cancel later barriers.
+ var pendingBarrier = service.WaitForPendingEventsAsync();
+ Assert.False(pendingBarrier.IsCompleted);
+ service.Release.TrySetResult();
+ await pendingBarrier.WaitAsync(TimeSpan.FromSeconds(5));
+ Assert.Equal(1, service.ProcessedEvents);
+ }
+ finally
+ {
+ service.Release.TrySetResult();
+ await service.StopAsync(CancellationToken.None);
+ }
+ }
+
+ private sealed class BlockingEventService(EventAggregator events, Logs logs)
+ : EventHostedServiceBase(events, logs)
+ {
+ public TaskCompletionSource Started { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
+ public TaskCompletionSource Release { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
+ public int ProcessedEvents { get; private set; }
+
+ public void Enqueue() => PushEvent(new object());
+
+ protected override async Task ProcessEvent(object evt, CancellationToken cancellationToken)
+ {
+ Started.TrySetResult();
+ await Release.Task;
+ ProcessedEvents++;
+ }
+ }
+ }
+}
### BTCPayServer.Tests/POSTests.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net;
@@ -10,6 +11,7 @@
using BTCPayServer.Client.Models;
using BTCPayServer.Controllers;
using BTCPayServer.Data;
+using BTCPayServer.Events;
using BTCPayServer.Forms;
using BTCPayServer.Forms.Models;
using BTCPayServer.HostedServices;
@@ -27,6 +29,7 @@
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Primitives;
using Microsoft.Playwright;
using NBitcoin;
@@ -358,13 +361,64 @@ public async Task CanUsePoSApp1()
store.AllowZeroAmountInvoices = true;
await client.UpdateStore(store.Id, store);
- var redirectToCheckout = Assert.IsType<RedirectToActionResult>(publicApps.ViewPointOfSale(app.Id, PosViewType.Cart, 0, choiceKey: "goodies").Result);
+ var invoiceEvents = new ConcurrentQueue<InvoiceEvent>();
+ using var invoiceEventsSubscription = tester.PayTester.GetService<EventAggregator>()
+ .Subscribe<InvoiceEvent>(evt => invoiceEvents.Enqueue(evt));
+ RedirectToActionResult redirectToCheckout = null;
+ var completedInvoice = await tester.WaitForEvent<InvoiceEvent>(async () =>
+ {
+ redirectToCheckout = Assert.IsType<RedirectToActionResult>(
+ await publicApps.ViewPointOfSale(app.Id, PosViewType.Cart, 0, choiceKey: "goodies"));
+ }, evt => evt.Name == InvoiceEvent.Completed && evt.Invoice.Metadata.ItemCode == "goodies");
Assert.Equal("InvoiceReceipt", redirectToCheckout.ActionName);
var invoiceId = redirectToCheckout.RouteValues!["invoiceId"]!.ToString();
+ Assert.Equal(invoiceId, completedInvoice.InvoiceId);
+ var zeroAmountEvents = invoiceEvents.Where(evt => evt.InvoiceId == invoiceId).ToArray();
+ Assert.Null(Assert.Single(zeroAmountEvents, evt => evt.Name == InvoiceEvent.Created).PreviousStatus);
+ Assert.Equal(InvoiceStatus.New,
+ Assert.Single(zeroAmountEvents, evt => evt.Name == InvoiceEvent.PaidInFull).PreviousStatus);
+ Assert.Equal(InvoiceStatus.Processing,
+ Assert.Single(zeroAmountEvents, evt => evt.Name == InvoiceEvent.Confirmed).PreviousStatus);
+ Assert.Equal(InvoiceStatus.Processing, completedInvoice.PreviousStatus);
var inv = await client.GetInvoice(invoiceId);
Assert.Equal(0, inv.Amount);
Assert.NotEqual(InvoiceType.TopUp, inv.Type);
+ // marking an expired invoice invalid should not increment the inventory
+ vmpos = await pos.UpdatePointOfSale(app.Id).AssertViewModelAsync<UpdatePointOfSaleViewModel>();
+ vmpos.Template = AppService.SerializeTemplate(TestUtils.ParsePOSYML(@"
+inventoryitem:
+ price: 1.0
+ title: good apple
+ inventory: 1
+"));
+ Assert.IsType<RedirectToActionResult>(pos.UpdatePointOfSale(app.Id, vmpos).Result);
+ var inventoryRedirect = Assert.IsType<RedirectToActionResult>(await publicApps
+ .ViewPointOfSale(app.Id, PosViewType.Cart, 1, choiceKey: "inventoryitem"));
+ Assert.Equal("UIInvoice", inventoryRedirect.ControllerName);
+ var inventoryInvoiceId = inventoryRedirect.RouteValues!["invoiceId"]!.ToString();
+ var appsService = tester.PayTester.GetService<AppService>();
+ async Task<int?> ItemInventory()
+ {
+ var fresh = await appsService.GetApp(app.Id, PointOfSaleAppType.AppType);
+ return AppService.Parse(fresh!.GetSettings<PointOfSaleSettings>().Template)
+ .Single(item => item.Id == "inventoryitem").Inventory;
+ }
+ var inventoryUpdater = tester.PayTester.GetService<IEnumerable<IHostedService>>()
+ .OfType<AppInventoryUpdaterHostedService>().Single();
+ await inventoryUpdater.WaitForPendingEventsAsync();
+ Assert.Equal(0, await ItemInventory());
+ var expiredInvoice = await tester.WaitForEvent<InvoiceEvent>(
+ () => tester.PayTester.InvoiceRepository.UpdateInvoiceExpiry(inventoryInvoiceId, TimeSpan.Zero),
+ evt => evt.InvoiceId == inventoryInvoiceId && evt.Name == InvoiceEvent.Expired);
+ Assert.Equal(InvoiceStatus.New, expiredInvoice.PreviousStatus);
+ await inventoryUpdater.WaitForPendingEventsAsync();
+ Assert.Equal(1, await ItemInventory());
+ var invoiceController = tester.PayTester.GetController<UIInvoiceController>(user.UserId, user.StoreId);
+ Assert.IsType<JsonResult>(await invoiceController.ChangeInvoiceState(inventoryInvoiceId, "invalid"));
+ await inventoryUpdater.WaitForPendingEventsAsync();
+ Assert.Equal(1, await ItemInventory());
+
// List
appList = Assert.IsType<ListAppsViewModel>(Assert.IsType<ViewResult>(apps.ListApps(user.StoreId).Result).Model);
app = appList.Apps[0];
### BTCPayServer.Tests/UnitTest1.cs
@@ -821,14 +821,13 @@ public async Task CanSaveKeyPathForOnChainPayments()
await user.RegisterDerivationSchemeAsync("BTC");
var invoice = await user.BitPay.CreateInvoiceAsync(new Invoice(0.01m, "BTC"));
- await tester.WaitForEvent<InvoiceEvent>(async () =>
+ var paidInFull = await tester.WaitForEvent<InvoiceEvent>(async () =>
{
await tester.ExplorerNode.SendToAddressAsync(
BitcoinAddress.Create(invoice.BitcoinAddress, Network.RegTest),
Money.Coins(0.01m));
- });
-
-
+ }, evt => evt.InvoiceId == invoice.Id && evt.Name == InvoiceEvent.PaidInFull);
+ Assert.Equal(InvoiceStatus.New, paidInFull.PreviousStatus);
var payments = Assert.IsType<InvoiceDetailsModel>(
Assert.IsType<ViewResult>(await user.GetController<UIInvoiceController>().Invoice(invoice.Id)).Model)
### BTCPayServer.Tests/WebhooksTests.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Concurrent;
using System.Linq;
using System.Net.Http;
using System.Text;
@@ -274,6 +275,9 @@ public async Task EnsureWebhooksInvoiceExpiredPaidLatePartial()
user.RegisterDerivationScheme("BTC");
await user.SetupWebhook();
var client = await user.CreateClient();
+ var invoiceEvents = new ConcurrentQueue<InvoiceEvent>();
+ using var subscription = tester.PayTester.GetService<EventAggregator>()
+ .Subscribe<InvoiceEvent>(evt => invoiceEvents.Enqueue(evt));
// PaidAfterExpiration
var invoicePaidAfterExpiration = await client.CreateInvoice(user.StoreId, new CreateInvoiceRequest() { Amount = 0.01m, Currency = "BTC" });
@@ -317,6 +321,15 @@ await user.AssertHasWebhookEvent(WebhookEventType.InvoiceExpiredPaidPartial, (We
{
Assert.Equal(invoiceExpiredPartial.Id, evt.InvoiceId);
});
+ var latePaymentEvents = invoiceEvents.Where(evt => evt.InvoiceId == invoicePaidAfterExpiration.Id).ToArray();
+ Assert.Equal(InvoiceStatus.New,
+ Assert.Single(latePaymentEvents, evt => evt.Name == InvoiceEvent.Expired).PreviousStatus);
+ Assert.Null(Assert.Single(latePaymentEvents, evt => evt.Name == InvoiceEvent.PaidAfterExpiration).PreviousStatus);
+ var partialPaymentEvents = invoiceEvents.Where(evt => evt.InvoiceId == invoiceExpiredPartial.Id).ToArray();
+ Assert.Equal(InvoiceStatus.New,
+ Assert.Single(partialPaymentEvents, evt => evt.Name == InvoiceEvent.Expired).PreviousStatus);
+ Assert.Equal(InvoiceStatus.New,
+ Assert.Single(partialPaymentEvents, evt => evt.Name == InvoiceEvent.ExpiredPaidPartial).PreviousStatus);
}
### BTCPayServer/Events/InvoiceEvent.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using BTCPayServer.Client.Models;
using BTCPayServer.Services.Invoices;
namespace BTCPayServer.Events
@@ -76,6 +77,10 @@ public InvoiceEvent(InvoiceEntity invoice, string name)
/// Only set for Refund event
/// </summary>
public string PullPaymentId { get; set; }
+ /// <summary>
+ /// Status before the transition represented by this event; null for events that do not change status.
+ /// </summary>
+ public InvoiceStatus? PreviousStatus { get; set; }
public override string ToString()
{
### BTCPayServer/HostedServices/AppInventoryUpdaterHostedService.cs
@@ -35,6 +35,8 @@ protected override async Task ProcessEvent(object evt, CancellationToken cancell
{
case InvoiceEvent.Expired:
case InvoiceEvent.MarkedInvalid:
+ if (invoiceEvent.PreviousStatus is InvoiceStatus.Expired)
+ return;
deduct = 1;
break;
case InvoiceEvent.Created:
### BTCPayServer/HostedServices/EventHostedServiceBase.cs
@@ -49,7 +49,8 @@ public async Task ProcessEvents(CancellationToken cancellationToken)
try
{
using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, e.CancellationToken);
- await ProcessEvent(e.Event, linkedCts.Token);
+ if (e.Event is not EventBarrier)
+ await ProcessEvent(e.Event, linkedCts.Token);
e.Tcs.TrySetResult();
}
catch (OperationCanceledException pce) when (e.CancellationToken.IsCancellationRequested || cancellationToken.IsCancellationRequested)
@@ -104,6 +105,14 @@ protected void PushEvent(object obj)
}
record ExecutingEvent(object Event, TaskCompletionSource Tcs, CancellationToken CancellationToken);
+ private sealed record EventBarrier;
+
+ /// <summary>
+ /// Waits for events already queued on this service to finish processing.
+ /// </summary>
+ public Task WaitForPendingEventsAsync(CancellationToken cancellationToken = default)
+ => RunEvent(new EventBarrier()).WaitAsync(cancellationToken);
+
protected Task RunEvent(object obj, CancellationToken cancellationToken = default)
{
var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
### BTCPayServer/HostedServices/InvoiceWatcher.cs
@@ -84,9 +84,9 @@ private void UpdateInvoice(UpdateInvoiceContext context)
context.MarkDirty();
invoice.Status = InvoiceStatus.Expired;
var paidPartial = invoice.ExceptionStatus == InvoiceExceptionStatus.PaidPartial;
- context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.Expired) { PaidPartial = paidPartial });
+ context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.Expired) { PaidPartial = paidPartial, PreviousStatus = InvoiceStatus.New });
if (invoice.ExceptionStatus == InvoiceExceptionStatus.PaidPartial)
- context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.ExpiredPaidPartial) { PaidPartial = paidPartial });
+ context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.ExpiredPaidPartial) { PaidPartial = paidPartial, PreviousStatus = InvoiceStatus.New });
}
var hasPayment = invoice.GetPayments(true).Any();
@@ -99,7 +99,7 @@ private void UpdateInvoice(UpdateInvoiceContext context)
{
if (invoice.Status == InvoiceStatus.New)
{
- context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.PaidInFull));
+ context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.PaidInFull) { PreviousStatus = invoice.Status });
invoice.Status = InvoiceStatus.Processing;
if (invoice.IsUnsetTopUp())
{
@@ -165,15 +165,16 @@ private void UpdateInvoice(UpdateInvoiceContext context)
// And not enough amount confirmed
(minimumDue > 0.0m))
{
- context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.FailedToConfirm));
+ context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.FailedToConfirm) { PreviousStatus = invoice.Status });
invoice.Status = InvoiceStatus.Invalid;
context.MarkDirty();
}
else if (minimumDue <= 0.0m)
{
+ var previousStatus = invoice.Status;
invoice.Status = InvoiceStatus.Settled;
- context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.Confirmed));
- context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.Completed));
+ context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.Confirmed) { PreviousStatus = previousStatus });
+ context.Events.Add(new InvoiceEvent(invoice, InvoiceEvent.Completed) { PreviousStatus = previousStatus });
context.MarkDirty();
}
}
### BTCPayServer/Services/Invoices/InvoiceRepository.cs
@@ -660,10 +660,12 @@ public async Task<bool> MarkInvoiceStatus(string invoiceId, InvoiceStatus status
return false;
}
+ var previousStatus = invoiceData.GetInvoiceState().Status;
invoiceData.Status = status.ToString();
invoiceData.ExceptionStatus = InvoiceExceptionStatus.Marked.ToString();
+ var invoiceEvent = new InvoiceEvent(ToEntity(invoiceData), eventName) { PreviousStatus = previousStatus };
await context.SaveChangesAsync();
- _eventAggregator.Publish(new InvoiceEvent(ToEntity(invoiceData), eventName));
+ _eventAggregator.Publish(invoiceEvent);
}
return true;Why this scored 47/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.