r/Supabase • u/BiosRios • 1d ago
tips Supabase bugs that only show up when two users click at the same time (from testing a lot of AI-built apps)
I've spent the last few months testing AI-built Supabase apps for one kind of bug: the kind that passes every normal test and only breaks when two requests overlap. These are the ones I keep finding.
- The last seat gets sold twice. A booking demo checked "is there a seat left?" and then inserted the booking. With two guests at once, both passed the check. In 12 of 20 test orders, the last seat was booked twice. Fix: lock the row before the capacity check (
select ... for update), or do the check and the write in one statement. - A limit of 100 ends at 101. A workspace capped at 100 accounts counted the rows, then inserted. At 99, two creates both saw 99 and both got in. Fix: keep a counter column and take a slot with
update ... set remaining = remaining - 1 where remaining > 0 returning ..., so only one request can win. - Credits get spent twice. Read the balance in JS, subtract, write it back. Two requests read the same balance and both succeed. Fix: do it in one statement,
update profiles set credits = credits - 1 where id = ... and credits > 0, and treat 0 updated rows as "not enough credits". - Stripe webhooks arrive out of order. If your handler copies the subscription status from the event into a table, a late "active" event can overwrite a newer "canceled". In 6 of 7 public handlers I tried, the delivery order changed the final state. Fix: in a worker, re-fetch the subscription from Stripe and write that, one worker per subscription.
- Deduping by event ID isn't enough. A new event ID can still carry old state. Dedup protects you from retries, not from order.
How to test any of these: two connections, the same starting state, run the action at the same time, then check what was actually saved. Normal tests run one request at a time, so they stay green.
What would you add to the list?
1
Upvotes
5
u/LessThanThreeBikes 1d ago
This isn't a Supabase bug. Your AI built app has a concurrency bug known as a race condition. This is a problem that has been solved many times over. Ask your AI agent to explain the logic and test for race conditions. Supabase/Postgres, has all the capabilities to address your issues. You (your AI agent) need to use the capabilities properly.
2
u/No-Fix-2116 1d ago
Running both connections 'at the same time' doesn't reliably overlap anything. Without a barrier before the insert, that test can stay green for months and then fail the first time real traffic hits it.
2
u/bigtime_porgrammer 1d ago
The first few are commonly referred to as TOCTOU bugs (time of check / time of use). They can be subtle and go unnoticed, but there are also painfully obvious cases like the ones you listed. They are definitely a consideration while designing and implementing a system for any seasoned developer... Come on, AI!
1
u/VigiliaFounder 1d ago
Thanks for writing this up, really useful list!
I like the fix in point 3, treating 0 updated rows as "not enough credits". Once you do that, the losing request fails cleanly instead of silently spending twice, and it's worth knowing how often that happens with real users.
For anyone reading who ships on Supabase, Vigilia can help there. It will watch your live app, catch failed requests and other errors, and show you the evidence, how many times it happened and a fix prompt for your AI.
You can start with 1 app for free.
Let me know if this helps!
1
u/Weak_Particular_5799 1d ago
for #1 it matters which row you lock. count(*) ... for update just errors (not allowed with aggregates), and locking the existing bookings doesn't help when there are 0 of them, in my test two guests still both got the last seat. locking the event row is what made the second one wait.
1
u/Brainerce 19h ago
Two from the checkout side that pass every single-request test.
Double click on "place order". The button fires twice, two checkout sessions get created, and if the user is quick enough they pay twice. Disabling the button in JS isn't a fix, a slow network retries for you. The fix is an idempotency key on the session create call (Stripe accepts one per request and returns the same session for the same key) plus a unique constraint on cart_id in the orders table so the second insert fails instead of becoming a second order.
The other one isn't two users, it's one user racing Stripe. The success redirect lands on your site before the webhook does, your thank-you page reads the order, sees "pending" and tells a customer who just paid that something went wrong. Order of arrival is not guaranteed in either direction. The page has to treat pending as normal and poll for a few seconds, or just thank them and say the confirmation is on its way. Same lesson as your point 4 really, the event is the truth, the redirect is a hint.
0
u/HittingSmoke 16h ago edited 14h ago
Just looking at the top of this subreddit right now, this community is such a joke. Vibe coders vibe tutorialing basic programming concepts that anyone shipping production software should already know like they just discovered some novel solution clearly also written by an LLM.